Results 1 to 6 of 6

Thread: QGraphicsScene in paintEvent ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2010
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsScene in paintEvent ?

    I can create and display a graphics scene, in main(), like this -

    Qt Code:
    1. QGraphicsScene scene(0, 0, WIDTH, HEIGHT);
    2.  
    3. Button *button;
    4. Button *button2;
    5.  
    6. QPixmap bg("/../mypix.png");
    7. scene.addPixmap(bg);
    8.  
    9. button = new Button();
    10. button2 = new Button();
    11.  
    12. scene.addItem(button);
    13. scene.addItem(button2);
    14.  
    15. view.setScene(&scene);
    16. view.show();
    To copy to clipboard, switch view to plain text mode 

    How would I alter the code to put it in a paintEvent function ? Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene in paintEvent ?

    paintEvent() is an event handler, which is called by Qt when a widget needs to paint it self (QPaintEvent).
    You do not call event handlers your self, they get called by the event loop.
    If you want a widget to have a custom painting, you have to re implement its paintEvent(), but leave the calling to the event loop.

    Therefore, your question is "wrong", since paintEvent() never comes by its self, its a member method, which you do not call, just re implement.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2010
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene in paintEvent ?

    I realise that paintEvent() is not directly called and has to be re-implemented - I was wondering if someone could give me example code showing how my example use of a graphics scene construction in main() would look in paintEvent() ?

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

    Default Re: QGraphicsScene in paintEvent ?

    Constructing a graphics scene in a paint event doesn't make any sense. Paint event should only contain code for redrawing the widget it is part of.
    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
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene in paintEvent ?

    how my example use of a graphics scene construction in main() would look in paintEvent()
    this sentence has not meaning... or I fail to to extract any meaning from it.

    Maybe if you explain what it is you want to do, we might help you to get in the right direction.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene in paintEvent ?

    I also am a little bit confused with what you're trying to do....Are you trying to have a scene embedded in a widget that is recreated every single time the paintevent for the parent widget is called? If that is the case, I believe that in the way qgraphicsscene is currently implemented, all events sent to a parent object are automatically sent to the scene as well, so you shouldn't have to tie the two together. If you are, however, trying to create a brand-new graphics scene every time the paintevent event is called in the eventloop is would urge you to rethink your implementation.

Similar Threads

  1. Calling paintEvent when I want to
    By di_zou in forum Newbie
    Replies: 3
    Last Post: 14th October 2009, 10:29
  2. Faster paintEvent
    By SailinShoes in forum Qt Programming
    Replies: 1
    Last Post: 1st October 2008, 15:25
  3. Finish of PaintEvent
    By jimfan in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 07:50
  4. paintEvent problem
    By MoaLaiSkirulais in forum Qt Programming
    Replies: 5
    Last Post: 12th February 2008, 13:59
  5. Replies: 12
    Last Post: 5th February 2006, 10:34

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.