How to resolve the QObject errors



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

    //main.h

    class Main : public QQuickView
    {
    Q_OBJECT

    public:
    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;
    

    private:
    explicit Main();

    };

    //main.cpp
    main instance ()
    {
    QDeclarativeView* view = NULL;
    view->setSource(QUrl::fromLocalFile("main.qml"));
    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"));

    }

    //main.qml

    Rectangle {
    id:status_locked
    color: "black"
    width: 10; height:10
    x:8;y:6
    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

    Hi,

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

    When casting QObject derivative, you should use "qobject_cast":http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#qobject_cast


  • Moderators

    In addition to SGaist comments "here is the description for placing code tags.":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01



  • 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 )'
    with
    [
    T=QObject *
    ]
    C:\Qt\Qt5.0.2\5.0.2\msvc2012_64\include\QtCore/qobject.h(465): or 'T qobject_cast<QObject
    >(const QObject *)'
    with
    [
    T=QObject *
    ]
    while trying to match the argument list '(QGraphicsObject *)'


  • Lifetime Qt Champion

    Are you missing:

    @#include <QGraphicsObject>@

    ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.