[Solved] Receiving signals from QML to C++
-
@//file main.qml
import QtQuick 1.0Rectangle {
width: 300; height: 200
MyItem {}
} @
@//file MyItem.qml
import QtQuick 1.0Item {
id: item
width: 100; height: 100signal qmlSignal(string msg) MouseArea { anchors.fill: parent onClicked: item.qmlSignal("Hello from QML") }
}@
@ //file main.cpp
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "myclass.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);QDeclarativeView view(QUrl::fromLocalFile("qml/MyClass/main.qml"));
_ QObject *item = view.rootObject();_
MyClass myClass;
_ QObject::connect(item,SIGNAL(qmlSignal(QString)),&myClass,SLOT(cppSlot(QString)));_
view.show(); return app.exec();
}
@
@//file MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include <qdebug.h>class MyClass : QObject
{
Q_OBJECTpublic slots:
void cppSlot(const QString &msg)
{
qDebug() << "called the C++ slot with message " << msg;
}public:
MyClass();
};#endif // MYCLASS_H
@
@ //file MyClass.cpp
#include "myclass.h"MyClass::MyClass()
{
} @i copied these snappets from the official reference and adapted them. the errors are:
..\MyClass\main.cpp: In function 'int qMain(int, char**)':
..\MyClass\main.cpp:10: error: cannot convert 'QGraphicsObject*' to 'QObject*' in initialization
..\MyClass\main.cpp:13: error: 'QObject' is an inaccessible base of 'MyClass' -
[quote author="spode" date="1308302610"]
i copied these snappets from the official reference and adapted them. the errors are:
..\MyClass\main.cpp: In function 'int qMain(int, char**)':
..\MyClass\main.cpp:10: error: cannot convert 'QGraphicsObject*' to 'QObject*' in initialization
..\MyClass\main.cpp:13: error: 'QObject' is an inaccessible base of 'MyClass'[/quote]Try to cast QGraphicsObject to QObject and do not forget to include required headers:
@
#include <QDeclarativeView>
@Also please check the implementation of MyClass.
-
leon.anavi, QDeclarativeView is already included bu qmlapplicationviewer.h (not usre why it is included in main.cpp btw). QGraphicsObject should be included in this case.
-
@ #include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "myclass.h"
#include <QGraphicsObject>
#include <QDeclarativeContext>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QDeclarativeView view(QUrl::fromLocalFile("qml/MyClass/main.qml")); MyClass myClass; view.rootContext()->setContextProperty("myObject", &myClass); //myObject sarĂ associato (in QML) alla classe MyClass view.show(); return app.exec();
}
@
now,errors are:
..\MyClass\main.cpp:14: error: 'QObject' is an inaccessible base of 'MyClass' -
[quote author="spode" date="1308316198"]
now,errors are:
..\MyClass\main.cpp:14: error: 'QObject' is an inaccessible base of 'MyClass'[/quote]I feel free to quote myself...
[quote author="Lukas Geyer" date="1308303674"]Line 7 of MyClass.h has to read
@
class MyClass : public QObject
@
[/quote] -
this time, why can not i call leggimi()?
@ #include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "funzioni.h"
#include <QDeclarativeContext>
#include <QGraphicsObject>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QmlApplicationViewer viewer; viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("qml/fileTrenner2/main.qml")); viewer.showExpanded(); viewer.setWindowTitle("File Trenner"); viewer.move(600,20); Funzioni funz; QDeclarativeView view(QUrl::fromLocalFile("qml/fileTrenner2/main.qml")); view.rootContext()->setContextProperty("myObject", &funz); return app.exec();
}
@@ //funzioni.cpp
#include "funzioni.h"Funzioni::Funzioni()
{}
void Funzioni::leggimi()
{
QDeclarativeView view(QUrl::fromLocalFile("qml/fileTrenner2/messaggioLeggimi.qml"));
view.show();
}
@@ //funzioni.h
#ifndef FUNZIONI_H
#define FUNZIONI_H
#include <QObject>
#include <QDeclarativeView>
#include <QDeclarativeContext>class Funzioni : public QObject
{
Q_OBJECTpublic:
Funzioni();
int caratteriAriga;public slots:
void leggimi();};
#endif // FUNZIONI_H
@i proved to call leggimi in main.qml by using "Image { width: 100; height: 101 MouseArea { anchors.fill:parent; onClicked: myObject.leggimi() } }". error: qml/fileTrenner2/main.qml:416: ReferenceError: Can't find variable: myObject. whY?
-
I see two problems here.
-
At first, your QML file is processed before the context property is added.
@
QDeclarativeView view(QUrl::fromLocalFile("qml/fileTrenner2/main.qml"));
view.rootContext()->setContextProperty("myObject", &funz);
@ should probably read
@
QDeclarativeView view;
view.rootContext()->setContextProperty("myObject", &funz);
view.setSource(QUrl::fromLocalFile("qml/fileTrenner2/main.qml"));
@ -
At second, you add the context property to the root context of view (the QDeclarativeView) and not viewer (the QmlApplicationViewer, which is in fact just a wrapper around another QDeclarativeView). view is actually never shown, the corresponding code can be removed completely.
-
-
bq. QDeclarativeView view;
view.rootContext()->setContextProperty("myObject", &funz);
view.setSource(QUrl::fromLocalFile("qml/fileTrenner2/main.qml"));thank you. you were very clear, but i do not understand why do you declare the QDeclarativeView making save to explain immediatly the file toward which methods will be applied. i mean: where is the conceptual or tecnical difference between the two snippets?
i will use the QDeclarativeView.
-
The qml is being processed as soon as you call setSource (whether directly or indirectly through the constructor). From the documentation:
[quote]
source : QUrl
This property holds the URL of the source of the QML component.
Changing this property causes the QML component to be reloaded.
[/quote]
Therefore, you have to add any special object and context variables before you use setSource. Otherwise they will not be present when (re)loading the qml. Does that make sense to you now? -
You have to connect the QDeclarativeEngine::quit() signal to the QApplication::quit() slot.
@
connect(view->engine(), SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
@Don't forget to add "[Solved]" to topic titles which have been solved for you.
-
[OFFTOPIC]
I would kindly like to propose to change the word "Reciving" in the title of the thread to "Receiving". :-)
[/OFFTOPIC] -
[quote author="spode" date="1308387674"]so, that was made!
how to choose the solver in this thread?[/quote]Um... what?
If the question is related to this [quote author="Lukas Geyer" date="1308340073"]Don't forget to add "[Solved]" to topic titles which have been solved for you.[/quote] just click the edit button on your first post an change the title from "Receiving signals from QML to C++" to "[Solved] Receiving signals from QML to C++".