Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT delay not working
QtWS25 Last Chance

QT delay not working

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S swansorter

    hi
    this my code .i want change text and color of the button after delay.
    only 1st delay working but its not taking 2nd delay
    void MainWindow::on_pushButtonSystemswitchon_clicked()
    {

        ui->pushButtonSystemswitchon->setStyleSheet("font-size: 20px;"
                                                    "background: red;");
        ui->pushButtonSystemswitchon->setText("ON");
        ui->pushButtonFeeder->setEnabled(false);
        ui->pushButtonEejecter->setEnabled(false);
    

    QThread::msleep(1000);

        ui->pushButtonFeeder->setStyleSheet("font-size: 20px;"
                                             "background: green;");
        ui->pushButtonFeeder->setText("FEEDER ON");
        FEEDER_button_flag=0;
        FEEDER_check_flag=false;
    

    QThread::msleep(1000);
    ui->pushButtonEejecter->setStyleSheet("font-size: 20px;"
    "background: blue;");
    ui->pushButtonEejecter->setText("EJECTER ON");
    EJECTER_button_flag=0;
    EJECTER_check_flag=false;

        power_button_flag=0;
        power_check_flag=false;
    

    }

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #3

    @swansorter

    For the love of code. Do not use QThread::sleep if you do not know how to use it and what it actually does!


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    3
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #4

      Hi
      It is not good to use QThread::msleep();
      in a GUI program as you completely freeze all drawing and input and
      it not suitable for any kind of delay in any sort of animation.

      In this case, a single shot timer is good for such a delay.

      QTimer::singleShot(1000, [ this]{
      qDebug("Hello from lambda");
      ui->pushButtonFeeder->setStyleSheet("font-size: 20px;""background: green;");
      ui->pushButtonFeeder->setText("FEEDER ON");
      });

      S 1 Reply Last reply
      7
      • S swansorter

        hi
        this my code .i want change text and color of the button after delay.
        only 1st delay working but its not taking 2nd delay
        void MainWindow::on_pushButtonSystemswitchon_clicked()
        {

            ui->pushButtonSystemswitchon->setStyleSheet("font-size: 20px;"
                                                        "background: red;");
            ui->pushButtonSystemswitchon->setText("ON");
            ui->pushButtonFeeder->setEnabled(false);
            ui->pushButtonEejecter->setEnabled(false);
        

        QThread::msleep(1000);

            ui->pushButtonFeeder->setStyleSheet("font-size: 20px;"
                                                 "background: green;");
            ui->pushButtonFeeder->setText("FEEDER ON");
            FEEDER_button_flag=0;
            FEEDER_check_flag=false;
        

        QThread::msleep(1000);
        ui->pushButtonEejecter->setStyleSheet("font-size: 20px;"
        "background: blue;");
        ui->pushButtonEejecter->setText("EJECTER ON");
        EJECTER_button_flag=0;
        EJECTER_check_flag=false;

            power_button_flag=0;
            power_check_flag=false;
        

        }

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @swansorter This is not how you should do this! Don't block your main thread with sleep()!
        Use https://doc.qt.io/qt-5/qtimer.html#singleShot instead

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        4
        • mrjjM mrjj

          Hi
          It is not good to use QThread::msleep();
          in a GUI program as you completely freeze all drawing and input and
          it not suitable for any kind of delay in any sort of animation.

          In this case, a single shot timer is good for such a delay.

          QTimer::singleShot(1000, [ this]{
          qDebug("Hello from lambda");
          ui->pushButtonFeeder->setStyleSheet("font-size: 20px;""background: green;");
          ui->pushButtonFeeder->setText("FEEDER ON");
          });

          S Offline
          S Offline
          swansorter
          wrote on last edited by
          #6

          @mrjj this working for me..but
          1.if i give same delay value it exicute at the same time
          QTimer::singleShot(1000, [ this]{
          qDebug("Hello from lambda");
          ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
          ui->pushButton1->setText("FEEDER ON");
          });
          QTimer::singleShot(1000, [ this]{
          qDebug("Hello from lambda");
          ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
          ui->pushButton2->setText("FEEDER ON");
          });
          2.if give different time value the it works
          QTimer::singleShot(500, [ this]{
          qDebug("Hello from lambda");
          ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
          ui->pushButton1->setText("FEEDER ON");
          });
          QTimer::singleShot(1000, [ this]{
          qDebug("Hello from lambda");
          ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
          ui->pushButton2->setText("FEEDER ON");
          });

          jsulmJ mrjjM 2 Replies Last reply
          0
          • S swansorter

            @mrjj this working for me..but
            1.if i give same delay value it exicute at the same time
            QTimer::singleShot(1000, [ this]{
            qDebug("Hello from lambda");
            ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
            ui->pushButton1->setText("FEEDER ON");
            });
            QTimer::singleShot(1000, [ this]{
            qDebug("Hello from lambda");
            ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
            ui->pushButton2->setText("FEEDER ON");
            });
            2.if give different time value the it works
            QTimer::singleShot(500, [ this]{
            qDebug("Hello from lambda");
            ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
            ui->pushButton1->setText("FEEDER ON");
            });
            QTimer::singleShot(1000, [ this]{
            qDebug("Hello from lambda");
            ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
            ui->pushButton2->setText("FEEDER ON");
            });

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @swansorter said in QT delay not working:

            if i give same delay value it exicute at the same time

            Sure, because you want the second part to be delayed after the first one, right? So, after 1sec delay you do the first change, then again after 1sec more you do the second part.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S swansorter

              @mrjj this working for me..but
              1.if i give same delay value it exicute at the same time
              QTimer::singleShot(1000, [ this]{
              qDebug("Hello from lambda");
              ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
              ui->pushButton1->setText("FEEDER ON");
              });
              QTimer::singleShot(1000, [ this]{
              qDebug("Hello from lambda");
              ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
              ui->pushButton2->setText("FEEDER ON");
              });
              2.if give different time value the it works
              QTimer::singleShot(500, [ this]{
              qDebug("Hello from lambda");
              ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
              ui->pushButton1->setText("FEEDER ON");
              });
              QTimer::singleShot(1000, [ this]{
              qDebug("Hello from lambda");
              ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
              ui->pushButton2->setText("FEEDER ON");
              });

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @swansorter

              Hi
              Yes it is executed when time is gone.

              Im not really sure what you are trying but it seems you want more than one button to change
              color ?

              S 1 Reply Last reply
              0
              • mrjjM mrjj

                @swansorter

                Hi
                Yes it is executed when time is gone.

                Im not really sure what you are trying but it seems you want more than one button to change
                color ?

                S Offline
                S Offline
                swansorter
                wrote on last edited by
                #9

                @mrjj i want like this
                1.ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
                ui->pushButton1->setText("FEEDER ON");
                delay(1000);
                after 1 sec delay
                2.
                ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
                ui->pushButton2->setText("FEEDER ON");
                delay(1000);another 1sec delay
                3.
                ui->pushButton3->setStyleSheet("font-size: 20px;""background: green;");
                ui->pushButton3->setText("FEEDER ON")

                mrjjM 1 Reply Last reply
                0
                • S swansorter

                  @mrjj i want like this
                  1.ui->pushButton1->setStyleSheet("font-size: 20px;""background: green;");
                  ui->pushButton1->setText("FEEDER ON");
                  delay(1000);
                  after 1 sec delay
                  2.
                  ui->pushButton2->setStyleSheet("font-size: 20px;""background: green;");
                  ui->pushButton2->setText("FEEDER ON");
                  delay(1000);another 1sec delay
                  3.
                  ui->pushButton3->setStyleSheet("font-size: 20px;""background: green;");
                  ui->pushButton3->setText("FEEDER ON")

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @swansorter
                  Cant you just use

                  1000
                  2000

                  as delay then ?

                  S 2 Replies Last reply
                  1
                  • CP71C Offline
                    CP71C Offline
                    CP71
                    wrote on last edited by CP71
                    #11

                    Hi,
                    I thing is not a good idea to use Qthread::msleep in this way for GUI, as someone has already suggested you I should use Qtimer::singleShot.

                    Only for test, have you tried with update function before msleep?

                    ui->pushButtonSystemswitchon->setStyleSheet("font-size: 20px;"
                                                                "background: red;");
                    ui->pushButtonSystemswitchon->setText("ON");
                    ui->pushButtonFeeder->setEnabled(false);
                    ui→pushButtonEejecter→setEnabled(false);
                    

                    ui→pushButtonSystemswitchon→update();
                    ui→pushButtonSystemswitchon→update();
                    ui→pushButtonFeeder→update();
                    ui→pushButtonEejecter→update();

                    QThread::msleep(1000);

                    1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @swansorter
                      Cant you just use

                      1000
                      2000

                      as delay then ?

                      S Offline
                      S Offline
                      swansorter
                      wrote on last edited by
                      #12

                      @mrjj am asking is their any other way ?

                      jsulmJ 1 Reply Last reply
                      0
                      • S swansorter

                        @mrjj am asking is their any other way ?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @swansorter Why do you need other way? What is wrong with QTimer::singleShot and delays of 1000 and 2000?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        S 1 Reply Last reply
                        2
                        • jsulmJ jsulm

                          @swansorter Why do you need other way? What is wrong with QTimer::singleShot and delays of 1000 and 2000?

                          S Offline
                          S Offline
                          swansorter
                          wrote on last edited by
                          #14

                          @jsulm i dnt know weather it is correct way or not

                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @swansorter
                            Cant you just use

                            1000
                            2000

                            as delay then ?

                            S Offline
                            S Offline
                            swansorter
                            wrote on last edited by
                            #15

                            @mrjj thank you very much

                            1 Reply Last reply
                            1

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved