Results 1 to 6 of 6

Thread: overloading paintEvent() function

  1. #1
    Join Date
    Jul 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default overloading paintEvent() function

    Hello All!!

    I was wondering if there is any way we could overload the paintEvent function with more parameters so that I could pass on other informations to the painEvent. And if yes, I guess there should be some kind of way to explicitly call the overloaded function as well.

    Actually, I want to draw on a particular QLabel inside my QMainWindow. I could've made a sub class of QLabel bt I have come a long way without doing so.... So, to do it with a subclass, I'd have to start over and it is a lot of work and I dont have much time til the deadline.

    So, can you please help me on this??

    Regards, Aayush Shrestha

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: overloading paintEvent() function

    BTW, what extra information you want to pass to paintEvent? If you have not implemented the paintEvent function, what is the use of sending other information, it will just be ignored by the standard QLabel paintEvent implementation, think again what you wan to do.

    Any way, one alternate for sub-classing QLabel, is to install a event filter on that particular instance on QLabel, and interrupt all the paint events, and do your custom functions instead.

    I hope you have explored all the options of using the standard QLabel as is

  3. #3
    Join Date
    Jul 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: overloading paintEvent() function

    Quote Originally Posted by Santosh Reddy View Post
    BTW, what extra information you want to pass to paintEvent? If you have not implemented the paintEvent function, what is the use of sending other information, it will just be ignored by the standard QLabel paintEvent implementation, think again what you wan to do.

    Any way, one alternate for sub-classing QLabel, is to install a event filter on that particular instance on QLabel, and interrupt all the paint events, and do your custom functions instead.

    I hope you have explored all the options of using the standard QLabel as is
    I have installed event filter onto my imageLabel (a QLabel). I use the event filter to detect mouse press, mouse release and mouse move. After detecting, I draw a line on the image that is currently on the Label by calculating mouse position. Now, I have to paint the modified image on the Label. My idea was to do so in the paintEvent function. Now, I have to send the modified image to the paintEvent function so that it could be painted over the label.

    What can I do for that??

    Please Help!!

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: overloading paintEvent() function

    just use QLabel::setPixmap(yourModifiedPixmap);

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: overloading paintEvent() function

    You can directly draw on the widget (QLabel in your case).

    I can give a an example, here I have QWidget (or any other widget), which I don't want to sub-class, or it is already being used heavily in design. Then I will create a event filer (say CustomPaint), and install it on objects which I want and do my custom painting instead of default widget painting.

    Qt Code:
    1. class CustomPaint : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. CustomPaint(QObject *parent) :
    6. QObject(parent)
    7. {
    8. }
    9.  
    10. protected:
    11. bool eventFilter(QObject * object, QEvent * event)
    12. {
    13. if(event->type() == QEvent::Paint)
    14. {
    15. QWidget * widget = dynamic_cast<QWidget *>(object);
    16. if(widget)
    17. {
    18. //[1]. Call this if you want to have original widget painting in background, else comment it out
    19. object->event(event); //Note: this will call QObject::event(), you cannot directly call QWidget::event()
    20.  
    21. //[2]. Sample paint, draws a rounded rectangle, as per original widget's size hint
    22. {
    23. QPainter painter(widget); //Note: widget is parent
    24.  
    25. int width = 8;
    26. QPen pen;
    27. pen.setWidth(width);
    28.  
    29. painter.setPen(pen);
    30. QRect rect = QRect(width/2, width/2, widget->sizeHint().width() - width, widget->sizeHint().height() - width);
    31.  
    32. painter.setRenderHint(QPainter::Antialiasing);
    33. painter.drawRoundedRect(rect, 20, 20);
    34. }
    35. //[3]. return false; if you want to have the original widget also to be painted
    36. return true; //filter the event, original widget will not paint
    37. }
    38. }
    39. return false;
    40. }
    41. };
    To copy to clipboard, switch view to plain text mode 

    Just install this on the widget you want

    Following can be use cases for this example
    1. If a widget does not have default paint, you can implement a paint, with out sub-classing
    2. If you want a widget's painting to be done on some background, then you can do it, with out sub-classing (three are simple ways to do so)
    3. If you want to paint extra content on a widgets content, with out sub-classing

    combinations of these, or any innovative way.

  6. #6
    Join Date
    Jul 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: overloading paintEvent() function

    Thanks...

Similar Threads

  1. Replies: 3
    Last Post: 27th August 2010, 06:00
  2. Replies: 1
    Last Post: 30th July 2010, 07:50
  3. Replies: 3
    Last Post: 18th November 2009, 11:18
  4. Strange thing after override paintEvent function
    By Sheng in forum Qt Programming
    Replies: 9
    Last Post: 20th February 2009, 16:44
  5. repainting through paintEvent() function
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 6th August 2008, 12:50

Tags for this Thread

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.