Results 1 to 4 of 4

Thread: Qml QGrapicsObject type in QGraphicsScene

  1. #1
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    20
    Thanked 28 Times in 27 Posts

    Default Qml QGrapicsObject type in QGraphicsScene

    In Qt4.8 we can use to Qml to create QGraphicsObject as described in the documentation as :http://doc.qt.io/qt-4.8/qml-integration.html#adding-qml-widgets-to-a-qgraphicsscene
    Qt Code:
    1. QGraphicsScene* scene = myExistingGraphicsScene();
    2. QDeclarativeEngine *engine = new QDeclarativeEngine;
    3. QDeclarativeComponent component(engine, QUrl::fromLocalFile("myqml.qml"));
    4. QGraphicsObject *object =
    5. qobject_cast<QGraphicsObject *>(component.create());
    6. scene->addItem(object);
    To copy to clipboard, switch view to plain text mode 
    I want the type() of QGraphicsItem in the QGraphicsObject i get from the factory function of QDeclarativeComponent to be set to something different from the default 'User' set in QGraphicsItem.
    As described in the docs here http://doc.qt.io/qt-4.8/qgraphicsitem.html#type I need to subclass the QgraphicsItem.
    If I go ahead and do something like
    Qt Code:
    1. class QmlObject : public QGraphicsObject{
    2. Q_OBJECT
    3. public:
    4. enum {Type = TypeMyType;}//TypeMyType = UserType+1;
    5. int type() const { return Type; }
    6. QmlObject ();
    7. }
    8.  
    9. QmlObject :: QmlObject () : QGraphicsObject()
    10. {
    11. QGraphicsScene* scene = myExistingGraphicsScene();
    12. QDeclarativeEngine *engine = new QDeclarativeEngine;
    13. QDeclarativeComponent component(engine, QUrl::fromLocalFile("myqml.qml"));
    14. QGraphicsObject *object =
    15. qobject_cast<QGraphicsObject *>(component.create());
    16. scene->addItem(object);
    17. //DEAD END HERE, can't change the embedded QGraphicsObject already initialized.
    18. }
    To copy to clipboard, switch view to plain text mode 
    So if I go ahead by this route, since QObject is not copyable by Q_DISABLE_COPY, there is no way left for me to subclass it. And if I can't subclass it, I can't set the type of QGraphicsItem to something other than User.
    AnyIdea how to go ahead and set the type of the QGraphicsItem?

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

    Default Re: Qml QGrapicsObject type in QGraphicsScene

    You are trying to do something weird. The docs you mention are related to something completely different, there is no subclassing there. It is possible to do what you want but in a different way -- subclass QGraphicsObject to return your new type, then create a QML component using QDeclarativeEngine instance, then cast the resulting object to QGraphicsObject or QDeclarativeItem and set it as a child of your custom graphics object. Then just make sure the child always occupies the whole area of its parent (you can use QGraphicsLayout for this).
    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.


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

    pkj (9th January 2013)

  4. #3
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    20
    Thanked 28 Times in 27 Posts

    Default Re: Qml QGrapicsObject type in QGraphicsScene

    cast the resulting object to QGraphicsObject or QDeclarativeItem and set it as a child of your custom graphics object. Then just make sure the child always occupies the whole area of its parent (you can use QGraphicsLayout for this).
    http://doc.qt.io/qt-4.8/QGraphicsLayout only work for items which inherit http://doc.qt.io/qt-4.8/QGraphicsLayoutItem. QGraphicsWidget does that. But not QDeclarativeItem. So I cannot use QGraphicsLayout. Is there another way in which the wrapper object for the QDeclarativeItem has a layout?

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

    Default Re: Qml QGrapicsObject type in QGraphicsScene

    Then monitor the size yourself and resize the child when the parent changes size.
    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. Replies: 1
    Last Post: 8th September 2012, 05:08
  2. QGraphicsScene only creating one item of each type
    By Fallen_ in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2011, 07:28
  3. Replies: 7
    Last Post: 19th April 2011, 12:20
  4. Replies: 2
    Last Post: 22nd December 2009, 20:52
  5. Type and User Type
    By campana in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2006, 23:22

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
  •  
Qt is a trademark of The Qt Company.