Reading QML object (CheckBox) property in a loop in C++: always the same value

孤人 提交于 2021-01-28 20:46:06

问题


I made a simple code to read the value of a checkbox from my QML in a C++ loop. However, I always get "unchecked" value, even after I toggle the checkbox with the mouse.

QML:

CheckBox {
    objectName: "simulatorCheckbox"
    text: "text"
}

C++:

QObject *rootObject = engine.rootObjects().first();
QObject *simulatorCheckboxQ = rootObject->findChild<QObject*>("simulatorCheckbox");
if (!simulatorCheckboxQ) {
    std::cout << "simulatorCheckboxQ not found" << std::endl;
    std::exit(1);
}

auto consume = [&simulatorCheckboxQ]() {
    while(true) {
        QVariant simulatorCheckboxState = simulatorCheckboxQ->property("checkedState");
        int simulatorCheckboxStateInt = simulatorCheckboxState.toInt();
        if (simulatorCheckboxStateInt==Qt::Unchecked) {
            std::cout << "UNchecked!" << std::endl;
        } else if (simulatorCheckboxStateInt==Qt::Checked) {
            std::cout << "checked!" << std::endl;
        } else if (simulatorCheckboxStateInt==Qt::PartiallyChecked) {
            std::cout << "PARTIALLY checked!" << std::endl;
        }
        //delay...
    }
};
//run consume as thread

回答1:


You have 2 bad practices:

  • Do not access QML elements from C++ as it can be dangerous.

  • You should not use while-loop, also consider the last option threads.

In this case, the GUI is not thread-safe, so it is dangerous to access this information from a secondary thread. In this case, just create a QObject that maps the CheckBox changes:

main.cpp

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

class CheckBoxMapper: public QObject{
    Q_OBJECT
    Q_PROPERTY(Qt::CheckState state READ state WRITE setState NOTIFY stateChanged)
public:
    using QObject::QObject;
    Qt::CheckState state() const{
        return m_state;
    }
public slots:
    void setState(Qt::CheckState state){
        if (m_state == state)
            return;
        m_state = state;
        emit stateChanged(m_state);
    }
signals:
    void stateChanged(Qt::CheckState state);
private:
        Qt::CheckState m_state;
};

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    CheckBoxMapper checkboxMapper;
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("checkboxMapper", &checkboxMapper);
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    QObject::connect(&checkboxMapper, &CheckBoxMapper::stateChanged, [](Qt::CheckState state){
        qDebug() << state;
    });

    return app.exec();
}
#include "main.moc"

main.qml

CheckBox {
    text: "text"
    onCheckStateChanged: checkboxMapper.state = checkState
    Component.onCompleted: checkboxMapper.state = checkState
}


来源:https://stackoverflow.com/questions/60628889/reading-qml-object-checkbox-property-in-a-loop-in-c-always-the-same-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!