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

15,443

Solution 1

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.

Solution 2

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

Solution 3

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

QList<QWidget *> allWidgets = mainWindow->findChildren<QWidget *>();
Share:
15,443

Related videos on Youtube

fortytwo
Author by

fortytwo

Developer

Updated on June 25, 2022

Comments

  • fortytwo
    fortytwo about 2 years

    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?

  • fortytwo
    fortytwo over 10 years
    Thank you. I actually went about disabling all widgets and enabling the one I want.
  • fortytwo
    fortytwo over 10 years
    As SingerOfTheFall suggested, disabling all widgets and enabling the one I want was easier in my case. But thank you.
  • László Papp
    László Papp over 10 years
    Please use foreach in a Qt code rather than C++11 only features. That is why foreach is still kept around because Qt has to work with pre-c++11 compilers at this point of time.
  • underscore_d
    underscore_d over 8 years
    @lpapp I hugely doubt C++ keeps foreach around just for the sake of one 3rd-party library. Users can program in whichever dialect of C++ they want; they aren't mandated to support pre-11 compilers. Besides, even in 2012, before you posted, Qt was using C++11 features within itself: woboq.com/blog/cpp11-in-qt5.html. I suggest you stop reeling off your own coding opinions as if they were facts everyone else has to obey.