PDA

View Full Version : Problem with QLabel & mouseEvent



harakiri
26th May 2007, 11:40
hi,

i've got a problem with a QLabel. I want to receive a mousePressEvent only inside the label.


void MainWindow::mousePressEvent(QMouseEvent* event) {
...
}



works fine, but


void imageLabel::mousePressEvent(QMouseEvent* event) {
..
}



doesn't work and the compiler tells me that imageLabel was not declared. But i declared it with


imageLabel = new QLabel;


and put


QLabel* imageLabel;


in the public section of the header.

imageLabel should be able to receive mouseEvents, so am i using mousePressEvent in the wrong way or is there anything else i misunderstood :confused:

wysota
26th May 2007, 11:49
You can only reimplement methods of classes and not objects and "imageLabel" is an object, not a class. You have to subclass QLabel and reimplement the method for the subclass.

harakiri
26th May 2007, 12:02
You can only reimplement methods of classes and not objects and "imageLabel" is an object, not a class. You have to subclass QLabel and reimplement the method for the subclass.

and how would i do that?

wysota
26th May 2007, 12:16
The same as you did with the main window. Do you know what subclassing (inheritance) means?

harakiri
26th May 2007, 13:17
so something like QLabel::imageLabel(){...} would help to make void imageLabel::mousePressEvent(QMouseEvent* event) {...} work?

my knowledge of classes and stuff isn't that good :rolleyes:, so any help in this case is quite useful for me

Gopala Krishna
26th May 2007, 13:54
so something like QLabel::imageLabel(){...} would help to make void imageLabel::mousePressEvent(QMouseEvent* event) {...} work?

my knowledge of classes and stuff isn't that good :rolleyes:, so any help in this case is quite useful for me

Here is a quick one if this the only part you need.You have to define you own class inheriting QLabel like

class MyLabel : public QLabel
{
Q_OBJECT
public:
protected:
void mousePressEvent(QMouseEvent *e);
public slots:
};

void Mylabel::mousePressEvent(QMouseEvent *e)
{
//your code
}


and finally declare MyLabel *label instead of QLabel* label.

Please understand that qt is entirely an object oriented library. If you don't know classes and other OOPS concepts, it becomes very difficult for one to understand what his program is doing. There are lots of c++ books and its better you familarise atleast the basics of c++.