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

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

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

  1. class QImageButtonP : public QAbstractButton   
  2. {  
  3.     Q_OBJECT  
  4. public:  
  5.     QImageButtonP(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked ="");  
  6.     QSize sizeHint() const;  
  7.     QSize minimumSizeHint() const;  
  8.     QSize maximumSize() const;  
  9.  
  10.     void changePics(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked = "");  
  11. protected:  
  12.     void paintEvent(QPaintEvent *e);  
  13.     void mouseMoveEvent(QMouseEvent *e);  
  14.     void mousePressEvent(QMouseEvent *e);  
  15.     void mouseReleaseEvent(QMouseEvent *e);  
  16. private:  
  17.     QPixmap myPic, myHoveredPic, myCheckedPic, myPressedPic;  
  18.     int lastUnderMouse; // last mouse pos recorded  
  19.     bool bpressed;  
  20.  
  21.     enum State {  
  22.         Normal,  
  23.         Hovered,  
  24.         Checked,  
  25.         Pressed  
  26.     };  
  27.     int lastState;  
  28. };  
  29.  
  30.  
  31. QImageButtonP::QImageButtonP(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked)   
  32.             : myPic(normal), myHoveredPic(hovered), myPressedPic(pressed), lastUnderMouse(-1), bpressed(false)  
  33. {  
  34.     setFixedSize(myPic.size());  
  35. #if defined(WIN32) || defined(WIN64)  
  36.     setMask(::mask(myPic));  
  37. #endif  
  38.     lastState = Normal;  
  39.  
  40.     /* Both are necessary for some styles */  
  41.     setMouseTracking(true);  
  42.     setAttribute(Qt::WA_Hover, true);  
  43.  
  44.     if (checked != "")  
  45.         myCheckedPic = QPixmap(checked);  
  46. }  
  47.  
  48. void QImageButtonP::changePics(const QString &normal, const QString &hovered,const QString &pressed, const QString &checked)  
  49. {  
  50.     myPic = QPixmap(normal);  
  51.     myHoveredPic = QPixmap(hovered);  
  52.     myPressedPic = QPixmap(pressed);  
  53.     if (checked != "")  
  54.         myCheckedPic = QPixmap(checked);  
  55.  
  56. #if defined(WIN32) || defined(WIN64)  
  57.     setMask(lastState == Checked ? ::mask(myCheckedPic) : (lastState == Normal ? ::mask(myPic) : 
  58. (lastState == Pressed ? ::mask(myPressedPic) : ::mask(myHoveredPic))));  
  59. #endif  
  60.  
  61.     update();  
  62. }  
  63.  
  64. void QImageButtonP::mousePressEvent(QMouseEvent *e)  
  65. {  
  66.     bpressed = true;  
  67.     QAbstractButton::mousePressEvent(e);  
  68. }  
  69.  
  70. void QImageButtonP::mouseReleaseEvent(QMouseEvent *e)  
  71. {  
  72.     bpressed = false;  
  73.     QAbstractButton::mouseReleaseEvent(e);  
  74. }  
  75.  
  76. QSize QImageButtonP::sizeHint() const  
  77. {  
  78.     return myPic.size();  
  79. }  
  80.  
  81. QSize QImageButtonP::minimumSizeHint() const  
  82. {  
  83.     return sizeHint();  
  84. }  
  85.  
  86. QSize QImageButtonP::maximumSize() const  
  87. {  
  88.     return sizeHint();  
  89. }  
  90.  
  91. void QImageButtonP::paintEvent(QPaintEvent *e)  
  92. {  
  93.     QPainter painter(this);  
  94.  
  95.     int newState;  
  96.     if ((this->isChecked()) && !myCheckedPic.isNull()) {  
  97.         newState = Checked;  
  98.         painter.drawPixmap(e->rect(), myCheckedPic, e->rect());  
  99.     }else if(this->isDown () && !myPressedPic.isNull()) {  
  100.         newState = Pressed;  
  101.         painter.drawPixmap(e->rect(), myPressedPic, e->rect());  
  102.     }else {  
  103.         if (!underMouse()) {  
  104.             newState = Normal;  
  105.             painter.drawPixmap(e->rect(), myPic, e->rect());  
  106.         }  
  107.         else {  
  108.             newState = Hovered;  
  109.             painter.drawPixmap(e->rect(), myHoveredPic, e->rect());  
  110.         }  
  111.     }  
  112.  
  113.     if (newState != lastState) {  
  114.         lastState = newState;  
  115. #if defined(WIN32) || defined(WIN64)  
  116.         setMask(lastState == Checked ? ::mask(myCheckedPic) : (lastState == Normal ? ::mask(myPic) : 
  117. (lastState == Pressed ? ::mask(myPressedPic) : ::mask(myHoveredPic))));  
  118. #endif  
  119.     }  
  120.  
  121.     lastUnderMouse = underMouse();  
  122. }  
  123.  
  124. void QImageButtonP::mouseMoveEvent(QMouseEvent *)  
  125. {  
  126.     if (int(underMouse()) == lastUnderMouse)  
  127.         return;  
  128.     update();  

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

  1. class QIRCLineEdit : public QLineEdit   
  2. {  
  3.     Q_OBJECT  
  4. public:  
  5.     QIRCLineEdit();  
  6. private:  
  7.     void keyPressEvent(QKeyEvent *);  
  8.     QList<QString> m_Inputlist1;//Stores the inputed strings,up list.  
  9.     QList<QString> m_Inputlist2;//Stores the inputed strings,down list.  
  10.     //QString m_Currentline;//Stores a copy of the current text in the LineEdit.  
  11. public slots:  
  12.     void myTextEdited();  
  13.     void myclear();  
  14. };  
  15.  
  16.  
  17. QIRCLineEdit::QIRCLineEdit()   
  18. {  
  19.     connect(this,SIGNAL(textEdited(QString)),this,SLOT(myTextEdited()));  
  20. }  
  21.  
  22. void QIRCLineEdit::keyPressEvent(QKeyEvent *e)  
  23. {  
  24.     if(e->key() == Qt::Key_Up) {  
  25.         if(text()==""){  
  26.             if(m_Inputlist1.empty()){  
  27.  
  28.             }else{  
  29.                 setText(m_Inputlist1.last());  
  30.                 m_Inputlist1.removeLast();  
  31.             }  
  32.         }else if(text()!=""){  
  33.             m_Inputlist2.append(text());  
  34.             if(m_Inputlist1.empty()){  
  35.                 clear();  
  36.             }else{  
  37.                 setText(m_Inputlist1.last());  
  38.                 m_Inputlist1.removeLast();  
  39.             }  
  40.         }  
  41.     }else if(e->key() == Qt::Key_Down) {  
  42.         if(text()==""){  
  43.             if(m_Inputlist2.empty()){  
  44.  
  45.             }else{  
  46.                 setText(m_Inputlist2.first());  
  47.                 m_Inputlist2.removeFirst();  
  48.             }  
  49.         }else if(text()!=""){  
  50.             if(m_Inputlist2.empty()){  
  51.                 m_Inputlist1.append(text());  
  52.                 clear();  
  53.             }else{  
  54.                 m_Inputlist1.append(text());  
  55.                 setText(m_Inputlist2.first());  
  56.                 m_Inputlist2.removeFirst();  
  57.             }  
  58.         }  
  59.     }else{  
  60.         QLineEdit::keyPressEvent(e);  
  61.     }  
  62.  
  63. }  
  64. void QIRCLineEdit::myTextEdited()  
  65. {  
  66.     //if(!m_Inputlist2.empty()) m_Inputlist1.append(m_Currentline);  
  67.     m_Inputlist2.clear();  
  68. }  
  69.  
  70. void QIRCLineEdit::myclear()  
  71. {  
  72.     if(text()!=""){  
  73.         m_Inputlist1.append(text());  
  74.     }  
  75.     QLineEdit::clear();  

小结:详解两个自制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实例

2009-07-15 18:29:22

Jython应用

2011-09-09 19:05:28

Widget

2011-06-20 16:38:33

Qt QWidget Qt4.5

2011-09-06 10:46:19

QT播放器

2010-02-25 16:45:13

WCF应用技巧

2016-03-31 11:28:21

imageView图片轮播

2010-07-17 00:50:12

batch Telne

2011-09-07 14:20:42

Android Wid组件

2010-04-20 15:09:05

负载均衡

2011-09-07 16:24:10

Qt Widget

2013-06-20 09:35:18

Instagramvine短视频应用

2010-04-08 10:17:37

Oracle体系结构

2011-09-09 10:19:13

2011-09-08 11:43:32

GTK Widget

2011-09-08 10:46:12

Widget
点赞
收藏

51CTO技术栈公众号