How to restore widget parent
-
I'm trying to animate a widget that is inside a grid and a container, whenever hovered it is animated being expanded horizontally.
As the widget is inside of a container it gets cut because it cant expand above the parent widget area.
What I thought: when the widget is hovered, the code changes her parent to the
centralWidget
then it can expand above other widgets, and when it's no longer hovered restore her 'original' parent.An example:
As it could be seen in the gif below, before hovering:
The widget is 'attached' to the
GridLayout
and inside of theWidget
container.After the mouse leaves:
It's no longer attached to the
GridLayout
/Widget
.I tried to 'restore' the widget to her parent with:
this->setParent(widgetParent);
But it didn't work or I may be doing something wrong.
I could not think of another way of doing all this (expanding a widget inside of a container above her parent limits), any different suggestion is welcome.
class AnimatedButton : public QPushButton { Q_OBJECT public: QPropertyAnimation* anim; struct WidgetPos { int x = 0; int y = 0; int w = 0; int h = 0; }; WidgetPos wp; QWidget* widgetParent; QWidget* centralwidget; void CreateAnimation(QByteArray propertyName) { if (propertyName == "geometry") { anim = new QPropertyAnimation(this, propertyName); this->anim->setDuration(100); this->anim->setEasingCurve(QEasingCurve::Linear); this->wp.x = this->x(); this->wp.y = this->y(); this->wp.w = this->width(); this->wp.h = this->height(); } } AnimatedButton(QWidget* parent = 0) : QPushButton(parent) { widgetParent = parent; this->installEventFilter(this); } bool eventFilter(QObject* obj, QEvent* event) { if (event->type() == QEvent::Enter) { if (!this->wp.x) this->CreateAnimation("geometry"); // setParent to the central widget so the widget can resize above her default parent area. this->setParent(this->centralwidget); this->show(); this->anim->stop(); this->anim->setStartValue( QRect(this->x(), this->y(), this->width(), this->height())); this->anim->setEndValue( QRect(this->x(), this->y(), (this->wp.w + 200) - this->width(), this->height())); this->anim->start(); } else if (event->type() == QEvent::Leave) { this->anim->stop(); this->anim->setStartValue( QRect(this->x(), this->y(), (this->wp.w + 200) - this->width(), this->height())); this->anim->setEndValue( QRect(this->wp.x, this->wp.y, this->wp.w, this->wp.h)); this->anim->start(); // Restore default parent (--------NOT WORKING---------) this->setParent(widgetParent); this->show(); } return QWidget::eventFilter(obj, event); } }; QtWidgetsApplication::QtWidgetsApplication(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); // Pass the central widget to the AnimatedButton class. ui.pushButton->centralwidget = ui.centralWidget; return; }
-
@Cesar said in How to restore widget parent:
the code changes her default parent to the central widget so it can be resized above the other widgets.
By changing the red button's parent you are removing it from the layout. Resetting the red widget's parent does not put it back in the layout. Put it back in the grid layout.
-
@Cesar
? Your original screenshot is really unhelpful, you have left 3 push buttons namedpushButton...
and not told us which button is which.... If your red button ispushButton_3
then it is on a widget namedwidget
. The icon indicates that you have set aQGridLayout
(via Set Layout) in Designer onwidget
. Note that we are not talking aboutgrid_Layout2
for this. So if you want to add it back it needs to be placed into that layout onwidget
. -
@JonB in the picture you can see that the red button is promoted to 'AnimatedButton' so it's the
pushButton
, he is under aQWidget
widget
with is also set toGrid Layout
.I tried setting it first to the
gridLayout_2
and then setting the parent towidget
but result in the same thinlayout->addWidget(this, 0, 0, 1, 1); this->setParent(default_parent); this->show();
@JonB said in How to restore widget parent:
So if you want to add it back it needs to be placed into that layout on
widget
.could you give an example?
-
@Cesar said in How to restore widget parent:
layout->addWidget(this, 0, 0, 1, 1);
this->show();This is just plain stupid - addWidget() adds the widget to the layout and sets the parent to the one where the layout is set to. Afterwards you directly set a new parent (and therefore remove it from the layout again) - what should this do?
-
@Cesar said in How to restore widget parent:
So if you want to add it back it needs to be placed into that layout on widget.
could you give an example?
An example of adding a widget to a layout? You know how to do that:
layout->addWidget(...)
.
Accessing the layout on a widget?widget->layout()
. -
@Christian-Ehrlicher said in How to restore widget parent:
This is just plain stupid - addWidget() adds the widget to the layout and sets the parent to the one where the layout is set to. Afterwards you directly set a new parent (and therefore remove it from the layout again) - what should this do?
Well, it can be stupid to you that know how it works, but I'm having difficulty in understand how widgets/layout works.
@JonB I'm talking about an example of when hovering a widget setting he parent to the
central widget
and on exit hover set him back parent to whatever he was. -
@Cesar
So far as I understand, you are in the same position as you have been before. To get a widget back to exactly where it was, it is not enough to reset its parent. (Than on its own will probably place at the top left of the parent widget?) You also need to place it back wherever it was before on the parent's layout, isn't that the case for your situation? And you are not doing that.