Get objectname (as seen from Qt Designer) from QWidget?

fortytwo picture fortytwo · Nov 26, 2013 · Viewed 13.8k times · Source

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?

Answer

Shoe picture Shoe · Nov 26, 2013

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