Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 893
fortytwo Avatar asked Nov 26 '13 05:11

fortytwo


People also ask

What is QWidget in Qt?

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.

How do I add a layout to QWidget?

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.

How do you get children of QWidget?

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 .


2 Answers

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");
like image 194
Shoe Avatar answered Sep 20 '22 22:09

Shoe


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.

like image 25
SingerOfTheFall Avatar answered Sep 16 '22 22:09

SingerOfTheFall