Setting QWidget to maximize within parent in Qt Creator/Designer? - qt

I have dragged and dropped a widget within a parent widget in the designer within qt creator.
It has an arbitrary fixed position rectangle within its parent.
I want to modify it such that it will conform to its parents dimensions. That is it should be "maximized" within its parent.
What button in the designer do I press to do this?

This is what layouts are for. See this for an explanation.
I'm not particularly familiar with Qt Designer, but the general process is to create a layout (QHBoxLayout would work for you) for the parent widget, and add the child widget to that layout.
Another thing to consider is that, in your case, you could simply set the child widget to be the central widget of the QMainWindow:
this->setCentralWidget(theMaximizedWidget);

Related

Qt: Delete parent widget while retaining the child?

I have a Qt parent widget that displays a child widget.
I need to delete the parent widget.
I might (or not) later attach the child to a different parent and display it inside the new container.
I do not have access to the second parent at the time I delete the first parent.
How do I do this?
First hide the child widget using hide() or setVisible(false) methods of QWidget. Then make the child widget have no parent using setParent(nullptr), which makes it a top level window, which is why you needed to hide it first.
To later re-use the widget, add it to appropriate new layout, which also sets it parent. You need to call its show() or setVisible(true) method for it too, or it will stay hidden.

Is it possible to add QWidget to layout but don't remove with parent?

I don't want my widget to be deleted automatically with parent - is it possible?
Maybe it's possible to add widget to layout without setting parent?
If I do widget->setParent(nullptr); after adding to layout my widget simply doesn't appear :(
For Qt widgets, the notion of parent/child is tightly coupled with the way widgets are drawn an displayed on screen. You cannot have a widget in the layout of another widget without having them being child and parent.
Now if what you want is only to prevent the child widget (or any class inheriting QObject) to be deleted when its parent is deleted, you could listen to the QObject::destroyed() signal on the parent and remove the parent/child relationship when the signal fires (be sure to use direct or blocking connection).

Qt childs are visible but parent not

I have some widgets, QToolButtons to be exactly, and I initialize them like this:
QFrame *frmBackground = new QFrame(ui->centralWidget);
QToolButton *btnMenueExit = new QToolButton(frmBackground);
But now my problem. When I call frmBackground->setVisible(false), the child should disappear too, but thats not the case. The children are still visible and I would have to call setVisible(false) for every child. It's not like I can't do this, but I think I miss something essential about the concept of parent and child.
All of the widgets are organized in the same QGridLayout.
What concerns me is, that if I make the child<-->parent relationship in the designer, with dropping the child into the parent widget, the child is disappearing when I call parent->setVisible(false);
Are there some other parameters I have to set to make these properties to be passed to the child, like a property binding?
QLayout takes ownership of the widget, when you add one with addWidget(). Using parent argument in widget constructor is not necessary.
Setting one widget to be direct parent of another is not a good practice, you should always use layouts.
If you want to use a QWidget to hold child widgets you will usually want to add a layout to the parent QWidget.
setVisible() propagates to all children. If it doesn't work for child widgets then the widget is not their parent (anymore).

Qt widget com problem

I have a Rectangle widget, and a resize box widget, and a text edit widget. How can I compose them without using layouts ? I want all of them as a single widget.
The documentation for how to do a manual layout is in Qt Layout Management here.
Are you sure you can't use one of the standard layouts? You can make a custom widget with three children positioned inside using a layout (or layouts) and your new custom widget will still be just a single widget.

finding the top-level widget on a non-activewindow in qt

I'm trying to find the top-level widget on a non-active window. But I do not want to use QApplication::topLevelAt() method.
I have a main window and several child widget's of main window's central widget. Is it possible to distinguish whether one of the child widget is on top of other childs?
Sami
QObject::children() lists the children of a given object. For QWidgets, the children are drawn in order of the list, which means that you can get the top-most (last-drawn) child with the following code:
QWidget *topmost_p = qobject_cast<QWidget*>(parent_p->children().last());
Be aware that the above code may fail if the parent has no children.

Resources