Results 1 to 6 of 6

Thread: Problem with QLabel & mouseEvent

  1. #1
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with QLabel & mouseEvent

    hi,

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

    Qt Code:
    1. void MainWindow::mousePressEvent(QMouseEvent* event) {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 


    works fine, but

    Qt Code:
    1. void imageLabel::mousePressEvent(QMouseEvent* event) {
    2. ..
    3. }
    To copy to clipboard, switch view to plain text mode 


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

    Qt Code:
    1. imageLabel = new QLabel;
    To copy to clipboard, switch view to plain text mode 


    and put

    Qt Code:
    1. QLabel* imageLabel;
    To copy to clipboard, switch view to plain text mode 


    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
    Last edited by wysota; 26th May 2007 at 11:50. Reason: changed tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QLabel & mouseEvent

    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.

  3. #3
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QLabel & mouseEvent

    Quote Originally Posted by wysota View Post
    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QLabel & mouseEvent

    The same as you did with the main window. Do you know what subclassing (inheritance) means?

  5. #5
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QLabel & mouseEvent

    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 , so any help in this case is quite useful for me

  6. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QLabel & mouseEvent

    Quote Originally Posted by harakiri View Post
    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 , 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
    Qt Code:
    1. class MyLabel : public QLabel
    2. {
    3. Q_OBJECT
    4. public:
    5. protected:
    6. void mousePressEvent(QMouseEvent *e);
    7. public slots:
    8. };
    9.  
    10. void Mylabel::mousePressEvent(QMouseEvent *e)
    11. {
    12. //your code
    13. }
    To copy to clipboard, switch view to plain text mode 

    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++.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. The following user says thank you to Gopala Krishna for this useful post:

    harakiri (26th May 2007)

Similar Threads

  1. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  2. Replies: 16
    Last Post: 7th March 2006, 15:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.