Time delay using QThread showing build problems

I am trying to implement the QT Qthread sleep function, so I have declared it in the header file as -

namespace Ui {
    class MainWindow;
}
class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    static void sleep(unsigned long secs){QThread::sleep(secs);}
protected:
    void changeEvent(QEvent *e);
private:
    Ui::MainWindow *ui;
private slots:
    void on_pushButton_clicked();
};

      

and in my original code what I am doing, after connecting to the database, I want the label to change its background color (sort of like a glowing effect), so I tried to call the sleep function from within for a while (admittedly) loop.

while(db.open())
{
    MainWindow::sleep(13);

    qDebug()<<"Success ";
    ui->glow_label->setStyleSheet("QLabel {background-color: rgb(0, 255, 0);}");

    MainWindow::sleep(5);
    ui->glow_label->setStyleSheet("QLabel {background-color: rgb(0, 85, 255);}");
}

      

But it shows error during build ->

/usr/local/Trolltech/Qt-4.8.4/include/QtCore/qthread.h:115: error: 'static void QThread :: sleep (long unsigned int) protected / home / aj / MY _QT_WORK / timer_test / mainwindow. h: 22: error: in this context

Any ideas where I'm going wrong ???

+3


source to share


3 answers


It is a bad idea to use sleep () on the main thread because it blocks the entire GUI thread. Also, the Qt Test library is too heavy for production. So try using only QTimer or try something like:



void sleep(qint64 msec)
{
    QEventLoop loop;
    QTimer::singleShot(msec, &loop, SLOT(quit()));
    loop.exec();
}

      

+5


source


Never use while(true)

or anything like that in your GUI. Get rid of the whole idea of ​​using any sleep function and use a stateful timer to create your glowing animation.

enum GlowState{StateGreen, StateBlue}; // declare an enum somewhere in your class header
GlowState _animState; // declare a private member variable somewhere in your header

void MyClass::animateLabel() // create a slot in your class
{
    switch(_animState)
    {
    case StateGreen:
        myLabel->setStyleSheet("QLabel {background-color: rgb(0, 255, 0);}");
        _animState = StateBlue;
        QTimer::singleShot(5, this, SLOT(animateLabel()));
        break;
    case StateBlue:
        myLabel->setStyleSheet("QLabel {background-color: rgb(0, 0, 255);}");
        _animState = StateGreen;
        QTimer::singleShot(13, this, SLOT(animateLabel()));
        break;
    }
}

      



Call a animateLabel

slot somewhere to start the animation. Set the initial value _animState

before you call it. You can also create states StateStart

and StateStop

to make it clearer (and to stop the animation).

+4


source


You can find the answer to your question here: How to create a pause / wait function using Qt? ... If you are wondering why QThread :: sleep () is protected and you cannot use it, the answer is very simple. QThread :: sleep () is implemented so that it can only sleep on its own thread where it was called. Thus, this technique can be used in a multithreaded application to delay one thread for some time. And to specify which thread exactly you want to defer, you can only call this method from that thread code. This is why it is protected.

+3


source







All Articles