Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to resolve the QObject errors

How to resolve the QObject errors

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    subram_QT
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

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

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          subram_QT
          wrote on last edited by
          #4

          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 *)'

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Are you missing:

            @#include <QGraphicsObject>@

            ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved