3
votes

I am trying to implement the sleep function of QT Qthread, so I 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 source code what I am doing is after connecting to a database, I want a label to change background color (sort of like a glowing effect), so I tried calling the sleep function from inside a while(true) 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 time-->

/usr/local/Trolltech/Qt-4.8.4/include/QtCore/qthread.h:115: error: ‘static void QThread::sleep(long unsigned int)’ is protected /home/aj/MY_QT_WORK/timer_test/mainwindow.h:22: error: within this context

Any ideas where I am doing wrong ???

3

3 Answers

6
votes

Use sleep() in main thread is bad idea because it blocks all GUI thread. Also Qt Test library is too heavy for production. So try to use QTimer only or try something like:

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

You can find answer to your question here: How do I create a pause/wait function using Qt? . If you are interested in why QThread::sleep() is protected and you can't use it, the answer is very simple. QThread::sleep() is implemented so that it can make delay only in its own thread where it was called. So this method can be used in multithreading application to delay one thread for some time. And to specify which thread exactly you want to delay, you can call this method only from that thread code. That's why it's protected.

4
votes

Never use while(true) or anything similar in your GUI thread. Get rid of the whole idea of using any sort of sleep function and use a timer with states to create your glowy 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 the animateLabel slot somewhere to start your animation. Set the _animState start value before you call it though. You can also create StateStart and StateStop states to make it clearer (and to be able to stop the animation).