简体   繁体   中英

Use multithread in c++ and qml

main.cpp

int main(int argc, char* argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc,argv);
    QQmlApplicationEngine engine;
    BlueToothdevice d;

    engine.rootContext()->setContextProperty("device", &d);
    qDebug()<<"main thread:"<< QThread::currentThreadId();
    engine.load(QUrl(QStringLiteral("qrc:/assets/main.qml")));
    return app.exec();
}

BlueToothdevice.h

class BlueToothdevice : public QObject { balabalabala }

BlueToothdevice.cpp

balabalabala

main.qml

...
Text {
    id: bloodglucoseText2
    text: device.bdsugar
    font.pixelSize: 6 * dpi
}
...

how can I move object "d" defined in main.cpp to another thread? I still want to use object "d" without changing.

Define properties and invocable methods in your Qt class.

// Define the property bdsugar, accessible from Qml
Q_PROPERTY(bdsugar READ getBdSugar WRITE setBdSugar NOTIFY on BdSugarChanged)

// Define the method bdsugar, callable from Qml
Q_INVOKABLE QString bdsugar( return my_bdsugar; ) const;

But it is not necessary to move the object d to another thread (Or I did not understood the question).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM