详解两个自制Qt Widget应用案例实现

移动开发
Qt Widget应用案例实现是本文要介绍的内容,文章中主要讲解了自制的两个Qt Widget应用,具体内容的实现来看本文详解。

Qt Widget应用案例实现是本文要介绍的内容,文章中主要讲解了自制的两个Qt Widget应用,具体内容的实现来看本文详解。Qt自带的Widget不大够用了,于是和他一起做了两个。一个是图片按钮。这是一个基于QAbstractButton的按钮,按钮的实体是图片,coyotte508实现的时候忽略了按钮被按下时的效果,我给他完善了一下。

class QImageButtonP : public QAbstractButton   
{  
    Q_OBJECT  
public:  
    QImageButtonP(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked ="");  
    QSize sizeHint() const;  
    QSize minimumSizeHint() const;  
    QSize maximumSize() const;  
 
    void changePics(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked = "");  
protected:  
    void paintEvent(QPaintEvent *e);  
    void mouseMoveEvent(QMouseEvent *e);  
    void mousePressEvent(QMouseEvent *e);  
    void mouseReleaseEvent(QMouseEvent *e);  
private:  
    QPixmap myPic, myHoveredPic, myCheckedPic, myPressedPic;  
    int lastUnderMouse; // last mouse pos recorded  
    bool bpressed;  
 
    enum State {  
        Normal,  
        Hovered,  
        Checked,  
        Pressed  
    };  
    int lastState;  
};  
 
 
QImageButtonP::QImageButtonP(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked)   
            : myPic(normal), myHoveredPic(hovered), myPressedPic(pressed), lastUnderMouse(-1), bpressed(false)  
{  
    setFixedSize(myPic.size());  
#if defined(WIN32) || defined(WIN64)  
    setMask(::mask(myPic));  
#endif  
    lastState = Normal;  
 
    /* Both are necessary for some styles */  
    setMouseTracking(true);  
    setAttribute(Qt::WA_Hover, true);  
 
    if (checked != "")  
        myCheckedPic = QPixmap(checked);  
}  
 
void QImageButtonP::changePics(const QString &normal, const QString &hovered,const QString &pressed, const QString &checked)  
{  
    myPic = QPixmap(normal);  
    myHoveredPic = QPixmap(hovered);  
    myPressedPic = QPixmap(pressed);  
    if (checked != "")  
        myCheckedPic = QPixmap(checked);  
 
#if defined(WIN32) || defined(WIN64)  
    setMask(lastState == Checked ? ::mask(myCheckedPic) : (lastState == Normal ? ::mask(myPic) : 
              (lastState == Pressed ? ::mask(myPressedPic) : ::mask(myHoveredPic))));  
#endif  
 
    update();  
}  
 
void QImageButtonP::mousePressEvent(QMouseEvent *e)  
{  
    bpressed = true;  
    QAbstractButton::mousePressEvent(e);  
}  
 
void QImageButtonP::mouseReleaseEvent(QMouseEvent *e)  
{  
    bpressed = false;  
    QAbstractButton::mouseReleaseEvent(e);  
}  
 
QSize QImageButtonP::sizeHint() const  
{  
    return myPic.size();  
}  
 
QSize QImageButtonP::minimumSizeHint() const  
{  
    return sizeHint();  
}  
 
QSize QImageButtonP::maximumSize() const  
{  
    return sizeHint();  
}  
 
void QImageButtonP::paintEvent(QPaintEvent *e)  
{  
    QPainter painter(this);  
 
    int newState;  
    if ((this->isChecked()) && !myCheckedPic.isNull()) {  
        newState = Checked;  
        painter.drawPixmap(e->rect(), myCheckedPic, e->rect());  
    }else if(this->isDown () && !myPressedPic.isNull()) {  
        newState = Pressed;  
        painter.drawPixmap(e->rect(), myPressedPic, e->rect());  
    }else {  
        if (!underMouse()) {  
            newState = Normal;  
            painter.drawPixmap(e->rect(), myPic, e->rect());  
        }  
        else {  
            newState = Hovered;  
            painter.drawPixmap(e->rect(), myHoveredPic, e->rect());  
        }  
    }  
 
    if (newState != lastState) {  
        lastState = newState;  
#if defined(WIN32) || defined(WIN64)  
        setMask(lastState == Checked ? ::mask(myCheckedPic) : (lastState == Normal ? ::mask(myPic) : 
               (lastState == Pressed ? ::mask(myPressedPic) : ::mask(myHoveredPic))));  
#endif  
    }  
 
    lastUnderMouse = underMouse();  
}  
 
