QT-Qt图片按钮类
main.cpp
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
TImageButton.h
1 #ifndef TIMAGEBUTTON_H 2 #define TIMAGEBUTTON_H 3 4 #include <QPushButton> 5 #include <QMouseEvent> 6 #include <QPainter> 7 8 class TImageButton : public QPushButton 9 { 10 Q_OBJECT 11 public: 12 TImageButton(QWidget *parent, QString normal, QString hover, QString lighted); 13 ~TImageButton(); 14 void SetLighted(bool value); 15 public: 16 void paintEvent(QPaintEvent* pEvent); 17 void mousePressEvent(QMouseEvent *e); 18 void mouseReleaseEvent(QMouseEvent *e); 19 void enterEvent(QEvent *e); 20 void leaveEvent(QEvent *e); 21 protected: 22 bool m_bLighted; 23 bool m_bMouseIN; 24 bool m_bLeftDown; 25 QPixmap m_pHover; 26 QPixmap m_pNormal; 27 QPixmap m_pLighted; 28 QString m_sHover; 29 QString m_sNormal; 30 QString m_sLighted; 31 32 }; 33 34 #endif // TIMAGEBUTTON_H
TImageButton.cpp
1 #include "TImageButton.h" 2 3 4 TImageButton::TImageButton(QWidget *parent, QString normal, QString hover, QString lighted) 5 :QPushButton(parent), 6 m_bMouseIN(false), 7 m_bLeftDown(false), 8 m_bLighted(false) 9 { 10 m_sNormal = normal; 11 m_sHover = hover; 12 m_sLighted = lighted; 13 14 QImage *image = new QImage; 15 image->load(m_sNormal); 16 m_pNormal = QPixmap::fromImage(*image); 17 QImage *image2 = new QImage; 18 image2->load(m_sHover); 19 m_pHover = QPixmap::fromImage(*image2); 20 QImage *image3 = new QImage; 21 image3->load(m_sLighted); 22 m_pLighted = QPixmap::fromImage(*image3); 23 24 this->setFixedWidth(m_pNormal.width()); 25 this->setFixedHeight(m_pNormal.height()); 26 } 27 28 TImageButton::~TImageButton() 29 { 30 31 } 32 33 void TImageButton::SetLighted(bool value) 34 { 35 if(value != m_bLighted) 36 { 37 m_bLighted=value; 38 repaint(); 39 } 40 } 41 42 void TImageButton::paintEvent(QPaintEvent *pEvent) 43 { 44 QPixmap* pPixmap; 45 if(m_bLighted || m_bLeftDown) 46 pPixmap=&m_pLighted; 47 else 48 { 49 if(m_bMouseIN) 50 pPixmap=&m_pHover; 51 else 52 pPixmap=&m_pNormal; 53 } 54 QPainter painter; 55 painter.begin(this); 56 if(pPixmap) 57 painter.drawPixmap(rect(),*pPixmap); 58 if(text().count() > 0) 59 painter.drawText(rect(),Qt::AlignCenter,text()); 60 painter.end(); 61 } 62 63 void TImageButton::mousePressEvent(QMouseEvent *e) 64 { 65 if(e->button()==Qt::LeftButton) 66 { 67 m_bLeftDown=true; 68 repaint(); 69 } 70 QPushButton::mousePressEvent(e); 71 } 72 73 void TImageButton::mouseReleaseEvent(QMouseEvent *e) 74 { 75 if(e->button()==Qt::LeftButton) 76 { 77 m_bLeftDown=false; 78 repaint(); 79 } 80 QPushButton::mouseReleaseEvent(e); 81 } 82 83 void TImageButton::enterEvent(QEvent *e) 84 { 85 m_bMouseIN=true; 86 repaint(); 87 } 88 89 void TImageButton::leaveEvent(QEvent *e) 90 { 91 m_bMouseIN=false; 92 repaint(); 93 }
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QHBoxLayout> 6 #include <QDialog> 7 8 #include "TImageButton.h" 9 10 QT_BEGIN_NAMESPACE 11 namespace Ui { class MainWindow; } 12 QT_END_NAMESPACE 13 14 class MainWindow : public QMainWindow 15 { 16 Q_OBJECT 17 18 public: 19 MainWindow(QWidget *parent = nullptr); 20 ~MainWindow(); 21 22 private: 23 Ui::MainWindow *ui; 24 25 QHBoxLayout *m_pHBoxLayout; 26 TImageButton *m_pImageButton; 27 28 private slots: 29 void on_bu(); 30 }; 31 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 resize(400, 300); 10 setWindowTitle(QStringLiteral("Qt图片按钮类")); 11 12 m_pHBoxLayout = new QHBoxLayout(this); 13 m_pImageButton = new TImageButton(this, ":/new/prefix1/A.png", ":/new/prefix1/B.png", ":/new/prefix1/C.png"); 14 m_pHBoxLayout->addWidget(m_pImageButton); 15 // 设置显示内容,如果图片代文字的话就不需要这一行代码。 16 m_pImageButton->setText(QString::fromLocal8Bit("123as")); 17 // 设置字体为黑色,如果图片代文字的话就不需要这一行代码。 18 m_pImageButton->setStyleSheet("color: rgb(0, 0, 0);"); 19 connect(m_pImageButton, &QPushButton::clicked, this, &MainWindow::on_bu); 20 ui->centralwidget->setLayout(m_pHBoxLayout); 21 } 22 23 MainWindow::~MainWindow() 24 { 25 delete ui; 26 } 27 28 void MainWindow::on_bu() 29 { 30 QDialog *odia = new QDialog; 31 QHBoxLayout *ohb = new QHBoxLayout; 32 QPushButton *obu = new QPushButton; 33 obu->setText("OK"); 34 ohb->addWidget(obu); 35 odia->setLayout(ohb); 36 odia->show(); 37 }
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我