02-05-2013 02:44 AM
Let's say I want to detect the number of checkboxes selected in a Container, how would you do that exactly? Is there a way to get the properties of all child elements?
This has been stumping me all day. Thanks.
Solved! Go to Solution.
02-05-2013 08:53 AM
There are many ways to detect the checked elements. Why don't you examine them in Javascript? If you know how much checkboxes you have in the Container, this is the best way I think. You need to add every checkbox an id, and when an event triggers (ex. button press, navigate forward) it, you check every checkboxes check property in Javascript, like:
onClicked {
var checkedNum = 0;
if (checkBox1.checked) checkedNum++;
if (checkBox2.checked) checkedNum++;
if (checkBox3.checked) checkedNum++;
etc....
}But if you create checkboxes dynamically, you can get the whole QML object list in C++ side.
In the projectName.cpp file is some auto-generated lines:
AbstractPane *root = qml->createRootObject<AbstractPane>(); // set created root object as a scene app->setScene(root);
The root object contains the whole QML hierarchy. You can get it with this command:
QObjectList children = root->children();
So, children is a list, and it contains QObject elements. They can be iterated via a simple for cycle. Every QObject knows it's class name, so with this snippet, you can examine, that the actual QObject is a CheckBox or not.
for (int i = 0; i < children.count(); i++)
{
if (children.at(i)->metaObject()->className() == "CheckBox")
{
//do something
}
}I hope it helps.
cheers,
chriske
02-05-2013 12:42 PM
This was a very helpful explanation. Thank you!