Results 1 to 11 of 11

Thread: How do I get QPainter on top of QLabel in my QWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How do I get QPainter on top of QLabel in my QWidget

    I have subclassed QWidget, created and instance of the class and placed 6 QLabels on it. In paintEvent() I use QPainter to do some painting. The result is that all the QLabels are shown on top of the QPainter-stuff. I have tried to manually call update() on my QLabels in paintEvent to make them update before I draw, but that was apparently not the right approach.

    How do I control wether my QLabels or my QPaintings are shown on top?

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

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    Can you show ur paintevent ?
    may be you need to call the base class paintevent first.. and then do your drawing..

  3. #3
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    At another computer right now, so I don't have the src. Basically I've implemented a paintEvent that just creates a QPainter and draws an image. I don't do anything about the QLabels there, so they are updated/painted outside of my control

    The designer shows:

    the_top_widget
    - my_subclassed_widget
    - qlabel1
    - qlabel2
    <and so on>

    Which paintEvent did you mean? I see the smiley, but don't know enough Qt to know why you smiled, so I'll take the chance it wasn't a joke

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

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    Dont go by the smiley.. it was just a rolleyes.. wondering.

    I meant in your subclassed widget paintEvent, are you calling your base class paint event too ?

  5. #5
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    Nope. I have implemented my_subclassed_widget:aintEvent, which only does a bit of QPainting.

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

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    Try calling base class event before ur drawing...
    if that doesnt work, try calling raise on ur subclassed widget..

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

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    if they are just qlables, they must just be showing texts or pics (hopefully). And you have them as child to your Widget which is being painted on. Basically this is not a good approach as you can just paint the qlabel stuff (pic or text) directly onto the widget. That would be better solution. Did i understood your problem correctly?

  8. #8
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    You understand correctly. The QLabels show images. Why is having them as children to my widget not a good approach?
    Do you mean using QPainter::drawImage instead of QLabels? I did that before, but it seemed to cost more cycles.

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

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    Quote Originally Posted by anr78 View Post
    Why is having them as children to my widget not a good approach?
    short answer, because you are having problems with this solution.
    long answer, you widget is made heavy with child widgets and layout, without any benefit.

    Do you mean using QPainter::drawImage instead of QLabels? I did that before, but it seemed to cost more cycles.
    define "it seemed"!!.
    QLabel also uses painter->draw() functions to draw its own images. The total cost of your widget would be more coz it requires more paintevent calls ( widget + labels)
    Go ahead and draw everything in your paintevent.

  10. #10
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    Which I did before reading your reply, and it worked great. Thanks for the tutoring

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

    Default Re: How do I get QPainter on top of QLabel in my QWidget

    Quote Originally Posted by anr78 View Post
    The QLabels show images. Why is having them as children to my widget not a good approach?
    Let's try to provide a different answer. Children of a widget are always on top of the widget and since widgets are painted using the "painter's algorithm" the child widgets are always painted after their parent. Hence you can't do anything to make them paint before the parent using the regular event processing flow.

    By the way, instead of QPainter::drawImage() use QPainter::drawPixmap() storing your images as pixmaps (QPixmap) and not images (QImage), you'll see the performance is better.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. how to get QPainter object for QLabel
    By bpatel in forum Newbie
    Replies: 7
    Last Post: 28th May 2010, 10:30
  2. print a QLabel over a QWidget
    By franco.amato in forum Newbie
    Replies: 2
    Last Post: 18th March 2010, 04:42
  3. layout QLabel and QWidget
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2010, 11:53
  4. Prevent a QLabel resizing the parent QWidget
    By danc81 in forum Qt Programming
    Replies: 6
    Last Post: 10th November 2009, 20:54
  5. Replies: 1
    Last Post: 29th September 2009, 19:44

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.