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
Forum Updated to NodeBB v4.3 + New Features

QT delay not working

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 1.8k Views 3 Watching
  • 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
    17 Jul 2019, 06:43

    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 Offline
    J Offline
    J.Hilk
    Moderators
    wrote on 17 Jul 2019, 06:54 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 17 Jul 2019, 06:55 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 17 Jul 2019, 07:45
      7
      • S swansorter
        17 Jul 2019, 06:43

        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 Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 17 Jul 2019, 06:58 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
        • M mrjj
          17 Jul 2019, 06:55

          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 17 Jul 2019, 07:45 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");
          });

          J M 2 Replies Last reply 17 Jul 2019, 07:52
          0
          • S swansorter
            17 Jul 2019, 07:45

            @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");
            });

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 17 Jul 2019, 07:52 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
              17 Jul 2019, 07:45

              @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");
              });

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 17 Jul 2019, 07:55 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 17 Jul 2019, 07:59
              0
              • M mrjj
                17 Jul 2019, 07:55

                @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 17 Jul 2019, 07:59 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")

                M 1 Reply Last reply 17 Jul 2019, 08:02
                0
                • S swansorter
                  17 Jul 2019, 07:59

                  @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")

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 17 Jul 2019, 08:02 last edited by
                  #10

                  @swansorter
                  Cant you just use

                  1000
                  2000

                  as delay then ?

                  S 2 Replies Last reply 17 Jul 2019, 08:40
                  1
                  • C Offline
                    C Offline
                    CP71
                    wrote on 17 Jul 2019, 08:14 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
                    • M mrjj
                      17 Jul 2019, 08:02

                      @swansorter
                      Cant you just use

                      1000
                      2000

                      as delay then ?

                      S Offline
                      S Offline
                      swansorter
                      wrote on 17 Jul 2019, 08:40 last edited by
                      #12

                      @mrjj am asking is their any other way ?

                      J 1 Reply Last reply 17 Jul 2019, 08:44
                      0
                      • S swansorter
                        17 Jul 2019, 08:40

                        @mrjj am asking is their any other way ?

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 17 Jul 2019, 08:44 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 17 Jul 2019, 08:48
                        2
                        • J jsulm
                          17 Jul 2019, 08:44

                          @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 17 Jul 2019, 08:48 last edited by
                          #14

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

                          1 Reply Last reply
                          0
                          • M mrjj
                            17 Jul 2019, 08:02

                            @swansorter
                            Cant you just use

                            1000
                            2000

                            as delay then ?

                            S Offline
                            S Offline
                            swansorter
                            wrote on 17 Jul 2019, 08:51 last edited by
                            #15

                            @mrjj thank you very much

                            1 Reply Last reply
                            1

                            12/15

                            17 Jul 2019, 08:40

                            • Login

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