View Full Version : Creating click events for label

24th November 2011, 22:09
Can someone point me in the right direction of making a click-able label? I'm working with some code I found on a site, but I'm not converting it right from key event to most events...

class ClickLabel : public QObject

bool eventFilter(QObject *obj, QEvent *event);

bool ClickLabel::eventFilter(QObject *obj, QEvent *event)
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *MouseEvent = static_cast<QMouseEvent *>(event);
qDebug("MouseClicked", MouseEvent->clicked());
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);


error: invalid static_cast from type 'QEvent*' to type 'QMouseEvent*'

error: invalid use of incomplete type 'struct QMouseEvent'

error: forward declaration of 'struct QMouseEvent'

24th November 2011, 22:58
#include <QMouseEvent>

class ClickLabel : public QObject

24th November 2011, 23:47
Is it easy to inherit the functionality of the QLabel class into the new class, so that I can easily use the same features like set text, and stuff of that nature?

24th November 2011, 23:55
Yes, inherit from QLabel and not QObject.

25th November 2011, 01:01
It's discouraging QT hasn't added a simple clickable image, lol. A label should have a click slot by default, such a simple thing to over look....If I wasn't such a newb I could figure it out, but I need some help, lol.

25th November 2011, 01:09
That is a constructor (http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29) for your class and the arguments are used to initialise the object at creation. In this case the first argument is mandatory (the text of the label), and the second has a default value but could point to a parent QObject. QLabel has two constructors, distinguished by their arguments lists, and this mirrors one of them in your derived class.

Do yourself a favour an learn some basic C++ before you try anything too complicated with Qt.

A clicked() slot would be generally useless in a label. What would a generic label do in response to the clicked() slot being called?
I think you mean a clicked() signal, and you already have the 10 lines of code you need to add it if you need it. Most labels don't need it.

25th November 2011, 01:19
I know. It's for having a clickable image. I'm working on a piece of software, and need an image to be click able. I know some basic C++, just need to learn a little more, but I'm having fun doing it....

A clickable image is a big deal, haha...it should be easier to create...

are there simpler alternatives?

I could use the above code if I could figure out how to make the new class do everything that the regular QTLabel class can. Then I could create my labels, add the pixmaps, and figure out how to use the slicked signal.

You can see what I'm working on here..


I had it all set up, didn't know it would be so hard for me to make a qlabel image clickable...I figured qt would have thought of that one, lol.

25th November 2011, 01:30
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
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)
{ }

void mousePressEvent ( QMouseEvent * ev ) {
if (ev->button() == Qt::LeftButton) {
emit clicked();
qDebug() << "Click";

void clicked();

int main(int argc, char *argv[])
QApplication app(argc, argv);

ClickLabel w1("Some text to click");
ClickLabel w2;
return app.exec();
#include "main.moc"

25th November 2011, 01:44
Oh bomb. You just pointed out something I didn't realize. That's one of the reasons I was having trouble with my new class, and inherited features from QLabel. I was trying to access functions via -> instead of .

That takes me close, see I know where I want to be, just the little things that I need to understand to make it happen. Now I should be able to make my labels, set the pixmaps, and use the signals from my new class right?

I'm going to have to delete the labels that I've already made to code the new ones. How can I take down their quardinates on the form, so that I don't have any guess work? I'm guess one of the properties in qtdesigner holds that information?

thanks man.

25th November 2011, 02:31
If you have already got a UI designed using Designer then you can use the promotion feature to convert your QLabels into ClickLabels. If you hard coded your UI then just create ClickLabels wherever you created QLabels before.

BTW: You should be using a layout to manage placement of your widgets, in which case there are no coordinates to take down because they are all generated by layout rules.