PDA

View Full Version : Handling mouse events



Maluko_Da_Tola
26th August 2010, 19:35
Hi,

I have been trying to get a QLabel widget to handle mouse left click events. The label is in a layout, alongside other widgets. My strategy was to subclass QLabel and create a MyLabel widget, which contains the virtual methods for handling mouseEvents. However, when I run the application, the label does not handle any click events. On the header file of MyLabel I put:

protected:
virtual void mousePressEvent(QMouseEvent *event);


On the MyLabel.cpp file I wrote the following definition for the mousePressEvent:

void MyLabel::mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::leftButton){

//code for the mouse click event
}
}

What is missing in this implementation? How could I make the label respond to mouse clicks and execute the code for the mouse click event?

Thank you

Lykurg
26th August 2010, 20:57
Typo Qt::LeftButton? Following works fine for me:
#include <QtGui>

class MyLabel : public QLabel
{
Q_OBJECT

public:
MyLabel(QWidget *parent = 0) : QLabel(parent)
{}

protected:
virtual void mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::LeftButton)
{
qWarning() << Q_FUNC_INFO;
}
}
};

int main(int argc, char *argv[])
{

QApplication app(argc, argv);

MyLabel l;
l.setText("foo bar");
l.show();

return app.exec();
}

#include "main.moc"

Maluko_Da_Tola
26th August 2010, 21:59
Thank you very much! I just not sure about #include "main.moc". What does it mean?

Lykurg
26th August 2010, 22:58
You don't need that normally when you put one class in one file, but since I put all in one file I must tell qmake explicit that it calls moc on that file. moc is needed to get all QObject features to work.

Maluko_Da_Tola
26th August 2010, 23:57
Thank you! The only thing I don't understand is that when I try to define MyLabel in a separate header file, it wont compile! Do you know why is this happening?

Cheers

Lykurg
27th August 2010, 00:37
Can you post your exact code you use.

Maluko_Da_Tola
27th August 2010, 00:58
Sure. I created a myLabel.h header file that contains the following:

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class MyLabel : public QLabel
{
Q_OBJECT
public:

protected:
virtual void mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::LeftButton)
{
qWarning() << Q_FUNC_INFO;
}
}
};

#endif // MYLABEL_H

And in the main.cpp I wrote:

#include <QtGui>
#include "myLabel.h"


int main(int argc, char *argv[])
{

QApplication app(argc, argv);


MyLabel l;
l.setText("foo bar");
l.show();

return app.exec();
}


Cheers

Lykurg
27th August 2010, 06:20
...and the error you get is???

I guess because qWarning() is not defined. In your header include
#include <QtGlobal>and it should work.

Maluko_Da_Tola
27th August 2010, 13:06
It still does not work, even after the inclusion of #include <QtGlobal> in my header file. The error I get is:


/home/maluko/Documents/Qt creator/MouseEvents3/main.cpp:0: Warning: No relevant classes found. No output generated.
/home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:7: undefined reference to `vtable for MyLabel'
/home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:7: undefined reference to `vtable for MyLabel'
/home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:7: undefined reference to `vtable for MyLabel'
/home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:7: undefined reference to `vtable for MyLabel'
:-1: error: collect2: ld returned 1 exit status

tbscope
27th August 2010, 13:13
Clean your project, run qmake and rebuild everything.
That error means that a moc file didn't get created.

Maluko_Da_Tola
27th August 2010, 13:51
I just did that but now I get the following errors:

/home/maluko/Documents/Qt creator/MouseEvents3/moc_myLabel.cpp:10: In file included from moc_myLabel.cpp:10:
/home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:14: error: invalid use of incomplete type ‘struct QMouseEvent’
/usr/include/qt4/QtGui/qwidget.h:76: error: forward declaration of ‘struct QMouseEvent’
/home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:16: error: invalid use of incomplete type ‘struct QDebug’
/usr/include/qt4/QtCore/qglobal.h:1636: error: forward declaration of ‘struct QDebug’
/usr/include/qt4/QtCore/qglobal.h:1640: warning: inline function ‘QDebug qWarning()’ used but never defined

tbscope
27th August 2010, 14:02
These errors mean that some include files were not added to your mylabel.h file.

Specifically, the include files for the classes QMouseEvent and QDebug.
Add these include files to mylabel.h and the errors will go away.

Maluko_Da_Tola
27th August 2010, 14:45
Thank you very much! It finally works!

Urthas
28th August 2010, 00:37
Now that your query has been answered...why exactly are you using clickable labels versus other, more obviously user-interactive widgets?