Results 1 to 17 of 17

Thread: Dynamic adding of widgets to QGraphicsScene on the fly

  1. #1
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Dynamic adding of widgets to QGraphicsScene on the fly

    Hello,

    I'm trying to make a simple game to learn how to use QGraphicsScene. I want it to be the typical asteroid shooting game where you are a ship and you fly around.

    What I'm interested in learning how to do is also to dynamically add and control items in the QGraphicsScene. Currently, I am aware that we can do something along the lines of

    Qt Code:
    1. QGraphicsPixmapItem *bkgimg = scene->addPixmap(pixmap);//pretty background image
    To copy to clipboard, switch view to plain text mode 

    (pixmap was defined earlier) and then we can say

    Qt Code:
    1. bkgimg->setPos(0,0);
    To copy to clipboard, switch view to plain text mode 

    if we want to move it around.


    My question is can I add an item to QGraphicsScene sort of like this:
    Qt Code:
    1. scene->addItem(QGraphicsItem(300,300));
    To copy to clipboard, switch view to plain text mode 

    where 300,300 are the x and y positions and be able to modify that item later on? Or perhaps there is a way to dynamically declare objects and add them to QGraphicsScene and use signals/slots to update their position?

    Thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    My question is can I add an item to QGraphicsScene sort of like this:
    No, not like that. The code you posted won't compile.

    You can of course add an item that is a subclass of QGraphicsItem dynamically. If what you are asking is can you add a QGraphicsItem instance itself to the scene and change that to a specific type of graphics item later, no, you can't. You can't create an instance of QGraphicsItem itself because it is an abstract class. But neither can you create an instance of some concrete graphics item, add it to the scene, retrieve it later and change that same instance to an object of a different type. You can remove the first object and add the second object.

    You can change the properties of a graphics item instance dynamically, for example the position, size, z-value, pen, brush. etc. without adding and removing things

    If you want a graphics item to be able to handle or send signals, then you need to derive your class from QGraphicsObject, which inherits QObject. You can then do almost anything you want with custom signals and slots.

  3. #3
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    Thank you for your reply, d_stranz, it is a little clearer now.

    My next question was going to be what if I have a check for an event to occur and if a certain criterion is met, I want to spawn a graphics object such as an enemy on the screen at some position; do I have to define a preset number of enemy objects and somehow consecutively set enemy1 at pos (x1,y1), enemy2 at pos(x2,y2). But I stumbled on this thread and I think I figured out how to use vectors for that.

    I could define a class playerDraw : public QWidget type and create a vector<QWidget*> enemies container to which I'll be able to pass off push_back(new playerDraw(x,y), then just a simple for loop to scene->addWidget(enemies[j]) and set their position. I think that is quite an elegant solution.

    Thoughts?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    You probably don't want widgets as you game objects but otherwise that sounds ok.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    Ok I've been learning how to add widgets dynamically to QGraphicsScene and I almost got it. I have encountered an issue when trying to set the position of a widget, as the (0,0) point of the widget is in the top left corner and I need it to be in the center.

    I have
    Qt Code:
    1. QWidget *mySprite;//it has the paintEvent which draws everything I need
    2. QGraphicsWidget *wdg=scene->addWidget(mySprite);
    3.  
    4. //but now when I try to
    5. wdg->rotate(90);
    6. //it does so about top left point (0,0). And doing
    7.  
    8. wdg->translate(100,100);
    9. //doesn't seem to help. It actually appears that it moves the whole widget instead of moving the reference point
    To copy to clipboard, switch view to plain text mode 

    Any ideas how I can set the center point of my QGraphicsWidget?

    Thanks

    Edit: Also, how should we use QVector<QGraphicsWidget*> when trying to access properties of the graphics widgets? Should we use a for loop or should we use a foreach loop? (I've never used the latter before)

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    You are probably looking for setTransformOriginPoint()

    But I still don't get why you want widgets. What kind of standard UI elements are those made of?

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    Mr_Cloud (30th April 2014)

  8. #7
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    Brilliant dot exe. Thank you for that.


    Quote Originally Posted by anda_skoa View Post
    But I still don't get why you want widgets. What kind of standard UI elements are those made of?
    Just ellipses and lines at the moment. You're right, I shouldn't limit myself to QWidgets, but these seem to do the job for now.

    Quote Originally Posted by Mr_Cloud
    Edit: Also, how should we use QVector<QGraphicsWidget*> when trying to access properties of the graphics widgets? Should we use a for loop or should we use a foreach loop? (I've never used the latter before)
    Any ideas about this one, anda_skoa? And what I mean by this is that does a simple for (int i=0;i<myVector.size();i++){//do stuff;} work for this seemingly unusual (different, at least to me) QVector? Thanks
    Last edited by Mr_Cloud; 30th April 2014 at 15:02. Reason: Derp moment, got bits stuck in ceiling fan

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    Quote Originally Posted by Mr_Cloud View Post
    Just ellipses and lines at the moment. You're right, I shouldn't limit myself to QWidgets, but these seem to do the job for now.
    I am afraid I don't understand. There is no ellipse widget.

    Quote Originally Posted by Mr_Cloud View Post
    J
    Any ideas about this one, anda_skoa?
    Makes really no difference on a vector, use whatever you prefer.

    Cheers,
    _

  10. #9
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    Quote Originally Posted by anda_skoa View Post
    I am afraid I don't understand. There is no ellipse widget.
    Qt Code:
    1. class spriteDraw : public QWidget
    2. {
    3. spriteDraw();
    4.  
    5. protected:
    6. void paintEvent(QPaintEvent *event){//paint me like one of your french girls;}
    7.  
    8. private:
    9. QPainter painter;
    10. }
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    If you use custom painting, why not simply subclass QGraphicsItem?
    Or using a QGraphicsEllipseItem for the ellipse, QGraphicLineItem or QGraphicPolygonItem for lines, or more generically QGraphicsPathItem

    Cheers,
    _

  12. #11
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    I can see why you're suggesting I use QGraphicsItem. It looks like it's much easier to handle than a QWidget. I think I'll do that.

    Seeing as setRotation() is not available for QGraphicsItem, I'll implement the rotation in the void QGraphicsItem::paint().


    Regards,
    Mr_Cloud

  13. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    Quote Originally Posted by Mr_Cloud View Post
    Seeing as setRotation() is not available for QGraphicsItem.
    No?
    http://qt-project.org/doc/qt-4.8/qgr...ml#setRotation

    Cheers,
    _

  14. #13
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    You were right, anda_skoa, I was able to setRotation(). Code-wise everything compiles. It's much easier to use QGraphicsItem, thank you.

    I have an issue with loading a jpeg as the background though. On the machine that has the Qt environment set up (4.8.5, MSVC 2013, environment variables set) the jpeg loads fine, but as soon as I run the exe on a PC without Qt/MSVC installed, the jpg loading into memory fails.

    I've checked DLL dependencies with ProcessExplorer on Qt machine and besides the usual QtCore4 and QtGui4 DLLs, it also loads qjpeg4.dll from /qt/plugins/imageformats/ to handle the background. This DLL is not loaded nor mentioned on the non-Qt machines. How come this is happening?

    Here is how I load the jpg:
    Qt Code:
    1. #include <QImageReader>
    2. #include <QPixmap>
    3.  
    4. QImageReader reader("bkg.jpg");
    5. QSize imageSize = reader.size();
    6. imageSize.scale(600,600, Qt::IgnoreAspectRatio);
    7. reader.setScaledSize(imageSize);
    8. QPixmap pixmap=QPixmap::fromImage(QImage(reader.read()));
    9. scene->addPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

    I tried scene->addPixmap(QPixmap::fromImage(QImage("bkg.jpg")) ); for comparison and still no avail. Any ideas?


    Regards,
    Mr_Cloud

  15. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    Do you have the image format plugin for JPEG correctly as part of your deployment?

    Cheers,
    _

  16. #15
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    I don't think so buddy. How do I do that?

  17. #16
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly


  18. #17
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic adding of widgets to QGraphicsScene on the fly

    Thanks for that. I think I'll go with the dynamic way of just copying /imageformats/ with what dependencies I need in it.

    Once again, thank you for your guidance, anda_skoa. It means a lot.


    Regards,
    Mr_Cloud

Similar Threads

  1. QGraphicsScene and dynamic addition of QGraphicsItems
    By El Bazza in forum Qt Programming
    Replies: 7
    Last Post: 15th September 2012, 07:57
  2. Dynamic sorting using proper column after adding a row.
    By kremuwa in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2010, 23:50
  3. Replies: 5
    Last Post: 18th April 2010, 23:31
  4. Adding Library Methods - Static Vs Dynamic
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2010, 16:16
  5. Dynamic widgets adding and connecting signals& slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2009, 12:36

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.