Pregunta

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?

¿Fue útil?

Solución 2

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.

Otros consejos

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");

Write this on the first row (remove the quotation marks from the brackets):

QList<QWidget *> allWidgets = mainWindow->findChildren<QWidget *>();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top