void QImageButtonP::mouseMoveEvent(QMouseEvent *)  
{  
    if (int(underMouse()) == lastUnderMouse)  
        return;  
    update();  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.

另一个是IRC聊天输入框模式的一个输入框,输入框对光标-上和光标-下两个键盘输入产生反应,能够回到之前输入的内容或者返回当前内容。

class QIRCLineEdit : public QLineEdit   
{  
    Q_OBJECT  
public:  
    QIRCLineEdit();  
private:  
    void keyPressEvent(QKeyEvent *);  
    QList<QString> m_Inputlist1;//Stores the inputed strings,up list.  
    QList<QString> m_Inputlist2;//Stores the inputed strings,down list.  
    //QString m_Currentline;//Stores a copy of the current text in the LineEdit.  
public slots:  
    void myTextEdited();  
    void myclear();  
};  
 
 
QIRCLineEdit::QIRCLineEdit()   
{  
    connect(this,SIGNAL(textEdited(QString)),this,SLOT(myTextEdited()));  
}  
 
void QIRCLineEdit::keyPressEvent(QKeyEvent *e)  
{  
    if(e->key() == Qt::Key_Up) {  
        if(text()==""){  
            if(m_Inputlist1.empty()){  
 
            }else{  
                setText(m_Inputlist1.last());  
                m_Inputlist1.removeLast();  
            }  
        }else if(text()!=""){  
            m_Inputlist2.append(text());  
            if(m_Inputlist1.empty()){  
                clear();  
            }else{  
                setText(m_Inputlist1.last());  
                m_Inputlist1.removeLast();  
            }  
        }  
    }else if(e->key() == Qt::Key_Down) {  
        if(text()==""){  
            if(m_Inputlist2.empty()){  
 
            }else{  
                setText(m_Inputlist2.first());  
                m_Inputlist2.removeFirst();  
            }  
        }else if(text()!=""){  
            if(m_Inputlist2.empty()){  
                m_Inputlist1.append(text());  
                clear();  
            }else{  
                m_Inputlist1.append(text());  
                setText(m_Inputlist2.first());  
                m_Inputlist2.removeFirst();  
            }  
        }  
    }else{  
        QLineEdit::keyPressEvent(e);  
    }  
 
}  
void QIRCLineEdit::myTextEdited()  
{  
    //if(!m_Inputlist2.empty()) m_Inputlist1.append(m_Currentline);  
    m_Inputlist2.clear();  
}  
 
void QIRCLineEdit::myclear()  
{  
    if(text()!=""){  
        m_Inputlist1.append(text());  
    }  
    QLineEdit::clear();  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.

小结:详解两个自制Qt Widget应用案例实现的内容介绍完了,希望通过Qt Widget应用内容的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-09-07 16:28:46

QT WidgetQWidget

2011-09-09 17:59:26

QT Widget

2011-09-07 16:36:00

Qt Widget

2011-09-01 14:04:45

QT Webkit插件

2011-09-08 13:11:07

Android Wid实例

2011-09-09 19:05:28

Widget

2009-07-15 18:29:22

Jython应用

2011-06-20 16:38:33

Qt QWidget Qt4.5

2011-09-06 10:46:19

QT播放器

2011-09-07 14:20:42

Android Wid组件

2010-02-25 16:45:13

WCF应用技巧

2011-09-07 16:24:10

Qt Widget

2016-03-31 11:28:21

imageView图片轮播

2011-09-08 10:46:12

Widget

2010-07-17 00:50:12

batch Telne

2011-09-08 11:43:32

GTK Widget

2011-09-09 10:19:13

2010-04-20 15:09:05

负载均衡

2010-07-13 09:02:19

Widget开发

2010-04-08 10:17:37

Oracle体系结构
点赞
收藏

51CTO技术栈公众号