Results 1 to 19 of 19

Thread: drawing over QLabel

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default drawing over QLabel

    Hi.
    I'd like to know how to draw a selection rectangle over a label (QLabel) so that the QLabel continues to show. I think I need to somehow call QLabel::PaintEvent, but I don't know how.
    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: drawing over QLabel

    How about QRubberBand?
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    tommy (16th November 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawing over QLabel

    Thanks for the idea. I looked up QRubberBand but can't get it to work (getting "origin" and "m_pixmapLabel" not defined errors). I am misunderstanding something. I put my code below. Thanks!

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include "definitions.h"
    4.  
    5. int origin =0;
    6.  
    7. MyWidget::MyWidget(QWidget* parent): QWidget(parent)
    8. {
    9. m_pixmapLabel = new QLabel;
    10. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    11. QVBoxLayout* mainLayout = new QVBoxLayout;
    12. QPixmap pm;
    13. pm.load("image.bmp");
    14. m_pixmapLabel->setPixmap(pm);
    15. mainLayout->addWidget(m_pixmapLabel);
    16. setLayout(mainLayout);
    17.  
    18. }
    19.  
    20. void MyWidget::mouseMoveEvent(QMouseEvent *event)
    21. {
    22. rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
    23. }
    24.  
    25. void MyWidget::mousePressEvent(QMouseEvent *event)
    26. {
    27. origin = event->pos();
    28. rubberBand->setGeometry(QRect(origin, QSize()));
    29. rubberBand->show();
    30. }
    31.  
    32. void MyWidget::mouseReleaseEvent(QMouseEvent *event)
    33. {
    34. rubberBand->hide();
    35. }
    36.  
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QApplication application(argc, argv);
    41. MyWidget window;
    42. window.show();
    43. return application.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: drawing over QLabel

    Quote Originally Posted by tommy View Post
    I looked up QRubberBand but can't get it to work (getting "origin" and "m_pixmapLabel" not defined errors). I am misunderstanding something.
    Well, the documentation expects you to know basics of C++. You must define them as member variables:
    Qt Code:
    1. class MyWidget
    2. {
    3. ...
    4. private:
    5. QPoint origin;
    6. QRubberBand* rubberBand;
    7. };
    To copy to clipboard, switch view to plain text mode 
    PS. I presumed you meant "rubberBand" with "m_pixmapLabel" since QRubberBand docs talk nothing about "m_pixmapLabel".
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    tommy (16th November 2007)

  7. #5
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawing over QLabel

    Thanks again jpn. I had forgotten QPoint and I made the change. QRubberBand was declared correctly all along. Nevertheless the code won't work (compains about origin and mouseReleaseEvent). I pasted the current code again - in case you have a minute. Thanks a lot.

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include "tom.h"
    4.  
    5.  
    6. MyWidget::MyWidget(QWidget* parent): QWidget(parent)
    7. {
    8. m_pixmapLabel = new QLabel;
    9. origin = new QPoint;
    10. origin.setX(0);
    11. origin.setY(0);
    12. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    13. QVBoxLayout* mainLayout = new QVBoxLayout;
    14. QPixmap pm;
    15. pm.load("image.bmp");
    16. m_pixmapLabel->setPixmap(pm);
    17. mainLayout->addWidget(m_pixmapLabel);
    18. setLayout(mainLayout);
    19. }
    20.  
    21. void MyWidget::mouseMoveEvent(QMouseEvent *event)
    22. {
    23. rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
    24. }
    25.  
    26. void MyWidget::mousePressEvent(QMouseEvent *event)
    27. {
    28. origin = event->pos();
    29. rubberBand->setGeometry(QRect(origin, QSize()));
    30. rubberBand->show();
    31. }
    32.  
    33. void MyWidget::mouseReleaseEvent(QMouseEvent *event)
    34. {
    35. rubberBand->hide();
    36. }
    37.  
    38.  
    39. int main(int argc, char *argv[])
    40. {
    41. QApplication application(argc, argv);
    42. MyWidget window;
    43. window.show();
    44. return application.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef TOM_H
    2. #define TOM_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MyWidget : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. MyWidget (QWidget* parent = 0);
    11. protected:
    12. void mouseMoveEvent(QMouseEvent *event);
    13. void mousePressEvent(QMouseEvent *event);
    14. void mouseReleaseEvent(QMouseEvent *event)
    15. private:
    16. QLabel* m_pixmapLabel;
    17. QRubberBand* rubberBand;
    18. QPoint origin;
    19. };
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawing over QLabel

    missing a semicolon after mouseReleaseEvent in the header.....

  9. The following user says thank you to pherthyl for this useful post:

    tommy (16th November 2007)

  10. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: drawing over QLabel

    Quote Originally Posted by tommy View Post
    Nevertheless the code won't work (compains about origin and mouseReleaseEvent). I pasted the current code again - in case you have a minute.
    Always paste the error whenever you bump into such situation, please. Seeing the error message helps us a lot to tell where's the problem. We are not walking compilers...

    Quote Originally Posted by tommy View Post
    Qt Code:
    1. origin = new QPoint;
    To copy to clipboard, switch view to plain text mode 
    This is clearly wrong, "origin" was not declared as pointer. You can simply drop the line.

    Quote Originally Posted by tommy View Post
    Qt Code:
    1. origin.setX(0);
    2. origin.setY(0);
    To copy to clipboard, switch view to plain text mode 
    You can drop these as well. As you can see from the documentation, the default constructor of QPoint already initializes the point to (0,0).
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    tommy (16th November 2007)

  12. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: drawing over QLabel

    Quote Originally Posted by jpn View Post
    We are not walking compilers...
    Jacek is...

  13. The following user says thank you to wysota for this useful post:

    jpn (16th November 2007)

  14. #9
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawing over QLabel

    Hey now, what is this blatant nepotism... Admins handing thanks out to each other like it was candy..

  15. #10
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default jpn got it wrong

    Hi jpn,

    I think you got it wrong. I think you are a walking compiler - and more. You're better than a compiler because you can trouble-shoot so much better than a compiler . Anyway, thank you so much for being so helpful and patient with someone who doesn't yet know a whole lot about Qt or C++.
    If I may ask one last question here, that would be:
    What's the best way to get local coordinates out of the rubberband once the label which I rubberband (this is a verb as in "to rubberband something") becomes a part of my main layout?
    I intend to start using the rubberband for cropping.

    Thanks

  16. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: drawing over QLabel

    Quote Originally Posted by pherthyl View Post
    Hey now, what is this blatant nepotism... Admins handing thanks out to each other like it was candy..
    Well... J-P is not an admin And I'm really an AI - ask Kumosan, he'll tell ya...

  17. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: drawing over QLabel

    Quote Originally Posted by pherthyl View Post
    Hey now, what is this blatant nepotism... Admins handing thanks out to each other like it was candy..
    Indeed, I'm no admin. And I never thought I was spreading too many "thanks". Also, believe or not, that really cheered me up!

    Quote Originally Posted by tommy View Post
    What's the best way to get local coordinates out of the rubberband once the label which I rubberband (this is a verb as in "to rubberband something") becomes a part of my main layout?
    Child widget's coordinates are always relative to its parent. You can use various QWidget::mapTo*() and QWidget::mapFrom*() methods to map the coordinates if needed.
    J-P Nurmi

  18. #13
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: jpn got it wrong

    Once you're done dragging the rubberband (in the mouseReleaseEvent for example) you can use rubberBand->geometry() to get the rectangle defining this rubber band (in the coordinates of the parent).

  19. #14
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawing over QLabel

    Quote Originally Posted by jpn View Post
    Indeed, I'm no admin. And I never thought I was spreading too many "thanks". Also, believe or not, that really cheered me up!
    I was just kidding. Hence the smiley. I thought it was funny too.

  20. #15
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawing over QLabel

    That's great, the rubberband works great for me now and I also know how to get the coordinates out, but the problem is that my picture that I display (as part of the label) is a bit smaller than the label; also, the label does not extend all the way to the edges of the main layout. As a result of that the upper left coordinate of my picture is about (10,10) as opposed to the desired (0,0). And worse yet: as I expand and shrink the window, my label swims around in it and the coordinates change all the time. How'm I ever going to use this approach for cropping of my pixmap image.

  21. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: drawing over QLabel

    Map the coordinates of the rubberband to the label geometry using QWidget::mapTo.

  22. The following user says thank you to wysota for this useful post:

    tommy (17th November 2007)

  23. #17
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawing over QLabel

    ....so which way does the mapTo() work?

    If this is the syntax:

    myPoint = mapTo(my_QLabel_name, QPoint(0,0));

    is the myPoint then going to hold the absolute coordinates (two positive numbers) of the upper left of the label named my_QLabel_name?
    What function then separates myPoint (a QPoint) into two numbers : x and y?

  24. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: drawing over QLabel

    Quote Originally Posted by tommy View Post
    What function then separates myPoint (a QPoint) into two numbers : x and y?
    It's obvious once you take a look at QPoint docs, isn't it? Try to avoid asking every single question that comes to mind on the forums, please. You know, it doesn't make yourself look very good.. Docs are the number #1 resource after all!
    J-P Nurmi

  25. #19
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawing over QLabel

    Thanks for your help, I figured it all out now
    Last edited by tommy; 17th November 2007 at 20:20.

Similar Threads

  1. Problem with QLabel and setText
    By jambrek in forum Qt Programming
    Replies: 7
    Last Post: 31st October 2007, 16:02
  2. Drawing on QWidget - strech & resize
    By kemp in forum Qt Programming
    Replies: 5
    Last Post: 22nd January 2007, 14:39
  3. QLabel links?
    By gfunk in forum Qt Programming
    Replies: 3
    Last Post: 23rd December 2006, 00:42
  4. QScrollArea display custom QLabel
    By spawnwj in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2006, 03:38
  5. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 19: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.