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.7k 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.
  • 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

                      15/15

                      17 Jul 2019, 08:51

                      • Login

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