Important: Please read the Qt Code of Conduct -

How to resolve the QObject errors

  • I wrote the following code to assign image source runtime .


    class Main : public QQuickView

    static Main *instance();

    void mousePressEvent(QMouseEvent* m);
    //void keyReleaseEvent(QKeyEvent* e);
    void setup();
    static QObject *rootObject;
    static QObject *image;
     int lock_value;
    static QStringList lockimage;

    explicit Main();


    main instance ()
    QDeclarativeView* view = NULL;
    rootObject = dynamic_cast<QObject *>(view->rootObject());
    //rootObject = (QObject *)view->rootObject();
    // find element by name
    image = rootObject->findChild<QObject *>(QString("status_locked_img"));

    display instance ()

    image->setProperty("source", QString("images/image1.png"));



    Rectangle {
    color: "black"
    width: 10; height:10
    property int type : lock_status
    Image {
    objectName: status_locked_img
    visible: true

    I am getting error while building

    " error: C2681: 'QGraphicsObject *' : invalid expression type for dynamic_cast"

    please help me in resolving this error

  • Lifetime Qt Champion


    Please enclose your code with coding tags, that will make it readable.

    When casting QObject derivative, you should use "qobject_cast":

  • In addition to SGaist comments "here is the description for placing code tags.":

  • after adding qobject_cast

    " rootObject = qobject_cast<QObject *>(view->rootObject());"

    I am getting following error

    19: error: C2665: 'qobject_cast' : none of the 2 overloads could convert all the argument types
    C:\Qt\Qt5.0.2\5.0.2\msvc2012_64\include\QtCore/qobject.h(456): could be 'T qobject_cast<QObject*>(QObject )'
    T=QObject *
    C:\Qt\Qt5.0.2\5.0.2\msvc2012_64\include\QtCore/qobject.h(465): or 'T qobject_cast<QObject
    >(const QObject *)'
    T=QObject *
    while trying to match the argument list '(QGraphicsObject *)'

  • Lifetime Qt Champion

    Are you missing:

    @#include <QGraphicsObject>@


Log in to reply