Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to restore widget parent
Forum Updated to NodeBB v4.3 + New Features

How to restore widget parent

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 939 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CesarC Offline
    CesarC Offline
    Cesar
    wrote on last edited by Cesar
    #1

    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:

    dd166153-e65e-405a-9a61-77d46355f832-image.png

    As it could be seen in the gif below, before hovering:

    The widget is 'attached' to the GridLayout and inside of the Widget container.

    After the mouse leaves:

    It's no longer attached to the GridLayout / Widget.

    2022-10-08_16-35-49.gif

    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;
    }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @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.

      CesarC 1 Reply Last reply
      0
      • C ChrisW67

        @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.

        CesarC Offline
        CesarC Offline
        Cesar
        wrote on last edited by
        #3

        @ChrisW67 but the red button is inside of the widget not the grid.

        JonBJ 1 Reply Last reply
        0
        • CesarC Cesar

          @ChrisW67 but the red button is inside of the widget not the grid.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Cesar
          ? Your original screenshot is really unhelpful, you have left 3 push buttons named pushButton... and not told us which button is which.... If your red button is pushButton_3 then it is on a widget named widget. The icon indicates that you have set a QGridLayout (via Set Layout) in Designer on widget. Note that we are not talking about grid_Layout2 for this. So if you want to add it back it needs to be placed into that layout on widget.

          CesarC 1 Reply Last reply
          0
          • JonBJ JonB

            @Cesar
            ? Your original screenshot is really unhelpful, you have left 3 push buttons named pushButton... and not told us which button is which.... If your red button is pushButton_3 then it is on a widget named widget. The icon indicates that you have set a QGridLayout (via Set Layout) in Designer on widget. Note that we are not talking about grid_Layout2 for this. So if you want to add it back it needs to be placed into that layout on widget.

            CesarC Offline
            CesarC Offline
            Cesar
            wrote on last edited by
            #5

            @JonB in the picture you can see that the red button is promoted to 'AnimatedButton' so it's the pushButton, he is under a QWidget widget with is also set to Grid Layout.

            I tried setting it first to the gridLayout_2 and then setting the parent to widget but result in the same thin

            layout->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?

            Christian EhrlicherC JonBJ 2 Replies Last reply
            0
            • CesarC Cesar

              @JonB in the picture you can see that the red button is promoted to 'AnimatedButton' so it's the pushButton, he is under a QWidget widget with is also set to Grid Layout.

              I tried setting it first to the gridLayout_2 and then setting the parent to widget but result in the same thin

              layout->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?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • CesarC Cesar

                @JonB in the picture you can see that the red button is promoted to 'AnimatedButton' so it's the pushButton, he is under a QWidget widget with is also set to Grid Layout.

                I tried setting it first to the gridLayout_2 and then setting the parent to widget but result in the same thin

                layout->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?

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @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().

                CesarC 1 Reply Last reply
                0
                • JonBJ JonB

                  @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().

                  CesarC Offline
                  CesarC Offline
                  Cesar
                  wrote on last edited by
                  #8

                  @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.

                  Y 1 Reply Last reply
                  0
                  • CesarC Cesar

                    @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.

                    Y Offline
                    Y Offline
                    Ylvy
                    wrote on last edited by
                    #9

                    @Cesar you could try widgetParent = parentWidget(); before setitng it parent to the centralWidget.

                    CesarC 1 Reply Last reply
                    0
                    • Y Ylvy

                      @Cesar you could try widgetParent = parentWidget(); before setitng it parent to the centralWidget.

                      CesarC Offline
                      CesarC Offline
                      Cesar
                      wrote on last edited by Cesar
                      #10

                      @Ylvy same thing :(
                      the widget isn't 'attached' back to her parent

                      JonBJ 1 Reply Last reply
                      0
                      • CesarC Cesar

                        @Ylvy same thing :(
                        the widget isn't 'attached' back to her parent

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #11

                        @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.

                        Y 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @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.

                          Y Offline
                          Y Offline
                          Ylvy
                          wrote on last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved