Qt : changing the position of a widget quickly in a loop makes it unvisible for the duration of the loop. Why?
I am using a QTimer and connecting its timeout signal to the method animate(). In animate(), I am displaying a QPushButton at different positions using a loop. The problem is that the QPushButton is not visible till the loop is finished. It should be giving the effect of a moving object. The following is Qt Jambi code :
QTimer t=new QTimer();
t.timeout.connect(this,tr("anima开发者_开发问答te()"));
t.setSingleShot(true);
t.start(0);
The following is the animate() function :
void animate()
{
QPushButton a=new QPushButton(new QIcon("../Desktop/images/kya/32x32/down.png"),new String().format("%.1f",browser.page().totalBytes()/1024.0)+" KB");
a.setFont(new QFont("Impact",20,QFont.Weight.Light.value()));
int x,y,bx=130,by=50; //bx and by are the width and height of the pushbutton
a.setEnabled(false);
a.show();
for (x=QApplication.desktop().width()-bx,y=QApplication.desktop().height()/2+80;y<QApplication.desktop().height()-by;y+=5)
{
try{Thread.sleep(100);}catch(Exception e){System.out.println(e);}
a.setGeometry(x,y,bx,by);
a.update(x,y,bx,by);
a.show();
}
}
The call to a.update()
posts a message to Qt's event loop so the GUI cannot update until processing returns to the event loop (after it exits your animate function).
You could setup a timer to fire at the rate at which you want to animate the button and in the slot, increment your variables one step and set the button position. Then the button animation will advance each time the timer fires.
A better solution would be to use the QPropertyAnimation class from The Animation Framework to animate your button.
精彩评论