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. QTimer doesn't work the expected way
Forum Update on Monday, May 27th 2025

QTimer doesn't work the expected way

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 4 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hi I have a save button. I want to make it so that when a user clicks it, it gets disabled immediately and a text saying 'Saved." appears. The text disappears after 750 ms. So I coded it like this.

    But after running the program, I found that it actually worked this way.
    The program pauses for 750 ms. After that the save button gets disabled and the text blinks for a moment.
    What's the problem?

    ui->Save->setEnabled(false);
    ui->saved->show();
    QTimer::singleShot(750, [=](){ui->saved->hide();});
    
    jsulmJ 1 Reply Last reply
    0
    • M MasterBlade

      Hi I have a save button. I want to make it so that when a user clicks it, it gets disabled immediately and a text saying 'Saved." appears. The text disappears after 750 ms. So I coded it like this.

      But after running the program, I found that it actually worked this way.
      The program pauses for 750 ms. After that the save button gets disabled and the text blinks for a moment.
      What's the problem?

      ui->Save->setEnabled(false);
      ui->saved->show();
      QTimer::singleShot(750, [=](){ui->saved->hide();});
      
      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MasterBlade said in QTimer doesn't work the expected way:

      The program pauses for 750 ms.

      I would say you need to post more of your code. singleShot should not block your app.

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

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @MasterBlade said in QTimer doesn't work the expected way:

        The program pauses for 750 ms.

        I would say you need to post more of your code. singleShot should not block your app.

        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #3

        @jsulm said in QTimer doesn't work the expected way:

        @MasterBlade said in QTimer doesn't work the expected way:

        The program pauses for 750 ms.

        I would say you need to post more of your code. singleShot should not block your app.

        I think you are correct. I commented all other codes and the program worked as expected.
        But I didn't find any problems with those commented codes. I can post them here. They are mainly used to write INI files.

        void Database::on_Save_clicked()
        {
            if (ui->Numbers->property("no1").toInt() == 0){
                qDebug() << "Trying to Save an Empty Card on_Save_clicked().";
                return;
            }
            QSettings set("*PATH*", QSettings::IniFormat);
            set.beginGroup(ui->Numbers->text());
        
            set.setValue("no1", ui->Numbers->property("no1"));
            /* All are setValue() */
            set.setValue("Type", ui->Types->text());
        
            QStringList sublist = ui->Types->property("SubList").toStringList();
            if (sublist.size() < 2){
                set.setValue("SubList", "");
            }
            else{
                set.setValue("SubList", ui->Types->property("SubList"));
            }
            UpdateTypes();
        
            set.setValue("Cost", ui->cost_value->text());
            /* All are setValue() */
            set.setValue("ResComm", ui->Restrictions->property("Comm").toInt());
        
            set.endGroup();
        
            ui->Save->setEnabled(false);
            ui->saved->show();
            QTimer::singleShot(750, [=]{ui->saved->hide();});
        }
        
        jsulmJ 1 Reply Last reply
        0
        • M MasterBlade

          @jsulm said in QTimer doesn't work the expected way:

          @MasterBlade said in QTimer doesn't work the expected way:

          The program pauses for 750 ms.

          I would say you need to post more of your code. singleShot should not block your app.

          I think you are correct. I commented all other codes and the program worked as expected.
          But I didn't find any problems with those commented codes. I can post them here. They are mainly used to write INI files.

          void Database::on_Save_clicked()
          {
              if (ui->Numbers->property("no1").toInt() == 0){
                  qDebug() << "Trying to Save an Empty Card on_Save_clicked().";
                  return;
              }
              QSettings set("*PATH*", QSettings::IniFormat);
              set.beginGroup(ui->Numbers->text());
          
              set.setValue("no1", ui->Numbers->property("no1"));
              /* All are setValue() */
              set.setValue("Type", ui->Types->text());
          
              QStringList sublist = ui->Types->property("SubList").toStringList();
              if (sublist.size() < 2){
                  set.setValue("SubList", "");
              }
              else{
                  set.setValue("SubList", ui->Types->property("SubList"));
              }
              UpdateTypes();
          
              set.setValue("Cost", ui->cost_value->text());
              /* All are setValue() */
              set.setValue("ResComm", ui->Restrictions->property("Comm").toInt());
          
              set.endGroup();
          
              ui->Save->setEnabled(false);
              ui->saved->show();
              QTimer::singleShot(750, [=]{ui->saved->hide();});
          }
          
          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MasterBlade said in QTimer doesn't work the expected way:

          UpdateTypes();

          What does this do?

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

          M 2 Replies Last reply
          0
          • jsulmJ jsulm

            @MasterBlade said in QTimer doesn't work the expected way:

            UpdateTypes();

            What does this do?

            M Offline
            M Offline
            MasterBlade
            wrote on last edited by
            #5

            @jsulm said in QTimer doesn't work the expected way:

            @MasterBlade said in QTimer doesn't work the expected way:

            UpdateTypes();

            What does this do?

            It is used to collect some info from the ui and update a QPushButton. I tried to comment it only and The program still got blocked.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @MasterBlade said in QTimer doesn't work the expected way:

              UpdateTypes();

              What does this do?

              M Offline
              M Offline
              MasterBlade
              wrote on last edited by
              #6

              @jsulm said in QTimer doesn't work the expected way:

              @MasterBlade said in QTimer doesn't work the expected way:

              UpdateTypes();

              What does this do?

              In case you really need to check this. It looks like this.

              void Database::UpdateTypes()
              {
                  bool IScroll, NScroll;
                  /* other bool variables */
                  QString str;
              
                  IScroll = ui->Types->property("IScroll").toBool();
                  NScroll = ui->Types->property("NScroll").toBool();
                  /* other variable assignments */
              
                  if (IScroll) str += tr("IScroll") + " ";
                  if (NScroll) str += tr("NScroll") + " ";
                  /* OtherCondition Check */
                  str.remove(str.size() - 1, 1);
              
                  QStringList Sublist = ui->Types->property("SubList").toStringList();
                  int size = Sublist.size() / 2;
              
                  if (size){
                      str += " -";
                      for (int i = 0; i < size; ++i){
                          str += " " + Sublist.at(i * 2);
                      }
                  }
              
                  if (str.isEmpty()){
                      str = tr("Main && Subtypes");
                  }
                  ui->Types->setText(str);
              }
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Run your program with a debugger, stop the debugger when your program stops and take a look at the backtrace where it comes from.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                M 2 Replies Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  Run your program with a debugger, stop the debugger when your program stops and take a look at the backtrace where it comes from.

                  M Offline
                  M Offline
                  MasterBlade
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher said in QTimer doesn't work the expected way:

                  Run your program with a debugger, stop the debugger when your program stops and take a look at the backtrace where it comes from.

                  I am currently running the program with QTCreator in debug mode. May I ask how to stop the debugger?

                  jsulmJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    Run your program with a debugger, stop the debugger when your program stops and take a look at the backtrace where it comes from.

                    M Offline
                    M Offline
                    MasterBlade
                    wrote on last edited by MasterBlade
                    #9

                    @Christian-Ehrlicher said in QTimer doesn't work the expected way:

                    Run your program with a debugger, stop the debugger when your program stops and take a look at the backtrace where it comes from.

                    Hi I have set the single shot duration to 10 secs and rerun the program. It pauses for 2 secs and the text disappeared at 10 sec.
                    Does this mean there is a long latency when writing INI? I need to write 40 items to the INI file.

                    1 Reply Last reply
                    0
                    • M MasterBlade

                      @Christian-Ehrlicher said in QTimer doesn't work the expected way:

                      Run your program with a debugger, stop the debugger when your program stops and take a look at the backtrace where it comes from.

                      I am currently running the program with QTCreator in debug mode. May I ask how to stop the debugger?

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @MasterBlade said in QTimer doesn't work the expected way:

                      May I ask how to stop the debugger?

                      Simply press the same button you used to start debugging...

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

                      M 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @MasterBlade said in QTimer doesn't work the expected way:

                        May I ask how to stop the debugger?

                        Simply press the same button you used to start debugging...

                        M Offline
                        M Offline
                        MasterBlade
                        wrote on last edited by
                        #11

                        @jsulm said in QTimer doesn't work the expected way:

                        @MasterBlade said in QTimer doesn't work the expected way:

                        May I ask how to stop the debugger?

                        Simply press the same button you used to start debugging...

                        I paused the debugger, but didn't find many differences. Application output only has a single line. Where else should I check?

                        15:16:35: Debugging starts
                        
                        jsulmJ 1 Reply Last reply
                        0
                        • M MasterBlade

                          @jsulm said in QTimer doesn't work the expected way:

                          @MasterBlade said in QTimer doesn't work the expected way:

                          May I ask how to stop the debugger?

                          Simply press the same button you used to start debugging...

                          I paused the debugger, but didn't find many differences. Application output only has a single line. Where else should I check?

                          15:16:35: Debugging starts
                          
                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @MasterBlade said in QTimer doesn't work the expected way:

                          Where else should I check?

                          In the debugger. In debugger view there is "Debugger" section with the stack trace (select thread #1). You should really get familiar with the debugger.

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

                          M 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @MasterBlade said in QTimer doesn't work the expected way:

                            Where else should I check?

                            In the debugger. In debugger view there is "Debugger" section with the stack trace (select thread #1). You should really get familiar with the debugger.

                            M Offline
                            M Offline
                            MasterBlade
                            wrote on last edited by
                            #13

                            @jsulm said in QTimer doesn't work the expected way:

                            @MasterBlade said in QTimer doesn't work the expected way:

                            Where else should I check?

                            In the debugger. In debugger view there is "Debugger" section with the stack trace (select thread #1). You should really get familiar with the debugger.

                            1.png

                            jsulmJ 1 Reply Last reply
                            0
                            • M MasterBlade

                              @jsulm said in QTimer doesn't work the expected way:

                              @MasterBlade said in QTimer doesn't work the expected way:

                              Where else should I check?

                              In the debugger. In debugger view there is "Debugger" section with the stack trace (select thread #1). You should really get familiar with the debugger.

                              1.png

                              jsulmJ Online
                              jsulmJ Online
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @MasterBlade When exactly did you take this screen-shot? When your app was blocked? You also can check other threads.

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

                              M 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @MasterBlade When exactly did you take this screen-shot? When your app was blocked? You also can check other threads.

                                M Offline
                                M Offline
                                MasterBlade
                                wrote on last edited by
                                #15

                                @jsulm said in QTimer doesn't work the expected way:

                                @MasterBlade When exactly did you take this screen-shot? When your app was blocked? You also can check other threads.

                                Yeah it was taken when the program was blocked.
                                Thread #1-5 looks the same to me.

                                Thread #6 and #7
                                2.png
                                3.png

                                #8 and #9 are same as #1-5

                                #10-13
                                357baa19-b6cd-45c4-815b-6d2fba72be71-image.png

                                #14
                                b4c07d35-4b68-45ef-80d9-11635f78c4e1-image.png

                                #15
                                1ac91703-d49f-4777-ba3f-11b4c1ca9c7a-image.png

                                jsulmJ 1 Reply Last reply
                                0
                                • M MasterBlade

                                  @jsulm said in QTimer doesn't work the expected way:

                                  @MasterBlade When exactly did you take this screen-shot? When your app was blocked? You also can check other threads.

                                  Yeah it was taken when the program was blocked.
                                  Thread #1-5 looks the same to me.

                                  Thread #6 and #7
                                  2.png
                                  3.png

                                  #8 and #9 are same as #1-5

                                  #10-13
                                  357baa19-b6cd-45c4-815b-6d2fba72be71-image.png

                                  #14
                                  b4c07d35-4b68-45ef-80d9-11635f78c4e1-image.png

                                  #15
                                  1ac91703-d49f-4777-ba3f-11b4c1ca9c7a-image.png

                                  jsulmJ Online
                                  jsulmJ Online
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @MasterBlade Do you actually use threads in your app?

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

                                  M 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @MasterBlade Do you actually use threads in your app?

                                    M Offline
                                    M Offline
                                    MasterBlade
                                    wrote on last edited by
                                    #17

                                    @jsulm said in QTimer doesn't work the expected way:

                                    @MasterBlade Do you actually use threads in your app?

                                    I don't think I did. I don't know why there are so many threads.

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • M MasterBlade

                                      @jsulm said in QTimer doesn't work the expected way:

                                      @MasterBlade Do you actually use threads in your app?

                                      I don't think I did. I don't know why there are so many threads.

                                      jsulmJ Online
                                      jsulmJ Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @MasterBlade That's normal. I just wanted to know whether you create threads also by yourself. From the stack trace I can't see anything helpful.

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

                                      M 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @MasterBlade That's normal. I just wanted to know whether you create threads also by yourself. From the stack trace I can't see anything helpful.

                                        M Offline
                                        M Offline
                                        MasterBlade
                                        wrote on last edited by
                                        #19

                                        @jsulm said in QTimer doesn't work the expected way:

                                        @MasterBlade That's normal. I just wanted to know whether you create threads also by yourself. From the stack trace I can't see anything helpful.

                                        There is one problem I found curious. I am also using QSettings to write INI files, and I found it quite slow. Usually takes like 2 secs to write. I didn't use QTimer in that code. So I guess if it's QSettings that caused the block.

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • M MasterBlade

                                          @jsulm said in QTimer doesn't work the expected way:

                                          @MasterBlade That's normal. I just wanted to know whether you create threads also by yourself. From the stack trace I can't see anything helpful.

                                          There is one problem I found curious. I am also using QSettings to write INI files, and I found it quite slow. Usually takes like 2 secs to write. I didn't use QTimer in that code. So I guess if it's QSettings that caused the block.

                                          jsulmJ Online
                                          jsulmJ Online
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @MasterBlade Where are the settings stored? Is it network drive, local drive, Windows registry?

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

                                          M 1 Reply Last reply
                                          0

                                          • Login

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