有2种方法 最实用的就是用eventFilter的方式
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
// explicit
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
// Ui::MainWindow *ui;
QLabel *l1;
QLabel *l2;
protected:
bool eventFilter(QObject *obj, QEvent *ev);
};
#endif // MAINWINDOW_H
*************************************
mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QLabel>#include <QKeyEvent>#include "qDebug"#include "QMessageBox"#include <QtGui>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent)//,// ui(new Ui::MainWindow){
//ui->setupUi(this);l1 = new QLabel(this);l2 = new QLabel(this);l1->setText("123");l2->setText("345");QGridLayout *tt1 = new QGridLayout(this);tt1->addWidget(l1,6,6);tt1->addWidget(l2,1,3);QWidget * w = new QWidget(this);w->setLayout(tt1);this->setCentralWidget(w);l1->installEventFilter(this);}
bool MainWindow::eventFilter(QObject *obj, QEvent *event){
if (obj == l1) {if (event->type() == QEvent::MouseButtonPress) {QMessageBox::information(NULL, QString::fromLocal8Bit("单击"), QString::fromLocal8Bit("单击确定"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);return true;} else {return false;}} else {// pass the event on to the parent classreturn QMainWindow::eventFilter(obj, event);}}
MainWindow::~MainWindow(){
// delete ui;}
*************************************************
第2种内部得到单击事件,继承子QLabel
mainwindow.h#ifndef MAINWINDOW_H
#define MAINWINDOW_Hclass QLabel;class myLabel;#include <QMainWindow>namespace Ui {class MainWindow;}
class MainWindow : public QMainWindow{
Q_OBJECTpublic:// explicitMainWindow(QWidget *parent = 0);~MainWindow();private:// Ui::MainWindow *ui;QLabel *l1;myLabel *m_label;};
#endif // MAINWINDOW_H******************************
MYLABEL.H
#ifndef MYLABEL_H#define MYLABEL_H#include <QLabel>#include <QWidget>#include <QString>#include <QMainWindow>class myLabel : public QLabel{
Q_OBJECTpublic:myLabel(const QString & text, QWidget * parent = 0);~myLabel(){}signals:void clicked();public slots:void slotClicked();protected:public:void mousePressEvent ( QMouseEvent * event ) ;};
#endif // MYLABEL_H*********************mainwindow.cpp#include "mainwindow.h"
#include "ui_mainwindow.h"#include "myLabel.h"#include <QtGui>#include <QString>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent)// , ui(new Ui::MainWindow){
//ui->setupUi(this);
m_label = new myLabel("123",this);m_label->setText(QString::fromLocal8Bit("单击"));QLabel *l1=new QLabel;l1->setText("1234");QGridLayout *tt1 = new QGridLayout(this);tt1->addWidget(m_label,1,0);tt1->addWidget(l1,2,0);QWidget * w = new QWidget(this);w->setLayout(tt1);this->setCentralWidget(w);this->setWindowTitle(tr("main"));}
MainWindow::~MainWindow(){
//delete ui;}
*****************************MYLABEL.CPP#include "myLabel.h"#include "qdebug.h"#include "QMessageBox"#include "mainwindow.h"#include "QString"myLabel::myLabel( const QString & text, QWidget * parent ):QLabel(parent){
connect(this, SIGNAL(clicked()), this, SLOT(slotClicked()));}
void myLabel::slotClicked(){
QMessageBox::information(NULL, QString::fromLocal8Bit("单击"), QString::fromLocal8Bit("单击确定"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);}
void myLabel::mousePressEvent(QMouseEvent *event){
emit clicked();}