Results 1 to 17 of 17

Thread: Painting: How to control what's in the foreground?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Painting: How to control what's in the foreground?

    I am writing a program that displays several QWidgets on the screen, within one parent QWidget. Then I have a QLabel that I draw with the drawText() method from QPainter. There are als Buttons to move around this Label on the screen (up, down, left, right).
    Like so:

    Qt Code:
    1. void MyClass::paintEvent(QPaintEvent*)
    2. {
    3. QPainter painter(this);
    4. painter.setBrush(Qt::red);
    5. QPen myPen;
    6. myPen.setWidth(1);
    7. myPen.setColor(foreground);
    8. painter.setPen(myPen);
    9. painter.drawText(xpos, ypos, textDisplay->text());
    10. }
    To copy to clipboard, switch view to plain text mode 



    Now, the QWidgets are always shown in the foreground, and the QLabel is shown in the background, behind the QWidgets. How can I change this order? I want the QLabel to be in the foreground.
    Common sense tells you that whichever Object gets to be painted last should be in the foreground, since it is drawn "on top" of the other ones. How does one achieve this?

  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: Painting: How to control what's in the foreground?

    One can change the stacking order of children with QWidget::raise() and QWidget::lower().
    J-P Nurmi

  3. #3
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting: How to control what's in the foreground?

    Sounds good, but it does not seem to do the trick. I have added this method call to paintEvent():

    Qt Code:
    1. textDisplay->raise();
    To copy to clipboard, switch view to plain text mode 


    But it still gets painted below the other widgets, not on top. See this Screenshot: http://img156.imageshack.us/img156/3...picturedf4.png

  4. #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: Painting: How to control what's in the foreground?

    Quote Originally Posted by Mister_Crac View Post
    Sounds good, but it does not seem to do the trick.
    I have added this method call to paintEvent():

    Qt Code:
    1. textDisplay->raise();
    To copy to clipboard, switch view to plain text mode 
    You don't want to do that during every single paint event. It would only cause unnecessary overhead.

    But it still gets painted below the other widgets, not on top. See this Screenshot: http://img156.imageshack.us/img156/3...picturedf4.png
    Children are always on top of their parent. From the attached screenshot it's impossible to see which widget is a children of which parent. So what are MyClass and textDisplay? You are raising textDisplay but MyClass actually draws the text?
    J-P Nurmi

  5. #5
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting: How to control what's in the foreground?

    Quote Originally Posted by jpn View Post
    You don't want to do that during every single paint event. It would only cause unnecessary overhead.
    Okay, but where do I call raise()? Within the constructor?

    Children are always on top of their parent. From the attached screenshot it's impossible to see which widget is a children of which parent. So what are MyClass and textDisplay? You are raising textDisplay but MyClass actually draws the text?
    The QLabel textDisplay is a member of MyClass. In the constructor of MyClass, I create the widgets that are shown as black in the screenshot (Bus, Prozessor etc.) and arrange them in a layout. So that makes them child widgets - right? The textDisplay is not integrated into the layout.
    The one widget that is supposed to move around is textDisplay, so it is the only one that gets drawn in the paintEvent() method.

  6. #6
    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: Painting: How to control what's in the foreground?

    Quote Originally Posted by Mister_Crac View Post
    Okay, but where do I call raise()? Within the constructor?
    Yeah, it's enough change the stacking order once. Somewhere near creating the widgets is fine.

    The QLabel textDisplay is a member of MyClass. In the constructor of MyClass, I create the widgets that are shown as black in the screenshot (Bus, Prozessor etc.) and arrange them in a layout. So that makes them child widgets - right? The textDisplay is not integrated into the layout.
    The one widget that is supposed to move around is textDisplay, so it is the only one that gets drawn in the paintEvent() method.
    So if MyClass is the parent, anything it draws (the red text) appears below it's children. Each widget is responsible for drawing itself when it receives a paint event. You don't paint other widgets, like children in the paintEvent() of the parent. Where is the textDisplay widget in the screenshot? Maybe you have to call QWidget::adjustSize() or something to make it adjust it's size to correspond it's contents (this would be done automatically if it was in a layout).
    J-P Nurmi

  7. #7
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting: How to control what's in the foreground?

    Quote Originally Posted by jpn View Post
    Where is the textDisplay widget in the screenshot?
    It's the big red letters :-)

    Maybe you have to call QWidget::adjustSize() or something to make it adjust it's size to correspond it's contents (this would be done automatically if it was in a layout).
    I somehow fear that moving around a widget that is part of a layout will break the layout, no? I will try it out now.

  8. #8
    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: Painting: How to control what's in the foreground?

    Quote Originally Posted by Mister_Crac View Post
    It's the big red letters :-)
    Are you sure? So where is the red text drawn in MyClass::paintEvent()?

    I somehow fear that moving around a widget that is part of a layout will break the layout, no? I will try it out now.
    No, you can't move a widget managed by a layout.
    J-P Nurmi

  9. #9
    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: Painting: How to control what's in the foreground?

    Could you please do somewhere in MyClass after creating all the labels and other child widgets:
    Qt Code:
    1. dumpObjectTree();
    To copy to clipboard, switch view to plain text mode 
    and paste the output?
    J-P Nurmi

  10. #10
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting: How to control what's in the foreground?

    Okay I'm not sure if I came across clearly. I have made another screenshot and this time, I have changed all the German words to English for better understanding. Also, the first time around I did not show all of the windows that my application so far has. Here is the new and complete screenshot:

    http://img502.imageshack.us/img502/4...icture2nf4.png

    As you can see, the red text could really be anything. Anything you type in. It could also be another color if you want to. In any case, I definitely need it to be freely movable. Up, down, left, right, you name it. So it cannot be part of a layout, as it seems. All the other widgets - Memory, CPU, I/O Unit, Bus, Arrows - however can and in fact, are.

    Now, the problem remains: How can I achieve it so that the QLabel gets drawn on top of the other widgets? Calling raise() did not lead to success so far.

    Also,
    Quote Originally Posted by jpn View Post
    Could you please do somewhere in MyClass after creating all the labels and other child widgets:
    Qt Code:
    1. dumpObjectTree();
    To copy to clipboard, switch view to plain text mode 
    and paste the output?
    Would the output of that command happen to be found in the directory which is named "debug"? Then I fear that my Qt library is maybe not compiled for debugging, because that directory is empty always.

  11. #11
    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: Painting: How to control what's in the foreground?

    Quote Originally Posted by Mister_Crac View Post
    Would the output of that command happen to be found in the directory which is named "debug"? Then I fear that my Qt library is maybe not compiled for debugging, because that directory is empty always.
    No, I was referring to the debug output. IDEs tend to show the debug output. If you're not using any IDE, on Windows you will have to add
    CONFIG += console
    in the .pro file, regenerate makefile and rebuild the application. An external console is launched together with the application from which you can see the debug output. Alternatively, you can use such utilities as DebugView to monitor the debug output.
    J-P Nurmi

Similar Threads

  1. Replies: 3
    Last Post: 7th October 2015, 19:43
  2. Version control - what to use?
    By TheKedge in forum General Programming
    Replies: 11
    Last Post: 4th March 2007, 08:49
  3. How can i import a User Control Class .DLL
    By mmg123 in forum Qt Tools
    Replies: 2
    Last Post: 6th October 2006, 01:27
  4. QTextEdit control
    By vijay anandh in forum Qt Programming
    Replies: 2
    Last Post: 31st May 2006, 13:14
  5. Replies: 6
    Last Post: 3rd February 2006, 09:48

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.