Results 1 to 4 of 4

Thread: Force the painting of a QGraphicsItem

  1. #1
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Force the painting of a QGraphicsItem

    Hello guys,

    I'd like to ask you a question about the Qt Graphics View Framework (Qt 4.3, MacOS X 10.4.10).

    I created a custom class that inherits from QGraphicsItem, defined as follows:

    Qt Code:
    1. #ifndef SIMTARGET_H
    2. #define SIMTARGET_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QString>
    6.  
    7. class Sim_Target : public QObject, public QGraphicsItem {
    8.  
    9. Q_OBJECT
    10.  
    11. public:
    12.  
    13. // Constructor
    14. Sim_Target();
    15.  
    16. // itemChange() function
    17. QVariant itemChange(GraphicsItemChange change, const QVariant & value);
    18.  
    19. // Shape
    20. QRectF boundingRect() const;
    21. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    22.  
    23. // Tooltip
    24. QString str_target_tooltip;
    25.  
    26. private:
    27. QColor color;
    28.  
    29. signals:
    30. void dragged();
    31.  
    32. };
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 

    In a slot belonging to my application's main window, I create an instance of the object and I insert it into the scene:

    Qt Code:
    1. // Create the target and add it to the scene
    2. simulationTarget = new Sim_Target;
    3. graphScene_Scene->addItem(simulationTarget);
    To copy to clipboard, switch view to plain text mode 

    then I perform some basic operations on the object, trying to move it:

    Qt Code:
    1. simulationTarget->setPos(10,30);
    2. for (int counter=1; counter<1000; counter++) {
    3. simulationTarget->setPos(simulationTarget->x()+1, simulationTarget->y());
    4. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that, during the execution of the for-cycle, I cannot see the object moving. At the end of the for-cycle, of course, when the application's control returns to the event loop, I see the object occupying the expected position (ie, the movement was performed correctly).

    I think I have to call some functions that force the continuos repainting of the object after each movement. But looking at the documentation I didn't understand which is the function I should call. Again, while the for-cycle is executing, I'd like to be able to control the mouse (for example to press the button that quits my application); instead, until the program doesn't reach the event loop, I can only see my mouse cursor as a spinning beachball (the Mac's equivalent for the Windows' hourglass).

    Could someone offer me some suggests, please? :-(

  2. #2
    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: Force the painting of a QGraphicsItem

    A bad solution is to call QApplication::processEvents() in the loop. A good solution is to have a QTimeLine object that controls the animation which is performed from within some slot.

  3. The following user says thank you to wysota for this useful post:

    fabietto (2nd July 2007)

  4. #3
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Force the painting of a QGraphicsItem

    Hello Wysota,

    sorry for my late reply. Anyway I appreciated very much your hints. I'm now studying the documentation to figure out how to use QTimeLine with QGraphicsItemAnimation.

    Thanks a lot,
    Fabio

  5. #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: Force the painting of a QGraphicsItem

    There is an example of using QGraphicsItemAnimation in its docs.

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  2. nmake problems while building mysql driver
    By MarkoSan in forum Installation and Deployment
    Replies: 27
    Last Post: 25th May 2007, 12:57
  3. QT4 beginner Fatal Error
    By Remyfr in forum Installation and Deployment
    Replies: 3
    Last Post: 11th March 2006, 01: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.