Results 1 to 3 of 3

Thread: problem with qobject_cast<QWidget *>(object) (from QML object)

  1. #1
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default problem with qobject_cast<QWidget *>(object) (from QML object)

    Qt Code:
    1. qmlView = new QDeclarativeView;
    2. qmlView->setSource( QUrl::fromLocalFile( "modaldialog.qml" ) );
    3. QObject *object = qmlView->rootObject();
    4.  
    5. QMetaObject::invokeMethod( object, "dialog_type_02",
    6. Q_RETURN_ARG( QVariant, returnedValue ),
    7. Q_ARG( QVariant, dlg_text )
    8. );
    9.  
    10. QWidget *widget = qobject_cast<QWidget *>(object);//doesn't work, return null
    11. layout->addWidget( widget );//doesn't work, above is null
    12.  
    13. //however if i replace the above 2 lines of code with this:
    14. QGraphicsObject* graphicsObject =qobject_cast<QGraphicsObject *>(object); //works fine
    15. scene.addItem( graphicsObject ); //works fine
    To copy to clipboard, switch view to plain text mode 

    why qobject_cast works ok for a QGraphicsObject and not for a QWidget coming from QML?

    Why use the "layout->addWidget(...)" option? because i want a QDialog with the stuff coming from QML

  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: problem with qobject_cast<QWidget *>(object) (from QML object)

    because:
    Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns 0. If object is 0 then it will also return 0.
    QGraphicsObject is NOT a QWidget subclass which is why:
    Qt Code:
    1. QWidget *widget = qobject_cast<QWidget *>(object);
    To copy to clipboard, switch view to plain text mode 
    wont work.

    because i want a QDialog with the stuff coming from QML
    This technology does not exist yet.
    You can have widgets in QGraphicsView, but you can't have QGraphicsObject as QWidgets. (unless these are QProxyWidget s, which means they have a QWidget counterparts to start with)
    Last edited by high_flyer; 5th May 2011 at 09:16.
    ==========================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. The following user says thank you to high_flyer for this useful post:

    Mrdata (5th May 2011)

  4. #3
    qttre Guest

    Default Re: problem with qobject_cast<QWidget *>(object) (from QML object)

    thanks,very much.

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:07
  2. Deleting a QWidget derived object manually
    By hubbobubbo in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2010, 16:32
  3. Can't add an object derived from QWidget in a layout.
    By Netich in forum Qt Programming
    Replies: 1
    Last Post: 14th January 2010, 21:55
  4. Removing a QWidget object from a QTableWidget cell
    By mclark in forum Qt Programming
    Replies: 5
    Last Post: 13th March 2008, 15:19
  5. Replies: 1
    Last Post: 2nd May 2006, 21:11

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.