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. Cannot connect MyClass::data(QVariant) to (null)::updateData(QVariant)
Forum Updated to NodeBB v4.3 + New Features

Cannot connect MyClass::data(QVariant) to (null)::updateData(QVariant)

Scheduled Pinned Locked Moved QML and Qt Quick
17 Posts 2 Posters 5.3k 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.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    Below is my code
    I can't find where is the error.
    module "QtQuick" version 2.2 is not installed.
    Connect: cannot connect MyClass::data(QVariant) to (null)::updateData(QVariant)

    @

    -----------header file-----------

    #ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QObject>
    #include <QVariant>
    #include <QDeclarativeItem>

    class MyClass: public QObject
    {
    Q_OBJECT
    public:
    MyClass(){}
    public slots:
    void getData(){
    QString text("New data");
    emit data(QVariant (text)); //sending data to qml

    }
    

    signals:
    void data(QVariant data);

    };

    -----------cpp file-----------
    #include <QApplication>
    #include <QGraphicsObject>
    #include <QDeclarativeView>
    #include <myclass.h>
    #include <QUrl>
    #include <QtGui>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    MyClass myClass;
    
    QDeclarativeView view;
    view.setSource(QUrl("qrc:/file/main.qml"));
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    
    QObject *rootObject = dynamic_cast<QObject*>(view.rootObject());
    
    //QObject::connect(rootObject, SIGNAL(dataRequired()), &myClass, SLOT(getData()));
    QObject::connect(&myClass, SIGNAL(data(QVariant)), rootObject, SLOT(updateData(QVariant)));
    
    return app.exec();
    

    }

    -----------qml file-----------
    import QtQuick 2.2
    import QtQuick.Window 2.1

    Window {
    visible: true
    width: 360
    height: 360

    //signal dataRequired;
    function updateData(text) { dataText.text=text}
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
    
    Text{
        id:dataText
        anchors.centerIn: parent; color:"white"
    }
    

    }

    @

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Are you using Qt 4.x or Qt 5.x ?
      QDeclarativeView wont work with QtQuick 2.2 which was introduced in Qt 5

      157

      1 Reply Last reply
      0
      • H Offline
        H Offline
        houmingc
        wrote on last edited by
        #3

        did try 2 attempt and failed also
        @
        ---------1st attempt ---------------------------
        QQmlEngine engine;
        QQmlComponent component(&engine, QUrl("qrc:/file/main.qml"));

        QObject *root=component.create();

        QObject::connect(&myClass, SIGNAL(data(QVariant)), root, SLOT(updateData(QVariant)));

        error:
        aggregate "QQmlEngine Engine" has incomplete type and cannot be defined
        variable 'QQmlComponent component' has initializer but incomplete type

        ---------2rd attempt ---------------------------

        QQuickView view;
        view.setSource(QUrl("qrc:/file/main.qml"));

        QObject rootObject= dynamic_cast<QObject>(view.rootObject());
        QObject::connect(&myClass, SIGNAL(data(QVariant)), root, SLOT(updateData(QVariant)));

        error:
        cannot dynamic_cast view.QQuickView::rootObject to type 'Class QObject'
        main.o Error 1

        @

        1 Reply Last reply
        0
        • H Offline
          H Offline
          houmingc
          wrote on last edited by
          #4

          using Qt Creator 3.1.2 (opensource)
          will be upgrading the Qt end of the week.

          Not able to create an QML instance meant i can't continue anything, isn't it??

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            2nd Attempt will not work since Window requires QQmlApplicationEngine and not QQuickView to load it.

            Have a look at "this":http://qt-project.org/doc/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods for an example.

            Now instead of using QQmlEngine directly you can use more convenient QQmlApplicationEngine:
            @
            QQmlApplicationEngine engine;
            engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

            QObject *rootObject= engine.rootObjects().first();
            qDebug() << rootObject->objectName();

            QVariant returnedValue;
            QVariant msg = "Hello from C++";

            QMetaObject::invokeMethod(rootObject, "updateData",
            Q_RETURN_ARG(QVariant, returnedValue),
            Q_ARG(QVariant, msg));

            qDebug() << "QML function returned:" << returnedValue.toString();
            @

            157

            1 Reply Last reply
            0
            • H Offline
              H Offline
              houmingc
              wrote on last edited by
              #6

              i am running Qt on Ubuntu not windows

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                It's Window not Windows. Window here refers to the root element which you have used in the QML file.
                I think you must start with the basics of QML first.

                157

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  houmingc
                  wrote on last edited by
                  #8

                  Thanks, QMetaObject::InvokeMethod works better than signals/slot for C++ invoking QML. From same document, QML invoking C++, i could not compile, below is my code. Can help?
                  @
                  -----------------header file myclass----------------
                  #include <QDebug>

                  class MyClass: public QObject
                  {
                  Q_OBJECT
                  public slots:
                  void cppSlot(const QString &msg){
                  qDebug()<<"Called the c++ slot with message"<<msg;
                  }
                  };

                  -----------------cpp file----------------
                  #include <QApplication>
                  #include <QQmlApplicationEngine>
                  #include <QQuickView>
                  #include <QDebug>
                  #include "myclass.h"

                  int main(int argc, char *argv[])
                  {
                  QGuiApplication app(argc, argv);

                  QQuickView view("qrc:///main.qml");
                  QObject *item= view.rootObject();
                  
                  MyClass myClass;
                  QObject::connect(item,SIGNAL(qmlSignal(QString)),&myClass,SLOT(cppSlot(QString)));
                  
                  return app.exec&#40;&#41;;
                  

                  }

                  -----------------qml file----------------
                  import QtQuick 2.2
                  import QtQuick.Controls 1.1

                  ApplicationWindow {
                  id: item
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Hello World")

                  signal qmlSignal(string msg)
                  

                  MouseArea{
                  anchors.fill:parent
                  onClicked: item.qmlSignal("Hello from QML")
                  }
                  }
                  @

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    What error do you get during compiling?

                    157

                    1 Reply Last reply
                    0
                    • p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      To use ApplicationWindow/Window you will need to use QQmlApplicationEngine and not QQuickView. The error should be at runtime not compiletime.

                      157

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        houmingc
                        wrote on last edited by
                        #11

                        error at QQuickView and QObject statement.

                        @

                        no matching function for call to 'QQuickView::QQuickView(const char[16])'
                        cannot convert 'QQuickItem' to 'QObject *' in initialization.
                        @

                        1 Reply Last reply
                        0
                        • p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #12

                          Use QQmlApplicationEngine for loading ApplicationWindow/Window.
                          @
                          QQmlApplicationEngine engine;
                          engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
                          QObject *rootObject= engine.rootObjects().first();
                          QObject::connect(rootObject,SIGNAL(qmlSignal(QString)),
                          &myClass,SLOT(cppSlot(QString)));
                          @

                          Above should work.

                          157

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            houmingc
                            wrote on last edited by
                            #13

                            insert statement 'MyClass myclass;' before QObject::connect.
                            2 error encounter:
                            undefined reference to 'vtable for MyClass'
                            ld returned 1 exit status

                            1 Reply Last reply
                            0
                            • p3c0P Offline
                              p3c0P Offline
                              p3c0
                              Moderators
                              wrote on last edited by
                              #14

                              Run qmake and then Rebuild.

                              157

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                houmingc
                                wrote on last edited by
                                #15

                                I created a new header file for class 'MyClass'
                                Now my error is
                                QQmlApplicationEngine failed to load component
                                Assert:"!isEmpty()" in file /include/QtCore/qlist.h
                                The program has unexpectedly finished.

                                1 Reply Last reply
                                0
                                • p3c0P Offline
                                  p3c0P Offline
                                  p3c0
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  I don't know. You must have misplaced something somewhere. Better would be to create a new project with that class and qml file. Use the code that i posted above to launch the qml file. It is not a very complicated code.

                                  157

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    houmingc
                                    wrote on last edited by
                                    #17

                                    Thanks very much.

                                    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