标签:.exe bsp ext nbsp == com code amp bool
原本QLabel控件是没有CLICK事件的,我们可以通过重写函数继承QLabel的方法让QLabel有CLICK事件
直接上代码,如果看文章看不懂,可以去我网盘看看视频:http://m-carr.ys168.com,在Qt视频教程目录里面
#ifndef MYLABEL_H #define MYLABEL_H #include <QLabel> #include <QMessageBox> #include <QMouseEvent> class MyLabel:public QLabel { Q_OBJECT public: MyLabel(const QString & text,QWidget *parent=0); MyLabel(QWidget *parent=0); signals: public slots: public: virtual bool event(QEvent *event) override; }; #endif // MYLABEL_H
#include "mylabel.h" MyLabel::MyLabel(const QString & text,QWidget *parent):QLabel(parent) { setText(text); } MyLabel::MyLabel(QWidget *parent):QLabel(parent) { } bool MyLabel::event(QEvent *e) { if(e->type()==QEvent::MouseButtonPress) { QMouseEvent *mouseEvent=static_cast<QMouseEvent*>(e); if(mouseEvent->button()==Qt::LeftButton) { QMessageBox::information(NULL,"提示","点击QLabel控件弹出了我",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes); return true; } } return QLabel::event(e); }
#include "mainwindow.h" #include "ui_mainwindow.h" #include "mylabel.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); MyLabel* label=new MyLabel("点我试试",this); label->setGeometry(20,20,30,30); } MainWindow::~MainWindow() { delete ui; }
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
标签:.exe bsp ext nbsp == com code amp bool
原文地址:http://www.cnblogs.com/yanxiao/p/7684919.html