I’m working on an app, where the entire gui is QML, I’m using C++ QT just to connect the QML to the rest of my app. It’s coming along, but I keep running into little road blocks, the latest of which is connecting a signal from a registered class (using qmlRegisterType) to my QML.
Here is the relavant code from my gui.cpp:
QGuiApplication *q_app_ptr;
void gui_Init(int argc, char **argv) {
static QGuiApplication q_app(argc, argv);
q_app_ptr = &q_app;
// Prepare QT C++ for import in QML
qmlRegisterType<QmlConnect>("QmlConnect", 1, 0, "QmlConnect");
// Start QML app
static QQuickView view;
view.setSource(QUrl::fromLocalFile("gui.qml"));
view.show();
}
And of course later in the program I have q_app_ptr->exec();
Then the relavant code from my gui.h:
class QmlConnect : public QObject {
Q_OBJECT
Q_PROPERTY(QStringList getData READ getData NOTIFY dataChanged)
public:
QmlConnect(QObject *parent = 0){}
~QmlConnect(){}
QStringList getData() {
static QStringList data_list;
data_list << "Data1" << "Data2" << "Data3" << "Data4";
return data_list;
}
signals:
void dataChanged();
};
And here is my gui.qml:
import QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1 as ListItem
import QmlConnect 1.0
MainView {
objectName: "mainView"
// applicationName: "My App"
id: root
width: units.gu(100)
height: units.gu(80)
property real margins: units.gu(2)
property real buttonWidth: units.gu(9)
// Instantiate QmlConnect
QmlConnect {
id: qmlConnect
onDataChanged: {
console.log("DATA LIST CHANGED\n");
}
// Component.onCompleted:
}
Column {
id: libraryColumn
ListItem.Header {text: "Library"}
ListItem.Standard {text: "List1"}
ListItem.Standard {text: "List2"}
ListItem.Divider {}
ListItem.Header {text: "Lists"}
width: units.gu(10)
anchors {
left: parent.left
top: parent.top
bottom: parent.bottom
}
}
Grid {
id: browserGrid
Component.onCompleted: {
var data_list = qmlConnect.getData;
// for(var i = 0; i < data_list.length; i++) {
// console.log(data_list[i]);
// }
}
}
}
Then later in my app I have:
QmlConnect qmlConnect;
emit qmlConnect.dataChanged();
Now when I uncomment
for(var i = 0; i < data_list.length; i++) {
console.log(data_list[i]);
}
It successfully prints the data (of course it gets stuck in a loop printing it, but I’m not really worried about that right now, that was just to see if it was successfully importing the registered class and able to call a function from it). But never once is “DATA LIST CHANGED\n” printed, however if I put printf(“TEST\n”); after emit qmlConnect.dataChanged();, it prints “TEST\n” so I know that part of the code is running.
Does emit qmlConnect.dataChanged(); have to appear after exec()??? Because I have that at the very end of my program, and the emit is happening before exec() is called.
↧