I am trying to make an app that would use QSerialPort and QtQuick so I need to connect desktop-like app to QML somehow. I managed to (ok i copied and made some changes) send information from QML to main.cpp but I can't send anything the other way. The wanted effect is to manage everything from .cpp, add and delete ListElements and send parameters to draw a graph.
main.cpp
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QQuickView view(QUrl::fromLocalFile("path/main.qml"));
QObject *item = view.rootObject();
MyClass myClass,mySecondClass;
QObject::connect(item, SIGNAL(qmlSignal(QVariant)),
&myClass, SLOT(cppSlot(QVariant)));
QObject::connect(&myClass, SIGNAL(Nazwa(QVariant)),
item, SLOT(onNazwa(QVariant)));
QVariant c=200;
emit myClass.Nazwa(c);
view.show();
return app.exec();
}
myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include <QDebug>
#include <QQuickItem>
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = 0);
signals:
void Nazwa(QVariant a);
public slots:
void cppSlot(const QVariant &v) {
qDebug() << "QVariant :):" << v;
QQuickItem *item =
qobject_cast<QQuickItem*>(v.value<QObject*>());
qDebug() << "Wymiary:" << item->width()
<< item->height();
}
};
#endif // MYCLASS_H
main.qml
import QtQuick.Window 2.0
import QtQuick 2.0
Item {
id: item
width: 100; height: 100
// Item{
// id: item2
// onNazwa: { }
// }
signal qmlSignal(var anObject)
MouseArea {
anchors.fill: parent
onClicked: {
parent.width=200;
item.qmlSignal(item)
}
}
}
Also any changes to main.qml would break the 1st connection and I don't even know why. Can You give me some advise or an example? I spent 2nd day on Qt Documentation and I still can't do it :(