views:

49

answers:

1

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("animate()"));
    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();
        }
    }
+4  A: 

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.

Arnold Spence
P.S. Please review the answers for your previous questions to see if there are some you can "accept".
Arnold Spence
Thanks. I understand that there is only one thread (Called the GUI thread) that can modify GUI elements, and like all threads, it has an event loop which delivers the events for it. a.update() posts the event to the GUI's thread's event loop. What is meant by returning to the message loop? From where ? What's this message loop ?
Daud
P.S. I clicked on the tick mark before answers to my previous questions, but still, the accept rate is 0%.
Daud
Sorry for the confustion. The message loop is the event loop, they are just common terms for the same thing. I've edited my answer to use only one of them.
Arnold Spence
Thanks. update() posts the event t main event loop, but the docs say that repaint() does the repainting immediately. But if I replace update() with repaint() in my code, it still makes no difference. Also, why should we use update() or repaint() ? Shouldn't just resetting the geometry of the widget be enough ? After all, setGeometry() calls sendEvent() which notifies the receiver immedialtely ?
Daud