Can you post your exact code you use.
Can you post your exact code you use.
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
...and the error you get is???
I guess because qWarning() is not defined. In your header include and it should work.
Maluko_Da_Tola (27th August 2010), sparticus_37 (8th September 2010)
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
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)
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
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)
Thank you very much! It finally works!
Now that your query has been answered...why exactly are you using clickable labels versus other, more obviously user-interactive widgets?
Bookmarks