Results 1 to 17 of 17

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

  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

  12. #12
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    I dont know whether I understood your problem correctly. When you move QLabel, do you want it to be seen at top of other widgets. i.e whenever QLabel has a mouse on it, QLabel should be seen at top of other widgets. If that is what you are looking for then have a look at Examples->QWidgets->ToolTips. Might help.

    Thanks

  13. #13
    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
    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.
    I have done this and indeed, I do get an additional black console window now, but no text ever appears inside of it. Even if I call dumpObjectTree(). Are you sure I do not need to re-compile Qt?
    Last edited by Mister_Crac; 4th May 2007 at 16:51.

  14. #14
    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 vermarajeev View Post
    I dont know whether I understood your problem correctly. When you move QLabel, do you want it to be seen at top of other widgets.
    I want this QLabel to be in front of the other QWidgets all of the time.

    i.e whenever QLabel has a mouse on it, QLabel should be seen at top of other widgets. If that is what you are looking for then have a look at Examples->QWidgets->ToolTips. Might help.
    I think my problem boils down to this: If you have two QWidgets that overlap, how do you control which one of the two is in the foreground? This seems to be complicated by the fact that the one QWidget (the QLabel which must be freely movable) is not part of a layout, while the other QWidgets (which are supposed to be visible, but "behind" the moving QLabel) are.

  15. #15
    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
    I have done this and indeed, I do get an additional black console window now, but no text ever appears inside of it. Even if I call dumpObjectTree(). Are you sure I do not need to re-compile Qt?
    No, re-compiling Qt is not necessary. Sorry, I have no idea what's the problem with no text appearing, though. Did you clean and rebuild the whole project?

    Quote Originally Posted by Mister_Crac View Post
    I think my problem boils down to this: If you have two QWidgets that overlap, how do you control which one of the two is in the foreground?
    It's all about parenting widgets correctly. The order of sibling child widgets can be changed with raise() and lower() as mentioned in the beginning of the thread.

    Edit: This is why I asked for dumpObjectTree() output. It will show the actual hierarchy of the object tree.
    J-P Nurmi

  16. #16
    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
    No, re-compiling Qt is not necessary. Sorry, I have no idea what's the problem with no text appearing, though. Did you clean and rebuild the whole project?
    Yes, I deleted the "release" directory and compiled it from scratch. I don't use a fancy IDE, just Vim 7.0 (gvim) as the editor and compiling at the console. Old school!
    Maybe I could redirect the stderror output to a file?

    EDIT:
    I just realised that I made a mistake. For the QLabel, instead of doing this:
    Qt Code:
    1. textDisplay = new QLabel("Enter Text here", this);
    To copy to clipboard, switch view to plain text mode 
    I had left out the "this", thus making the QLabel not a child Widget of the application window.
    But still, even now that I have changed it, calling raise() seems to do nothing at all. Of course, there is always the possibility that I am making another stupid mistake...
    Last edited by Mister_Crac; 4th May 2007 at 18:04.

  17. #17
    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, so I finally managed to call dumpObjectTree() and get an output. Turns out that indeed my Qt was not compiled for debugging, but now it is. Here is what I get, I simplified it because the output was so long. Look at this screenshot to see how it looks now - and still my QLabel is in the background. :-(

    Qt Code:
    1. Rechner::
    2. QTextDocumentLayout::
    3. QTextImageHandler::
    4. QTextDocumentLayout::
    5. QTextImageHandler::
    6. QTextDocumentLayout::
    7. QTextImageHandler::
    8. QTextDocumentLayout::
    9. QTextImageHandler::
    10. QTextDocumentLayout::
    11. QTextImageHandler::
    12. QTextDocumentLayout::
    13. QTextImageHandler::
    14. QTextDocumentLayout::
    15. QTextImageHandler::
    16. QTextDocumentLayout::
    17. QTextImageHandler::
    To copy to clipboard, switch view to plain text mode 
    Last edited by Mister_Crac; 8th May 2007 at 13:12.

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.