I want to disable all but a selected set of widgets in my Qt application.
What I am trying to do is to iterate all children of mainWindow using findChildren
and disable all the resulting widgets except 'myTable' using setEnabled(false)
.
QList<QWidget *> allWidgets = mainWindow->findChildren<QWidget *>("");
QList<QWidget*>::iterator it;
for (it = allWidgets.begin(); it != allWidgets.end(); it++) {
if ((*it)->objectName() != "myTable") // here, objectName is not working!!
{
(*it)->setEnabled(false);
}
}
objectName()
inside the above if
statement is not working. What do I put there?
Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.
Layouts can be applied on QWidgets only. What you have to do is to insert a widget and apply the new layout to the widget. Tab widgets can contain the layout to align all items in it.
You can use the findChild() function with the object name to get a specific child. You can also use findChildren() to get all the children that have the same name and then iterate through the list using foreach() or QListIterator .
The objectName
function does not return the class name or the variable name, but the actual object name you have set with QObject::setObjectName
. Therefore you first need to set it in your table with:
myTable->setObjectName("myTable");
You could use the accessibleName
property. Set it for the widget you need, and then check it in your cycle with acessibleName()
function. It is an empty string by default, so it should be fairly easy to find your widget.
Another alternative is to disable all widgets, and then just enable the one you need directly:
for( QWidget * w : widgets )
{
w->setEnabled(false);
}
ui->myTable->setEnabled(true);
Or, finally, you can set the objectName
property with the setObjectName()
function, and use it as you do in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With