Results 1 to 5 of 5

Thread: mousePressEvent in QLabel

  1. #1
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default mousePressEvent in QLabel

    Hello,

    I'm currently working on a class with QLabel as parent
    Qt Code:
    1. class myclass : public QLabel
    2. {
    3. Q_OBJECT
    4. ......
    5. protected:
    6. void mousePressEvent(QMouseEvent* event);
    7. ......
    To copy to clipboard, switch view to plain text mode 

    with just this as overridden mousepressevent:
    Qt Code:
    1. void myclass::mousePressEvent(QMouseEvent* event){
    2. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mousePressEvent in QLabel

    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

  3. #3
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mousePressEvent in QLabel

    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....

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mousePressEvent in QLabel

    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..

  5. #5
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mousePressEvent in QLabel

    [EDIT]
    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)
    [/EDIT]


    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:
    Qt Code:
    1. .
    2. .
    3. .
    4. class CXRayDisplay : public QLabel
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. CXRayDisplay(QLabel* parent, QImage* XRayImage, ....and several other vars...);
    10. .
    11. .
    12. .
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. .
    2. .
    3. .
    4. //XRayDisplay part start
    5. //Initializing display
    6. QImage* test;
    7. test = new QImage("Resources\\xray.bmp");
    8. m_display2 = new CXRayDisplay(ui.xrayDisplay,test,-300,-300,-600,600);
    9.  
    10. //and show it
    11. ui.xrayDisplay->setPixmap(m_display2->getPixmap());
    12. .
    13. .
    14. .
    15. .
    To copy to clipboard, switch view to plain text mode 

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

    [EDIT]
    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:
    Qt Code:
    1. CXRayDisplay::CXRayDisplay(QLabel *parent, QImage* XRayImage, ..several other variables...)
    2. : QLabel(parent)
    To copy to clipboard, switch view to plain text mode 
    [/EDIT]
    Last edited by Sparx; 23rd June 2010 at 10:49.

Similar Threads

  1. mousePressEvent problem
    By jhowland in forum Qt Programming
    Replies: 9
    Last Post: 18th April 2010, 23:53
  2. Replies: 1
    Last Post: 29th September 2009, 19:44
  3. Change QLabel to QLineEdit when mousePressEvent happens
    By naoyamakino in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2009, 18:37
  4. Replies: 1
    Last Post: 2nd August 2008, 15:46
  5. mousePressEvent
    By mickey in forum Newbie
    Replies: 3
    Last Post: 21st March 2006, 15:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.