Re: Handling mouse events
Typo Qt::LeftButton? Following works fine for me:
Code:
#include <QtGui>
{
Q_OBJECT
public:
{}
protected:
{
if (event->button()==Qt::LeftButton)
{
qWarning() << Q_FUNC_INFO;
}
}
};
int main(int argc, char *argv[])
{
MyLabel l;
l.setText("foo bar");
l.show();
return app.exec();
}
#include "main.moc"
Re: Handling mouse events
Thank you very much! I just not sure about #include "main.moc". What does it mean?
Re: Handling mouse events
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.
Re: Handling mouse events
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
Re: Handling mouse events
Can you post your exact code you use.
Re: Handling mouse events
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
Re: Handling mouse events
...and the error you get is???
I guess because qWarning() is not defined. In your header include
Code:
#include <QtGlobal>
and it should work.
Re: Handling mouse events
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
Re: Handling mouse events
Clean your project, run qmake and rebuild everything.
That error means that a moc file didn't get created.
Re: Handling mouse events
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
Re: Handling mouse events
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.
Re: Handling mouse events
Thank you very much! It finally works!
Re: Handling mouse events
Now that your query has been answered...why exactly are you using clickable labels versus other, more obviously user-interactive widgets?