Please don't edit posts in such a way that it breaks replies: it makes it hard to follow for others.
Apart from the obvious alternative of putting an image on a QPushButton, which is designed for clicking, you could:
#include <QtGui>
#include <QDebug>
class ClickLabel
: public QLabel {
Q_OBJECT
public:
ClickLabel
(QWidget * parent
= 0, Qt
::WindowFlags f
= 0): { }
ClickLabel
(const QString & text,
QWidget * parent
= 0, Qt
::WindowFlags f
= 0): { }
protected:
if (ev->button() == Qt::LeftButton) {
emit clicked();
qDebug() << "Click";
}
}
signals:
void clicked();
};
int main(int argc, char *argv[])
{
ClickLabel w1("Some text to click");
w1.show();
ClickLabel w2;
w2.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class ClickLabel: public QLabel
{
Q_OBJECT
public:
ClickLabel(QWidget * parent = 0, Qt::WindowFlags f = 0):
QLabel(parent, f)
{ }
ClickLabel(const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0):
QLabel(text, parent, f)
{ }
protected:
void mousePressEvent ( QMouseEvent * ev ) {
if (ev->button() == Qt::LeftButton) {
emit clicked();
qDebug() << "Click";
}
}
signals:
void clicked();
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ClickLabel w1("Some text to click");
w1.show();
ClickLabel w2;
w2.setPixmap(QPixmap("test.png"));
w2.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks