Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPropertyAnimation, partially works.



  • Hi guys I have a query about QPropertyAnimation, I have a form with a QTableView where I show data, and then below in a GroupBox some controls, which together are the details, I made an animation to show that GroupBox, when I clicked on a button , it is shown and hidden well, but when you continue clicking it no longer shows anything, any suggestion would be appreciated, greetings.
    I leave my code.

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QSqlDatabase>
    #include <QSqlTableModel>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
      Q_OBJECT
    
    public:
      MainWindow(QWidget *parent = nullptr);
      ~MainWindow();
    
    private slots:
      void on_btnMostrar_clicked();
    
    private:
      Ui::MainWindow *ui;
      QSqlDatabase mDb;
      QSqlTableModel *model;
      int _height=0;
      int endValue=0;
      bool showDetails=false;
    
    };
    #endif // MAINWINDOW_H
    
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSqlError>
    #include <QMessageBox>
    #include <QtDebug>
    #include <QPropertyAnimation>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent), ui(new Ui::MainWindow)
    {
      ui->setupUi(this);
      qInfo()<<mDb.drivers();
      mDb=QSqlDatabase::addDatabase("QPSQL");
      if(!mDb.isDriverAvailable("QPSQL")){
          QMessageBox::critical(this,qApp->applicationName(),mDb.lastError().text());
          return;
        }
      mDb.setPort(5432);
      mDb.setHostName("localhost");
      mDb.setDatabaseName("covid");
      mDb.setUserName("postgres");
      mDb.setPassword("password");
      if(!mDb.open()){
          QMessageBox::critical(this,qApp->applicationName(), mDb.lastError().text());
          return;
        }
      model=new QSqlTableModel(this);
      model->setTable("datos");
      model->select();
      
      ui->tableView->setModel(model);
      ui->gbDatos->setVisible(false);
    //  ui->btnMostrar->setCheckable(true);
    
    }
    
    MainWindow::~MainWindow()
    {
      delete ui;
    }
    void MainWindow::on_btnMostrar_clicked()
    {
      if(showDetails==true)
        _height=this->height();
      else
        _height=this->height()-ui->gbDatos->height();
    
      if(_height==293){
          endValue=this->height();
          showDetails=true;
          ui->gbDatos->setVisible(true);
        }else{
          endValue=293;
          showDetails=false;
          ui->gbDatos->setVisible(false);
        }
    
      QPropertyAnimation *animate=new QPropertyAnimation(this,"minimumHeight");
      animate->setDuration(1000);
      animate->setStartValue(_height);
      animate->setEndValue(endValue);
      animate->setEasingCurve(QEasingCurve::OutQuad);
      animate->start();
    }
    

    a32f0005-4059-4de7-93d7-d9b69e82cf21-image.png

    90ab815f-95ae-48bf-ba4e-3d802b043593-image.png


  • Lifetime Qt Champion

    You can use for example the size hint to get the estimated final size of your widget.


  • Lifetime Qt Champion

    Hi,

    293 looks like a magic number that is going to get you in trouble.

    You should not rely on that kind of value for your animations.



  • @SGaist said in QPropertyAnimation, partially works.:

    293 looks like a magic number that is going to get you in trouble.
    You should not rely on that kind of value for your animations.

    And then what should I use, would be the question.


  • Lifetime Qt Champion

    You can use for example the size hint to get the estimated final size of your widget.



  • great, fix my little problem, thanks.


Log in to reply