Hallo, ich würde gerne die erzeugte MyQMLType mit einer weiteren C++
Instanz verbinden, welche in der main.cpp erstellt wurde, die C++
Instanz benötigt keine GUI.
Anhand der Beschreibung
(https://felgo.com/cross-platform-development/how-to-expose-a-qt-cpp-class-with-signals-and-slots-to-qml)
konnte ich erfolgreich die neue QMLType benutzen und die Signals, Slots
und Q_PROPERTY klappen wunderbar.
h Datei:
1 | #ifndef MYQMLTYPE_H
|
2 | #define MYQMLTYPE_H
|
3 |
|
4 | #include <QObject>
|
5 |
|
6 | class MyQMLType : public QObject
|
7 | {
|
8 | Q_OBJECT
|
9 | Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged) // this makes message available as a QML property
|
10 |
|
11 | public:
|
12 | MyQMLType();
|
13 |
|
14 | public slots: // slots are public methods available in QML
|
15 | int increment(int value);
|
16 |
|
17 | signals:
|
18 | void messageChanged();
|
19 |
|
20 | public:
|
21 | QString message() const;
|
22 | void setMessage(const QString& value);
|
23 |
|
24 | private:
|
25 | QString m_message;
|
26 |
|
27 | };
|
28 |
|
29 | #endif // MYQMLTYPE_H
|
C++ Datei:
1 | #include "myqmltype.h"
|
2 |
|
3 | MyQMLType::MyQMLType() : m_message("")
|
4 | {
|
5 |
|
6 | }
|
7 |
|
8 | int MyQMLType::increment(int value) {
|
9 | return value + 1;
|
10 | }
|
11 |
|
12 | QString MyQMLType::message() const {
|
13 | return m_message;
|
14 | }
|
15 |
|
16 | void MyQMLType::setMessage(const QString& value) {
|
17 | if(m_message != value) {
|
18 | m_message = value;
|
19 | messageChanged(); // trigger signal of property change
|
20 | }
|
21 | }
|
main.cpp Datei:
1 | int main(int argc, char *argv[])
|
2 | {
|
3 | // ...
|
4 |
|
5 | // register a QML type made with C++
|
6 | qmlRegisterType<MyQMLType>("com.yourcompany.xyz", 1, 0, "MyQMLType"); // MyQMLType will be usable with: import com.yourcompany.xyz 1.0
|
7 |
|
8 | engine.load(QUrl(vplay.mainQmlFileName()));
|
9 | return app.exec();
|
10 | }
|
Wie verbinde ich jetzt die MyQMLType mit einer weiteren reinen C++
Objektinstanz (mysuperclass)? Eventuell ist mein Verständnis falsch,
aber nach meiner Auffassung kann ich mehrere Objekte über Signal & Slots
verbinden.
1 | int main(int argc, char *argv[])
|
2 | {
|
3 | // ...
|
4 |
|
5 | // register a QML type made with C++
|
6 | qmlRegisterType<MyQMLType>("com.yourcompany.xyz", 1, 0, "MyQMLType"); // MyQMLType will be usable with: import com.yourcompany.xyz 1.0
|
7 |
|
8 | MySuperClass mysuperclass;
|
9 |
|
10 | connect ??
|
11 |
|
12 | engine.load(QUrl(vplay.mainQmlFileName()));
|
13 | return app.exec();
|
14 | }
|