AlbertoN
9th October 2014, 16:38
Hi all,
I'd like to animate (show and hide) a widget when a toggle button is pressed.
I wrote this code:
MainWindow::MainWindow
{
[...]
//ui->widget->hide();//[1]
//ui->button->setChecked(false);//[2]
startgeometry = QRect(0,0,0,0);
connect(ui->button,SIGNAL(toggled(bool)),this,SLOT(togglePyShe ll(bool)));
}
void MainWindow::togglePyShell(bool show)
{
QRect formerGeometry = QRect(ui->widget->geometry());
if(startgeometry==QRect(0,0,0,0)){
startgeometry = formerGeometry;
}
if(show){
//ui->widget->show();//[3]
QPropertyAnimation *showAnimation = new QPropertyAnimation(ui->widget, "geometry");
showAnimation->setDuration(100);
showAnimation->setEasingCurve(QEasingCurve::Linear);
showAnimation->setStartValue(formerGeometry);
showAnimation->setEndValue(startgeometry);
showAnimation->start(QPropertyAnimation::DeleteWhenStopped);
}else{
QPropertyAnimation *hideAnimation = new QPropertyAnimation(ui->widget, "geometry");
hideAnimation->setDuration(100);
hideAnimation->setEasingCurve(QEasingCurve::Linear);
hideAnimation->setStartValue(formerGeometry);
QPoint endTopLeftCorner = QPoint(ui->widget->pos() + QPoint(0, ui->widget->height()));
QRect finalGeometry = QRect(endTopLeftCorner, QSize(ui->widget->width(), 0));
hideAnimation->setEndValue(finalGeometry);
hideAnimation->start(QPropertyAnimation::DeleteWhenStopped);
//ui->widget->hide();//[4]
}
}
It works if the widget is visible after costructor (and button pressed), but I'd like to start with the widget invisible (and button not checked).
I don't know how to hide the widget.
If i try to uncomment from [1] up to [4] or subset of them I have strange behaviour, because (i guess) I can't save original widget's size into startgeometry.
Any help is appreciated,
Thanks in advance.
I'd like to animate (show and hide) a widget when a toggle button is pressed.
I wrote this code:
MainWindow::MainWindow
{
[...]
//ui->widget->hide();//[1]
//ui->button->setChecked(false);//[2]
startgeometry = QRect(0,0,0,0);
connect(ui->button,SIGNAL(toggled(bool)),this,SLOT(togglePyShe ll(bool)));
}
void MainWindow::togglePyShell(bool show)
{
QRect formerGeometry = QRect(ui->widget->geometry());
if(startgeometry==QRect(0,0,0,0)){
startgeometry = formerGeometry;
}
if(show){
//ui->widget->show();//[3]
QPropertyAnimation *showAnimation = new QPropertyAnimation(ui->widget, "geometry");
showAnimation->setDuration(100);
showAnimation->setEasingCurve(QEasingCurve::Linear);
showAnimation->setStartValue(formerGeometry);
showAnimation->setEndValue(startgeometry);
showAnimation->start(QPropertyAnimation::DeleteWhenStopped);
}else{
QPropertyAnimation *hideAnimation = new QPropertyAnimation(ui->widget, "geometry");
hideAnimation->setDuration(100);
hideAnimation->setEasingCurve(QEasingCurve::Linear);
hideAnimation->setStartValue(formerGeometry);
QPoint endTopLeftCorner = QPoint(ui->widget->pos() + QPoint(0, ui->widget->height()));
QRect finalGeometry = QRect(endTopLeftCorner, QSize(ui->widget->width(), 0));
hideAnimation->setEndValue(finalGeometry);
hideAnimation->start(QPropertyAnimation::DeleteWhenStopped);
//ui->widget->hide();//[4]
}
}
It works if the widget is visible after costructor (and button pressed), but I'd like to start with the widget invisible (and button not checked).
I don't know how to hide the widget.
If i try to uncomment from [1] up to [4] or subset of them I have strange behaviour, because (i guess) I can't save original widget's size into startgeometry.
Any help is appreciated,
Thanks in advance.