Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Casting QGraphicsObject to QObject for connecting QML signals to C++ slots (Harmattan specific issues?)
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Casting QGraphicsObject to QObject for connecting QML signals to C++ slots (Harmattan specific issues?)

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 6.2k 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
    solatic
    wrote on last edited by
    #1

    Hi,

    Trying to learn Harmattan development (being a Qt newbie isn't helping) and one of the fundamental things is connecting QML signals to C++ slots (my end goal is to run some XML-import code where the QML signal states which XML file, then the C++ processes the file and then signals the QML back with the extracted data).

    So my start page looks like this:

    @
    import QtQuick 1.1
    import com.nokia.meego 1.0

    Page {
    Button {
    ...
    signal importerSignal(string msg);
    ...
    onClicked: {
    importerSignal("XMLName");
    pageStack.push(Qt.resolvedUrl("NextPage.qml"));
    }
    }
    }
    @

    and that should have the signal taken care of. The slot is defined in the main C++ fille:

    @
    #include <QtGui/QApplication>
    #include <QDebug>
    #include "qmlapplicationviewer.h"

    class MyClass : public QObject{
    Q_OBJECT

    public slots:
    void LoadXML(const QString &msg){--
    qDebug() << "Requesting XML for " << msg;
    }
    };

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile&#40;QLatin1String("qml/myproject/main.qml"&#41;&#41;;
    
    QObject *rootObject = viewer.rootObject();
    
    MyClass xmlreader;
    QObject::connect(rootObject, SIGNAL(importerSignal(QString)), &xmlreader, SLOT(LoadXML(QString)));
    
    viewer.showExpanded();
    
    return app->exec&#40;&#41;;
    

    }
    @

    But, I'm getting an error saying that viewer.rootObject() (which was generated by QtCreator) is a QGraphicsObject, not a QObject. Other threads (for non-Harmattan projects) say, OK then just cast viewer.rootObject() as a QObject, but doing so:

    @QObject *rootObject = (QObject)viewer.rootObject();@

    just gives me errors saying, in essence, that it can't be cast as such (error: no matching function for call to 'QObject::QObject(QGraphicsObject*)'). Is there something I missed, or am I supposed to connect signals to slots differently for N9/N950 development, or...?

    My understanding is that I don't need to #include <QDeclarativeView> because qmlapplicationviewer.h already includes it. Also, if it helps for some reason, the build target is for a remote physical N9.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      srikanth_trulyit
      wrote on last edited by
      #2

      qobject_cast is preferred way to cast between qobjects.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        qobject_cast is not necessary to assign to a QObject pointer, as everything that qobject_cast return is a QObject.

        The include

        @
        #include <QGraphicsObject>
        @

        is missing. The compiler does plainly not know that [[Doc:QGraphicsObject]] inherits from QObject.

        http://www.catb.org/~esr/faqs/smart-questions.html

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

          Thanks Volker, that solved it (not sure why the compiler doesn't know about QGraphicsObject; if QmlApplicationViewer has a function with return type QGraphicsObject then wouldn't the compiler know about QGraphicsObject from QmlApplicationViewer? But I digress...).

          Different question comes up now: undefined reference to vtable for the MyClass. A little Googling says this is a common error that indicates that MyClass's moc-generated code isn't linking into the executable, but I don't see where I'm supposed to define that stuff in QtCreator. Furthermore, I'm not certain why it wouldn't be linked correctly; after all, the class is a part of the main.cpp file (bad programming practice, I know, but baby steps first...), which presumably WAS linked properly into the executable, since the initial code builds ran fine.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            About the QGraphicsObject/QObject pointer:
            The method returns a pointer to an object of a certain type. That's all the compiler knows about that method. To decide whether the returned type is compatible with the pointer variable it is assigned to, the compiler needs the class declaration of that object. In most cases, that declaration (read: include file) has already been included by the main class' header. This is one of the exceptions, where it is not needed - so you need to include it maually.

            For the second problem:
            You should put MyClass into a separate header file and add it to the HEADERS variable in the .pro file. Then re-run qmake and build your project. If the error remains, do a complete rebuild of the project. Don't forget to #include it in your main.cpp, of course :-)

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • S Offline
              S Offline
              solatic
              wrote on last edited by
              #6

              Thanks, all problems solved :)

              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