PDA

View Full Version : QPropertyAnimation problem: Variable widget height (upwards)



ddonate
20th December 2013, 14:43
Hi,

I am trying to do a QPropertyAnimation but faced some problems.

My target is a main widget with 2 parts: bottom & top, with no spacing between them.

Bottom: fixed size
Top: same width as bottom and animated height but UPWARDS (from 0px, i.e, initially "hidden"). Like a 'popup'

Animation will start with a button:
1st click: Top widget "goes up" from 0px to 100px in top of bottom widget
2nd click: Top widget" goes down" from 100px to 0px in top of bottom widget ("hidden" again)

I have tried using 'height' property, but no success since top widget increased its height downwards

I have tried using 'geometry' property, using negative values since top widget position is referenced to its parent (main widget). This way my top widget go upwards (ok), but main widget does NOT resize, so top widget is NOT seen... I thought I could use 'valueChanged' signal from QPropertyAnimation (top widget height values: 0 -> 100) to change main widget geometry, but no success.

I hope I have explained my problem...

Any idea? Thanks in advance,

Diego

anda_skoa
21st December 2013, 11:16
You most likely have to increase the size of the window itself, i.e. decrease its y value and at the same time increasing its height.

The layout should then resize the top widget accordingly since the other ones is fixed in height.

Cheers,
_

ddonate
23rd December 2013, 14:09
Hi,

Thanks for your response.

I have tried this code:




// TOP FIXED WIDGET ------------------------------------
QLabel* pLabelFixed = new QLabel( "fixed" );
pLabelFixed->setFixedHeight( 50 );
pLabelFixed->setFixedWidth(500);
pLabelFixed->setFrameStyle( QFrame::StyledPanel );

// BOTTOM VARIABLE -------------------------------------
QLabel* pLabelVariable = new QLabel( "variable" );
pLabelVariable->setFrameStyle( QFrame::StyledPanel );
pLabelVariable->resize(500, 0);

// WHOLE WIDGET ---------------------------------------
QWidget* pWidget = new QWidget( NULL, Qt::Popup );
pWidget->setFixedWidth( 500 );

QVBoxLayout* pLayout = new QVBoxLayout();
pLayout->setMargin(0);
pLayout->setSpacing(0);

pLayout->addWidget(pLabelVariable);
pLayout->addWidget(pLabelFixed);
pWidget->setLayout(pLayout);

QPoint pos( 500,500 );
pWidget->move( pos );
pWidget->show();

// ANIMATIONS ------------------------------------------
QPropertyAnimation* pAni = new QPropertyAnimation( pLabelVariable, "minimumHeight" );
pAni->setStartValue( 0 );
pAni->setEndValue( 50 );
pAni->setDuration( 1000 );
pAni->start();

QPropertyAnimation* pAni2 = new QPropertyAnimation( pWidget, "geometry" );
pAni2->setStartValue( QRect( QPoint(500,500), QSize(500,50) ) );
pAni2->setEndValue( QRect( QPoint(500,450), QSize(500,100) ) );
pAni2->setDuration( 1000 );
pAni2->start();


and the effect is what i need... BUT the mixed animation flickers too much. I think it is logic, since 1st animation makes TopWidget to downwards, while 2nd animation forces the whole widget to move upwards. It seems to cause "painting problems"...

Any idea or another solution?

Thanks in advance,

Diego

anda_skoa
23rd December 2013, 14:57
My suggestion would be to change the windows geometry.
That allows you to set a new position (moving up) and a new height.

Since the contents are layouted, they will be resized automatically, i.e. the variable sized label will expand.

Cheers,
_

ddonate
23rd December 2013, 17:36
Hi,

I understand your idea, it seems to be right. But when I try to apply that it does not work for me (altough when animation is finished it looks ok). I change main widget geometry (position & size), but:
- main widget moves a little upwards at the beginning
- variable sized label does not start with height = 0

I supponse I am doing sth wrong.

Animation code:



QPoint pointIni = QPoint(500,500);
QPoint pointEnd = QPoint(500,450);

QSize sizeIni = QSize(300, 50);
QSize sizeEnd = QSize(300, 100);

QPropertyAnimation* pAni2 = new QPropertyAnimation( pWidget, "geometry" );
pAni2->setStartValue( QRect( pointIni, sizeIni ) );
pAni2->setEndValue( QRect( pointEnd, sizeEnd ) );
pAni2->setDuration( 1000 );
pAni2->start();


Thanks a lot,

Diego

anda_skoa
24th December 2013, 14:57
Maybe you are not using the correct values.

See http://qt-project.org/doc/qt-5.0/qtwidgets/application-windows.html#window-geometry for the different geometry types on top level windows.

Cheers,
_