Search code examples
c++qtqt4qt4.8

How can I set the background with a usleep()?


I call the function of background two times with two different backgrounds. I setup usleep(1000) but does not work. My system is linux and I am run Qt 4.8.

main.cpp

MainWindow w;
w.setBg('A');
usleep(1000);
w.setBg('B');

In the mainwindow.cpp there is setBg(char c) with switch to select between two backgrounds.

How can I call the first switch with usleep then switch to the other?

Should I reload my widget?


Solution

  • The tasks that block the inner loop of the GUI are not correct for this type of tasks, as in your case it is usleep, it is advisable to use a QTimer as shown below:

    /*Qt5*/
    MainWindow w;
    w.setBg('A');
    w.show();
    
    QTimer::singleShot(1 /*in ms*/, [&w](){
        w.setBg('B');
    });
    
    /*Qt4*/
    /*on MainWindow*/
    
    private slots:
        void mySlot(){
            setBg('B');
        }
    /*constructor*/
        QTimer::singleShot(1000, this, SLOT(mySlot()));