Cannot connect MyClass::data(QVariant) to (null)::updateData(QVariant)
-
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.1Window {
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" }
}
@
-
Hi,
Are you using Qt 4.x or Qt 5.x ?
QDeclarativeView wont work with QtQuick 2.2 which was introduced in Qt 5 -
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@
-
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??
-
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();
@ -
i am running Qt on Ubuntu not windows
-
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. -
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();
}
-----------------qml file----------------
import QtQuick 2.2
import QtQuick.Controls 1.1ApplicationWindow {
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")
}
}
@ -
What error do you get during compiling?
-
To use ApplicationWindow/Window you will need to use QQmlApplicationEngine and not QQuickView. The error should be at runtime not compiletime.
-
error at QQuickView and QObject statement.
@
no matching function for call to 'QQuickView::QQuickView(const char[16])'
cannot convert 'QQuickItem' to 'QObject *' in initialization.
@ -
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.
-
insert statement 'MyClass myclass;' before QObject::connect.
2 error encounter:
undefined reference to 'vtable for MyClass'
ld returned 1 exit status -
Run qmake and then Rebuild.
-
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. -
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.
-
Thanks very much.