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 can i expand and collapse part of my form with QPropertyAnimation
QtWS25 Last Chance

How can i expand and collapse part of my form with QPropertyAnimation

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 4.3k 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.
  • L Offline
    L Offline
    lincoln
    wrote on 15 Jun 2021, 20:09 last edited by lincoln
    #1

    Hello guys I want is to show and collapse the groupBox by clicking on the button; so far it works, but i think its not the correct way to do.

    The animation looks very bad when the form is expanded and when it is contracted it is a little more normal.

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QPropertyAnimation>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Widget; }
    QT_END_NAMESPACE
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
    
    private slots:
        void on_pushButton_clicked();
    
    
    private:
        Ui::Widget *ui;
        QPropertyAnimation *animation;
        bool expand=false;
        int height;
    };
    #endif // WIDGET_H
    
    
    #include "widget.h"
    #include "./ui_widget.h"
    #include <QDebug>
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent), ui(new Ui::Widget)
    {
        ui->setupUi(this);
        ui->pushButton->setText("expand");
        height=this->size().height();
        ui->groupBox->setVisible(false);
    
    
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    
    void Widget::on_pushButton_clicked()
    {
    
        if(!expand){
    
            animation=new QPropertyAnimation(this,"size");
            animation->setDuration(250);
            animation->setEndValue(QSize(this->width(),height));
            animation->setEasingCurve(QEasingCurve::InQuad);
            animation->start();
            ui->groupBox->setVisible(true);
            expand=true;
            ui->pushButton->setText("Shrink");
    
    
        }else{
            ui->groupBox->setVisible(false);
            animation=new QPropertyAnimation(this,"size");
            animation->setDuration(250);
            animation->setEndValue(QSize(this->width(),(height-ui->groupBox->size().height())));
            animation->setEasingCurve(QEasingCurve::InQuad);
            animation->start();
            expand=false;
            ui->pushButton->setText("Expand");
    
    
        }
    
    }
    
    
    

    1b8f327f-e399-499e-b033-bb8502cc36bf-image.png

    Solitary wolf

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Jun 2021, 20:19 last edited by
      #2

      Hi,

      One issue you have is your reference height. Widgets get an actual size when they are shown not when they are constructed.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • L Offline
        L Offline
        lincoln
        wrote on 15 Jun 2021, 21:28 last edited by
        #3

        Anyway, the animation looks very rough, when expanding the form, but when contracting it looks good.

        Solitary wolf

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 15 Jun 2021, 21:47 last edited by
          #4

          Is this what you are looking for? https://stackoverflow.com/questions/32476006/how-to-make-an-expandable-collapsable-section-widget-in-qt

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lincoln
            wrote on 16 Jun 2021, 18:47 last edited by
            #5

            Thanks for your answer, I did the example but nothing is shown

            859f68fc-0046-487e-9c3e-948fb1a1e607-image.png

            in action:

            1da0348f-a9fd-456d-834f-8eaf2f08ed3f-image.png

            my code:

            #include "widget.h"
            #include "./ui_widget.h"
            #include "spoiler.h"
            
            Widget::Widget(QWidget *parent)
                : QWidget(parent)
                , ui(new Ui::Widget)
            {
                ui->setupUi(this);
                Spoiler spoiler;
                spoiler.setContentLayout(*ui->groupBox->layout());
            }
            
            Widget::~Widget()
            {
                delete ui;
            }
            
            
            

            Solitary wolf

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 16 Jun 2021, 19:44 last edited by
              #6

              Spoiler is destroyed as soon as your constructor finishes.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              6/6

              16 Jun 2021, 19:44

              • Login

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