PDA

View Full Version : mousePressEvent in QLabel



Sparx
16th June 2010, 14:00
Hello,

I'm currently working on a class with QLabel as parent


class myclass : public QLabel
{
Q_OBJECT
......
protected:
void mousePressEvent(QMouseEvent* event);
......


with just this as overridden mousepressevent:


void myclass::mousePressEvent(QMouseEvent* event){
}


While debugging, I set a break point on the first line of this function definition. It seems to work but not as expected: It only stops, if my mouse was pressed in the upper left corner of the label(lets say 3 pixels in width 8 pixels in height). I already tried adding text to the label, a pixmap changing different attribubtes that sound like they could have to deal with labelsize... no way...
What am I doing wrong?
Thanks in advance, this drives me crazy.

SneakyPeterson
17th June 2010, 14:43
Allow me to respond to your questions with a few questions of my own.

1: Does your label have any children?
If your label has children that have their own implementations of mousePressEvent(), then the event will be
handled by the children instead and will only be passed up if the children choose explicitly to use ignore(), which
most implementations of widgets don't by default.

2. Are you trying to use an empty implementation of mouse click, or is there more code you're hiding from us!?!
If you're hoarding your implementation of mousePressEvent() we won't be able to see if you're doing something that
would murder it. If you have an empty implementation of it, then ermmm, it SHOULDN'T do anything, and thus the
behavior you're seeing is expected.

Hope that helps,
sp

Sparx
22nd June 2010, 09:48
at number 1:
I created a Label with the ui designer and give it the size of 500X500. I overload the mousePressEvent().Then by runtime, I tell it to have a loaded Pixmap as Pixmap instead of nothing: so that pixmap is shown.

at 2:
It is empty, allready put some debug variables into the function like putting the height and width of the lebal into the variable temp, just to check if that is allright. As I said: 3X8 pixel is about the box (left top corner) in what the PressEvent is called and then I see my debug variables.... I don't think that is an expected behaviour....

aamer4yu
22nd June 2010, 10:13
You created the label in designer... how did you assign it to your class ??
class myclass : public QLabel ?

I guess you created one label in designer, and another through code..

Sparx
23rd June 2010, 10:28
You are definitely right, I seem to create another instance of a label. Somehow Its on the same position as the other label.. with exact geometry 4X12

How do I asign an Existing instance of a QLabel in such way, that no new instance of QLabel is created upon creation of my own class which is derived from QLabel (or other way round, sry I'm not sure about the right term)



first thing: the width and height of the object assigned as parent is 500X500 So crazy thing that mousePressEvent is only called in such a small area.
As far as I know I do not create the label through code.. here my code:
Definitin in header file:


.
.
.
class CXRayDisplay : public QLabel
{
Q_OBJECT

public:
CXRayDisplay(QLabel* parent, QImage* XRayImage, ....and several other vars...);
.
.
.

Here how a new instance of my class is generated and the label assigned to it and I assign a Pixmap which is retrieved from my class back to the label:


.
.
.
//XRayDisplay part start
//Initializing display
QImage* test;
test = new QImage("Resources\\xray.bmp");
m_display2 = new CXRayDisplay(ui.xrayDisplay,test,-300,-300,-600,600);

//and show it
ui.xrayDisplay->setPixmap(m_display2->getPixmap());
.
.
.
.


by the way: ui.xrayDisplay is the Label I created with ui Designer


I just see: maybe you are right about the creation of a new Label... I am not too good with derived attributes so someone can help me?

Well this is the deifintion of the source file of my class:


CXRayDisplay::CXRayDisplay(QLabel *parent, QImage* XRayImage, ..several other variables...)
: QLabel(parent)