在 C++ 中循环读取 QML 对象(CheckBox)属性:始终相同的值 [英] Reading QML object (CheckBox) property in a loop in C++: always the same value

查看:73
本文介绍了在 C++ 中循环读取 QML 对象(CheckBox)属性:始终相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的代码来在 C++ 循环中从我的 QML 中读取复选框的值.但是,我总是得到未选中"的值,即使在我用鼠标切换复选框之后也是如此.

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

推荐答案

你有两个不好的做法:

  • 不要从 C++ 访问 QML 元素,因为它可能很危险.

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

你不应该使用while循环,还要考虑最后一个选项线程.

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

在这种情况下,GUI 不是线程安全的,因此从辅助线程访问此信息是危险的.在这种情况下,只需创建一个映射 CheckBox 更改的 QObject:

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:

ma​​in.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"

ma​​in.qml

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

这篇关于在 C++ 中循环读取 QML 对象(CheckBox)属性:始终相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