Results 1 to 8 of 8

Thread: How to change position of Qgraphicsview image in real-time?

  1. #1
    Join Date
    Oct 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to change position of Qgraphicsview image in real-time?

    Hello, I recently figured out how to draw an image but now I'm trying to be able change the position of the image "whenever I want"/i.e. real time. I tried to by changing the position in a loop in the main file, but it just seemed to make the window freeze.

    So, can someone give me an idea of what's going on? I tried studying the examples but the layers and layers of abstractions seem to be hiding the fundamentals.

    So, I have to create a class that extends the Qgraphicsitem and use that to create the Qpixmap item. And, then I have to overwrite the paint function that draws the image? And then after that, I can change the position of the QPixmap whenever I want and the position will automatically change in the scene?

    Just curious if I'm getting this straight.

  2. #2
    Join Date
    Oct 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change position of Qgraphicsview image in real-time?

    I was looking at the asteroids example, and I think I got to the root of what caused it to move. Apparently there's a "moveBy" function and I assume an overwritten paint function does the actual painting (refreshing the painting).

    Searching online, it appears this is a function from QCanvasItem but I think that belongs to QCanvas and I thought QCanvas was being antiquated in favor of QGraphics? So, what the modern form of doing this? I don't really like how all of the "QGraphicsItem" examples are just ports from QCanvas, so it doesn't seem like I can learn the "proper way" to do this with QGraphics.

  3. #3
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: How to change position of Qgraphicsview image in real-time?

    If you create a loop that does something, it will block your GUI, so this is expected behaviour.

    The proper way would be either to implement advance() in the graphic item or move it in a slot that is periodically triggered by a QTimer.
    It's nice to be important but it's more important to be nice.

  4. #4
    Join Date
    Oct 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change position of Qgraphicsview image in real-time?

    Seeing how advance() is a function of QGraphicsItem....

    Would I sub-class QGraphicsItem and then over-ride the advance and paint function to customize?

    And then in the advance function, I would manually use the setPos to change the position of the image?

    And then in the paint function, I would just call "drawImage" or something similar to redraw the image?

  5. #5
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: How to change position of Qgraphicsview image in real-time?

    Yes, indeed.
    It's nice to be important but it's more important to be nice.

  6. #6
    Join Date
    Oct 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change position of Qgraphicsview image in real-time?

    Ok, so I got the code set up and it builds fine but... it's not moving, although the image does show. Here's the code...

    I programmed it to just move back and forth from one spot to the next on each advance, so maybe it's just moving by sooo fast that it appears to stay in one position?
    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsEllipseItem>
    3. #include <QGraphicsScene>
    4. #include <QGraphicsView>
    5. #include <qdatetime.h>
    6. #include <qmainwindow.h>
    7. #include <qstatusbar.h>
    8. #include <qmessagebox.h>
    9. #include <qmenubar.h>
    10. #include <qapplication.h>
    11. #include <qpainter.h>
    12. #include <qprinter.h>
    13. #include <qlabel.h>
    14. #include <qimage.h>
    15. #include <qpixmap.h>
    16. #include <QMouseEvent>
    17. #include <QStyleOptionGraphicsItem>
    18. #include <qdebug.h>
    19. #include <stdlib.h>
    20.  
    21. QString myImgName = "myimg.png";
    22. static QImage *myImg;
    23.  
    24. static const int imageRTTI = 984376;
    25.  
    26. class ImageItem: public QGraphicsRectItem
    27. {
    28. public:
    29. ImageItem( QImage img );
    30. int rtti () const { return imageRTTI; }
    31. void advance(int phase);
    32. protected:
    33. void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
    34. private:
    35. QImage image;
    36. QPixmap pixmap;
    37. int state;
    38. };
    39.  
    40.  
    41. void ImageItem::advance(int phase)
    42. {
    43. if(state==0)
    44. {
    45. moveBy(0,30);
    46. state = 1;
    47. }
    48. else
    49. {
    50. moveBy(0,-30);
    51. state=0;
    52. }
    53. }
    54.  
    55.  
    56. ImageItem::ImageItem( QImage img )
    57. : image(img)
    58. {
    59. state = 0;
    60. setRect(0, 0, image.width(), image.height());
    61. setFlag(ItemIsMovable);
    62. pixmap = pixmap.fromImage(image, Qt::OrderedAlphaDither);
    63.  
    64. }
    65.  
    66. void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
    67. {
    68. p->drawPixmap( option->exposedRect, pixmap, option->exposedRect );
    69. }
    70.  
    71. int main( int argc, char **argv )
    72. {
    73.  
    74. QApplication app(argc, argv);
    75.  
    76. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    77.  
    78. myImg = new QImage;
    79. myImg->load(myImgName);
    80.  
    81. QAbstractGraphicsShapeItem* i = new ImageItem(*myImg);
    82. scene.addItem(i);
    83. i->setPos(-50,-50);
    84.  
    85. QGraphicsView view( &scene );
    86. view.setRenderHints( QPainter::Antialiasing );
    87. view.show();
    88.  
    89. return app.exec();
    90. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change position of Qgraphicsview image in real-time?

    Okay, I just changed the code so that instead of moving back and forth, it constantly goes in one direction. So, if it was going "so fast", it would've disappeared. Well, it didn't, so that tells me advance is not being executed, or the image isn't being updated, or something.

  8. #8
    Join Date
    Oct 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change position of Qgraphicsview image in real-time?

    Just got it to work!!!

    Apparently I had to stick something like...

    Qt Code:
    1. QTimer timer;
    2. QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
    3. timer.start(100);
    To copy to clipboard, switch view to plain text mode 

    in the main function to get it to operate correctly. If my intuition is correct, timeout is called when the timer counts down from 100 to 0 and it then calls advance(), and then the timer starts again at 100 and repeats the cycle. So, it seems to be a way for everything to synchronize.

    Now, it's time for some real development to begin.

    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsEllipseItem>
    3. #include <QGraphicsScene>
    4. #include <QGraphicsView>
    5.  
    6. #include <qdatetime.h>
    7. #include <qmainwindow.h>
    8. #include <qstatusbar.h>
    9. #include <qmessagebox.h>
    10. #include <qmenubar.h>
    11. #include <qapplication.h>
    12. #include <qpainter.h>
    13. #include <qprinter.h>
    14. #include <qlabel.h>
    15. #include <qimage.h>
    16. #include <qpixmap.h>
    17. #include <QMouseEvent>
    18. #include <QStyleOptionGraphicsItem>
    19. #include <qdebug.h>
    20. #include <stdlib.h>
    21. #include <qtimer.h>
    22.  
    23. QString myImgName = "myimg.png";
    24. static QImage *myImg;
    25.  
    26.  
    27. static const int imageRTTI = 984376;
    28.  
    29.  
    30. class ImageItem: public QGraphicsRectItem
    31. {
    32. public:
    33. ImageItem( QImage img );
    34. int rtti () const { return imageRTTI; }
    35. void advance(int phase);
    36. protected:
    37. void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
    38. private:
    39. QImage image;
    40. QPixmap pixmap;
    41. int state;
    42. };
    43.  
    44.  
    45. void ImageItem::advance(int phase)
    46. {
    47. moveBy(0,1);
    48. }
    49.  
    50.  
    51. ImageItem::ImageItem( QImage img )
    52. : image(img)
    53. {
    54. state = 0;
    55. setRect(0, 0, image.width(), image.height());
    56. setFlag(ItemIsMovable);
    57. #if !defined(Q_WS_QWS)
    58. pixmap = pixmap.fromImage(image, Qt::OrderedAlphaDither);
    59. #endif
    60. }
    61.  
    62. void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
    63. {
    64. // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
    65. // but on other platforms, we need to use a QPixmap.
    66. #if defined(Q_WS_QWS)
    67. p->drawImage( option->exposedRect, image, option->exposedRect, Qt::OrderedAlphaDither );
    68. #else
    69. p->drawPixmap( option->exposedRect, pixmap, option->exposedRect );
    70. #endif
    71. }
    72.  
    73. int main( int argc, char **argv )
    74. {
    75.  
    76. QApplication app(argc, argv);
    77.  
    78. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    79.  
    80. myImg = new QImage;
    81. myImg->load(myImgName);
    82.  
    83.  
    84. QAbstractGraphicsShapeItem* i = new ImageItem(*myImg);
    85. scene.addItem(i);
    86. i->setPos(-50,-50);
    87.  
    88. QTimer timer;
    89. QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
    90. timer.start(100);
    91.  
    92. QGraphicsView view( &scene );
    93. view.setRenderHints( QPainter::Antialiasing );
    94. view.show();
    95.  
    96. return app.exec();
    97. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How can change image format?
    By electronicboy in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 09:44
  2. QTimer stops after system time change
    By djurodrljaca in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2009, 20:36

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.