自定義Qt按鈕


轉自:http://blog.csdn.net/starcloud_zxt/article/details/5185556

       Qt自帶的PushButton樣式比較單一,在開發的時候往往按鈕的形狀各異,所以需要自定義Qt的按鈕。其方法是做一張圖片來作為按鈕,如果需要動態效果的話,可以做兩張圖片進行替換。按鈕的載體可以是QLabel、QPushButton,可以通過QStyle類來設計樣式,如果對QStyle不太了解的話,可以用下面的方法來實現。

1. 使用QPushButton

    通過自定義一個按鈕樣式函數,在該函數中設置按鈕的樣式。(可以設計一個QPushButton的子類來完成設置)

實現代碼:

 

[c-sharp]  view plain copy
 
 
  1. QPushButton *custButton(QString str,QString str1)  
  2. {  
  3.     QPushButton *pushButton= new QPushButton;  
  4.   
  5.     pushButton->setGeometry(10,10,200,200); //按鈕的位置及大小  
  6.     pushButton->clearMask();  
  7.     pushButton->setBackgroundRole( QPalette::Base);  
  8.   
  9.     QPixmap mypixmap;   mypixmap.load(str);  
  10.   
  11.     pushButton->setFixedSize( mypixmap.width(), mypixmap.height() );  
  12.     pushButton->setMask(mypixmap.createHeuristicMask());  
  13.     pushButton->setIcon(mypixmap);  
  14.     pushButton->setIconSize(QSize(mypixmap.width(),mypixmap.height()));  
  15.     pushButton->setToolTip(str1);  
  16.     return pushButton;  
  17. }  

 

調用代碼:

 

[c-sharp]  view plain copy
 
 
  1. QPushButton *btn=custButton("../login.png", "LOGIN");  
  2.   
  3. connect(btn, SIGNAL(clicked()), this, SLOT(slotLogin()));  

 

 

2. 通過QLabel

   我們可以把一個圖片放在QLabel里面作為按鈕,因為我沒有找到QLabel是否有當點擊后發出的信號,所以自定義了一個鼠標事件用來檢測是否在QLabel上點擊了鼠標。在自定義的鼠標事件中檢測QLabel所在的區域,當在該區域發生鼠標點擊事件后,發送信號。

   設計時通過Qt Creator在widget.ui中加入一個QLabel即可,不需要進行設置。

代碼widget.h

 

[c-sharp]  view plain copy
 
 
  1. #ifndef WIDGET_H  
  2. #define WIDGET_H  
  3.  
  4. #include <QWidget>  
  5.   
  6. namespace Ui {  
  7.     class Widget;  
  8. }  
  9.   
  10. class Widget : public QWidget {  
  11.     Q_OBJECT  
  12. public:  
  13.     Widget(QWidget *parent = 0);  
  14.     ~Widget();  
  15.   
  16. signals:  
  17.     void clicked();  
  18.   
  19. protected:  
  20.     void mousePressEvent(QMouseEvent *e);  
  21.   
  22. protected slots:  
  23.     void slotClicked();  
  24.   
  25. private:  
  26.     Ui::Widget *ui;  
  27. };  
  28.  
  29. #endif // WIDGET_H  

 

 

代碼widget.cpp

 

[c-sharp]  view plain copy
 
 
  1. #include "widget.h"  
  2. #include "ui_widget.h"  
  3. #include <QMouseEvent>  
  4. #include <QMessageBox>  
  5. #include <QPixmap>  
  6. #include <QLabel>  
  7.   
  8. Widget::Widget(QWidget *parent) :  
  9.     QWidget(parent),  
  10.     ui(new Ui::Widget)  
  11. {  
  12.     ui->setupUi(this);  
  13.     //使用label作為按鈕,通過自定義鼠標事件,點擊label所在區域實現鼠標單擊  
  14.     QPixmap pm;   pm.load("../logo.png");  
  15.     ui->label->setGeometry(0,0,pm.width(), pm.height());  
  16.     ui->label->setPixmap(pm);  
  17.   
  18.     connect( this, SIGNAL(clicked()), this, SLOT(slotClicked()));   //信號連接  
  19. }  
  20.   
  21. Widget::~Widget()  
  22. {  
  23.     delete ui;  
  24. }  
  25.   
  26.   
  27. void Widget::mousePressEvent(QMouseEvent *e)  
  28. {  
  29.     int x = e->x();  
  30.     int y = e->y();  
  31.   
  32.     //假如在QRect( 0, 0, 48, 48 )這個區域里(圖片大小為48X48),就發出信號  
  33.     if (x>0 && x<48 && y>0 && y<48){  
  34.         emit clicked();  
  35.     }  
  36. }  
  37.   
  38. void Widget::slotClicked()  
  39. {  
  40.     QMessageBox::about( this, "Mouse Example", "You have pressed mouse, exit now!");  
  41.     close();  
  42.   
  43. }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM