Get objectname (as seen from Qt Designer) from QWidget?
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 *>();
Related videos on Youtube
![fortytwo](https://i.stack.imgur.com/rcqqG.png?s=256&g=1)
Comments
-
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' usingsetEnabled(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 aboveif
statement is not working. What do I put there? -
fortytwo over 10 yearsThank you. I actually went about disabling all widgets and enabling the one I want.
-
fortytwo over 10 yearsAs SingerOfTheFall suggested, disabling all widgets and enabling the one I want was easier in my case. But thank you.
-
László Papp over 10 yearsPlease 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 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.