Solved 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(); }
-
You can use for example the size hint to get the estimated final size of your widget.
-
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.
-
You can use for example the size hint to get the estimated final size of your widget.
-
great, fix my little problem, thanks.