1

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
PPP
  • 1,279
  • 1
  • 28
  • 71

1 Answers1

2

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
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Are QML properties 'thread safe'? Because here https://stackoverflow.com/questions/9062189/how-to-modify-a-qml-text-from-c/9064469 it says that I can set them from C++ – PPP Mar 11 '20 at 06:11
  • @LucasZanella No, they are not thread-safe. SO they have many answers that apply bad practices unfortunately. – eyllanesc Mar 11 '20 at 13:35
  • I'm getting https://pastebin.com/uY1MSHZs did I do something wrong? – PPP Mar 11 '20 at 16:46
  • I'm not using qmake, instead I use cmake. I cleaned everything and rebuilt everything but I still get these errors. All the answers about this on stackoverflow seem to rely on people forgetting to put Q_OBJECT, which is not the case here – PPP Mar 11 '20 at 17:01
  • The only difference is that I didn't add `#include "main2.moc"` in the end of my `main2.cpp` because it complains this file does not exist – PPP Mar 11 '20 at 17:12
  • Ok added now, didn't complain, but didn't solve the problem – PPP Mar 11 '20 at 17:13
  • @LucasZanella The question is that from the beginning I point out your bad practices. Explain yourself better, for example I am waiting for an answer to my question: *Why is the while loop necessary?* Besides pointing out *didn't solve the problem* does not help at all, it explains why my code is not the solution. – eyllanesc Mar 11 '20 at 17:34