Results 1 to 6 of 6

Thread: fillRect does not display anything?

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default fillRect does not display anything?

    Ok, I'm missing something simple here. For some reason, QPainter isn't displaying anything. I thought this code should simply fill the background of the scene with blue. Any ideas? This is the entirety of my code:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5. view.setScene(&scene);
    6.  
    7. QPainter painter;
    8.  
    9. QRectF sceneRect = scene.sceneRect();
    10. painter.fillRect(sceneRect, Qt::blue);
    11.  
    12. view.show();
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: fillRect does not display anything?

    You should use QPainter in PaintEvent only.
    Here you can use scene.setBackgroundBrush(Qt::blue);
    So now your code will look like :

    Qt Code:
    1. #include <QtGui>
    2. int main(int argc, char **argv){
    3. QApplication app(argc, argv);
    4. QGraphicsView view(&scene);
    5. scene.setBackgroundBrush(Qt::blue);
    6. view.show();
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: fillRect does not display anything?

    Thanks for the response. I think I oversimplified my example. Actually I'd like to be able to paint a filled rectangle anywhere on the screen, not necessarily just the background. What should I use instead of QPainter to do this?

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

    Default Re: fillRect does not display anything?

    You can't paint "anywhere on the screen" (meaning the desktop). You can only paint on widgets.
    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.


  5. #5
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: fillRect does not display anything?

    Oops, sorry. I meant to say "anywhere on the SCENE" (not SCREEN). Is this possible? I'd like to be able to superimpose simple drawings on the scene - it seemed like QPainter was supposed to do exactly that.

    For that matter, maybe you have a better solution for my specific current problem. I'm trying to fill in the rectangle that contains a character (QSimpleTextItem - thanks wysota!). I am able to get a rectangle to appear around the item by doing the following
    Qt Code:
    1. QRectF r = item->sceneBoundingRect();
    2. scene->addRect(r);
    To copy to clipboard, switch view to plain text mode 
    but what I'd really like to do is display a filled rectangle. How do I do this?

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

    Default Re: fillRect does not display anything?

    The scene should be composed from items, so use items and forget drawing on the scene directly. It's possible but I don't think this is something you really want.
    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. QPixmap Display RGB data
    By phoenixtju in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2009, 10:31
  2. synchronise QLabel display and webcam capture
    By fbmfbm in forum Qt Programming
    Replies: 2
    Last Post: 24th February 2009, 11:10
  3. OS/X how to get physical display handle
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2009, 19:51
  4. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  5. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08

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.