Results 1 to 2 of 2

Thread: QPainter on a QtWidget?

  1. #1
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QPainter on a QtWidget?

    I'm trying to paint on a QtWidget. In this case a slider.

    I created my widget with a paint event:

    Qt Code:
    1. class MySlider(QtWidgets.QSlider):
    2. def __init__(self, parent=None):
    3. super(MySlider, self).__init__(parent)
    4.  
    5. def paintEvent(self, QPaintEvent):
    6. qp = QPainter(self)
    7.  
    8. qp.setPen(Qt.black)
    9. size = self.size()
    10. qp.drawLine(10,10,60,10)
    To copy to clipboard, switch view to plain text mode 

    And the line does show up, but the normal slider graphics (the knob and line) don't show up.

    I assume I'm missing how to tell the slider widget to draw before or after my code is done. Or, am I totally off base (not unlikely for me... )

    Mac


    Added after 5 minutes:


    I got it working by adding a call to super for the paint event, but is this the RIGHT way?:

    Qt Code:
    1. class MySlider(QtWidgets.QSlider):
    2. def __init__(self, parent=None):
    3. super(MySlider, self).__init__(parent)
    4.  
    5. def paintEvent(self, event):
    6. super(MySlider, self).paintEvent(event)
    7. qp = QPainter(self)
    8.  
    9. qp.setPen(Qt.black)
    10. size = self.size()
    11. qp.drawLine(10,10,60,10)
    To copy to clipboard, switch view to plain text mode 
    Last edited by drmacro; 3rd March 2017 at 16:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPainter on a QtWidget?

    If you want the base class to paint something, then calling the base class implementation is the right thing to do.

    Cheers,
    _

Similar Threads

  1. how to use macros in Qtwidget
    By iswaryasenthilkumar in forum Newbie
    Replies: 5
    Last Post: 16th February 2015, 20:03
  2. Embedding MacOS application in QtWidget
    By brainTech in forum Newbie
    Replies: 0
    Last Post: 3rd January 2013, 05:46
  3. Replies: 5
    Last Post: 4th May 2011, 14:37
  4. igstk::QTWidget display problem.
    By Ashwath in forum Newbie
    Replies: 0
    Last Post: 4th March 2011, 07:51
  5. QtWidget in Overlay Planes on Windows OpenGL
    By IVTdeveloper in forum Qt Programming
    Replies: 2
    Last Post: 20th August 2008, 11:00

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.