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).
Related
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.
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).
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've got QListWidget as a child inside QPlainTextEdit for purposes of completion suggestions.
Suggestion list is displayed under cursor and when line is almost full, part of list widget is cropped by the border of text edit. Is there any possibility to force child widget show out of parent widget region?
An idea I have, is to make the text and list edit elements "siblings", i. e. add them to the same parent. But this would require my event handling mechanism (as I suppose parent-child relation between those two elements).
I think there must be a way to achieve this, as I studied Katepart component and there is KateCompletionWidget (displays suggestions) that is direct child of KateView and when I run it (e. g. Kate text editor), the list is shown across text edit border.
There's no way for a child to paint outside of a parent's QWidget rect, unfortunately. Your sibling approach is a popular solution to this problem. Another approach is for the child to notify the parent of what it wants to draw, but this can be a bit more advanced because you have to re-implement paint events.
Today I found a possibly viable way -- it's based on setWindowFlags(Qt::ToolTip) method of QListWidget element. There's, however, some slight change in positioning and event handling.
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.