簡體   English   中英

在除main.cpp之外的.cpp文件中訪問QMLEngine / rootObject屬性

[英]Accessing QMLEngine /rootObject properties in .cpp files other than main.cpp

  1. 我在一個.qml文件中定義的一個面板中有兩個單選按鈕。
  2. 無論是否在另一個QML文件或某些c ++類的.cpp文件中進行檢查,我都需要訪問該屬性。
  3. 我可以在main.cpp中做到這一點

在下面使用這些行

QQmlApplicationEngine engine;    
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 
if(engine.rootObjects().isEmpty())
  return -1;
// Step 1: get access to the root object
QObject *rootObject = engine.rootObjects().first();
QObject *qmlObject_serial_radio = rootObject->findChild<QObject*>("serial_radio");
QObject *qmlObject_tcpip_radio = rootObject->findChild<QObject*>("tcpip_radio");    
// Step 2a: set or get the desired property value for the root object
qDebug() << qmlObject_serial_radio->property("checked");
qDebug() << qmlObject_tcpip_radio->property("checked");

但是我想在其他一些.cpp文件中做同樣的事情。

怎么做?

在C ++中實例化QML對象可能很危險,因為這些項目的生命周期不是直接在C ++中處理的,因此QML可以在不通知項目的情況下消除它們,例如,在創建和刪除頁面的StackView中。

更好的方法是將C ++對象導出到QML:

main.cpp中

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include <QDebug>

class Helper: public QObject{
    Q_OBJECT
    Q_PROPERTY(bool isSerialEnabled READ isSerialEnabled WRITE setIsSerialEnabled NOTIFY isSerialEnabledChanged)
    Q_PROPERTY(bool isTcpIpEnabled READ isTcpIpEnabled  WRITE setIsTcpIpEnabled  NOTIFY isTcpIpEnabledChanged)
public:
    using QObject::QObject;
    bool isSerialEnabled() const{
        return mIsSerialEnabled;
    }
    void setIsSerialEnabled(bool isSerialEnabled){
        if(mIsSerialEnabled == isSerialEnabled) return;
        mIsSerialEnabled = isSerialEnabled;
        emit isSerialEnabledChanged(mIsSerialEnabled);
    }
    bool isTcpIpEnabled () const{
        return mIsTcpIpEnabled ;
    }
    void setIsTcpIpEnabled (bool isTcpIpEnabled ){
        if(mIsTcpIpEnabled  == isTcpIpEnabled ) return;
        mIsTcpIpEnabled = isTcpIpEnabled ;
        emit isTcpIpEnabledChanged(mIsTcpIpEnabled );
    }
signals:
    void isSerialEnabledChanged(bool);
    void isTcpIpEnabledChanged(bool);
private:
    bool mIsSerialEnabled;
    bool mIsTcpIpEnabled ;
};

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    Helper helper;
    // check changes
    QObject::connect(&helper, &Helper::isSerialEnabledChanged, [](bool isSerialEnabled){
        qDebug()<<"isSerialEnabled"<<isSerialEnabled;
    });
    QObject::connect(&helper, &Helper::isTcpIpEnabledChanged, [](bool isSerialEnabled){
        qDebug()<<"isTcpIpEnabledChanged"<<isSerialEnabled;
    });
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("helper", &helper);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();
}

#include "main.moc"

main.qml

RadioButton {
    text: "Serial"
    onCheckedChanged: helper.isSerialEnabled = checked
}
RadioButton {
    text: "tcpip"
    onCheckedChanged: helper.isTcpIpEnabled = checked
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM