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