Чтобы не быть голословными приведу оба варианта:
C++ (Qt)
#include <QtGui/QApplication>
#include <QtGui/QMouseEvent>
#include <QtGui/QLabel>
class QExLabel : public QLabel
{
Q_OBJECT
public:
QExLabel(QWidget *parent = 0) : QLabel(parent){};
signals:
void clicked();
protected:
void mouseReleaseEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton)
{
emit clicked();
}
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QExLabel label;
QObject::connect(&label, SIGNAL(clicked()), &app, SLOT(quit()));
label.show();
return app.exec();
}
#include <main.moc>
//mainwindow.h
C++ (Qt)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtCore/QtGlobal>
#include <QtCore/QtDebug>
#include <QtGui/QMainWindow>
#include <QtGui/QMouseEvent>
#include <QtGui/QErrorMessage>
namespace Ui
{
class MainWindowClass;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void btnClicked();
signals:
void clicked();
protected:
bool eventFilter(QObject *, QEvent *);
private:
Ui::MainWindowClass *ui;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
C++ (Qt)
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
QErrorMessage::qtHandler();
ui->setupUi(this);
connect(this, SIGNAL(clicked()), SLOT(btnClicked()));
ui->label->installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *ev)
{
switch(ev->type())
{
case QEvent::MouseButtonRelease:
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(ev);
if (mouseEvent->button() == Qt::LeftButton)
{
emit clicked();
return true;
}
break;
}
default:;
}
return QObject::eventFilter(obj, ev);
}
void MainWindow::btnClicked()
{
qDebug() << "clicked";
}
MainWindow::~MainWindow()
{
delete ui;
}