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.
Related
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).
Let's say I have a widget that cannot allow a child widget to be displayed on top of it- yet that's the look I need. I'm assuming the only way to accomplish that look would be to make the child widget into its own window.
How can I make that "child" window always on top of the "parent", and position it to always be at the bottom (with a predefined margin) and centered horizontally?
It should respond to resizing of the parent window as well.
A good example would be like a controlbar for video
(in fact- I would guess that in fullscreen mode VLC is essentially doing something like this... is it?)
EDIT: here is my current widgets layout: http://i.imgur.com/NcRLmrd.png
Note that the seekbar is not displaying over the video
The child widget should have Qt::Tool window flag, and parent widget should be set as parent widget of the child as usual. This way it will be a top-level widget and will always be on top of the parent.
You should position the child widget manually. For example, you can install an event filter on the parent widget and react on Move and Resize event types.
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).
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);
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.