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. Display text for 4 seconds
Forum Updated to NodeBB v4.3 + New Features

Display text for 4 seconds

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtimer
15 Posts 7 Posters 2.7k 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.
  • O Offline
    O Offline
    ollarch
    wrote on 10 Nov 2020, 09:33 last edited by
    #5

    You can have a QString member variable to store the message to show and when the timer triggers you can show the variable text.

    1 Reply Last reply
    0
    • U User123456
      10 Nov 2020, 07:13

      How can I display a string to a label for 4 seconds? Then after 4 seconds, the string can be changed to whatever string value.

      This is what I have tried so far:

      void Dialog::onDisplayMessage(string sMessage, bool bWithTimeout)
      {
          if(bWIthTimeout)
          {
              pMessageTimer_ = new QTimer(this);
      
              connect(pMessageTimer_, SIGNAL(timeout()), this, SLOT(stopTimer()));
              pMessageTimer_->start(4000);
      
              ui->LabelMessage->setText(QString::fromStdString(sMessage));
              ui->LabelMessage->update();
              QCoreApplication::processEvents();
      
          }
          else
          {
              ui->LabelMessage->setText(QString::fromStdString(sMessage));
              ui->LabelMessage->update();
              QCoreApplication::processEvents();
          }
      }
      
      void Dialog::stopTimer()
      {
          pMessageTimer_->stop();
      }
      

      With the above code, the text is not changing after 4 seconds.

      onDisplayMessage() is a SLOT and can be called anytime. I put a Boolean flag (bWithTimeout) to know which message will be displayed for four seconds. After four seconds and the onDisplayMessage is called again and the flag is false, the timer should not start and the text will be changed.```

      J Offline
      J Offline
      JonB
      wrote on 10 Nov 2020, 09:54 last edited by JonB 11 Oct 2020, 14:11
      #6

      @User123456
      2 lines will do this, if you know how to use C++ lambdas (only a couple of lines longer if you don't and write an explicit slot):

      ui->LabelMessage->setText("Hello");
      QTimer::singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });
      

      No need for your update(), processEvents().

      1 Reply Last reply
      6
      • U Offline
        U Offline
        User123456
        wrote on 10 Nov 2020, 14:00 last edited by User123456 11 Oct 2020, 14:02
        #7

        @JonB said in Display text for 4 seconds:

        QTimer:singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });

        Thank you @JonB but when I try this, it gives me this error:
        no matching function for call to ‘QTimer::singleShot(int, Dialog*, Dialog::onDisplayMessage(std::__cxx11::string, bool)::<lambda()>)’
        QTimer::singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });

        I am not familiar with c++ lambdas right now.
        ^

        J P 2 Replies Last reply 10 Nov 2020, 14:11
        0
        • U User123456
          10 Nov 2020, 14:00

          @JonB said in Display text for 4 seconds:

          QTimer:singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });

          Thank you @JonB but when I try this, it gives me this error:
          no matching function for call to ‘QTimer::singleShot(int, Dialog*, Dialog::onDisplayMessage(std::__cxx11::string, bool)::<lambda()>)’
          QTimer::singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });

          I am not familiar with c++ lambdas right now.
          ^

          J Offline
          J Offline
          JonB
          wrote on 10 Nov 2020, 14:11 last edited by JonB 11 Oct 2020, 14:13
          #8

          @User123456
          I don't know how I mis-pasted since I tested it :( But anyway where I had a single colon QTimer: I meant a double colon (and have corrected above):

          QTimer::singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });
          

          Then again, you seem to have that in your error message, so maybe you had already done that? It works for me, are you at least Qt 5.8 (i.e. not old)?

          U 1 Reply Last reply 11 Nov 2020, 03:26
          0
          • U User123456
            10 Nov 2020, 14:00

            @JonB said in Display text for 4 seconds:

            QTimer:singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });

            Thank you @JonB but when I try this, it gives me this error:
            no matching function for call to ‘QTimer::singleShot(int, Dialog*, Dialog::onDisplayMessage(std::__cxx11::string, bool)::<lambda()>)’
            QTimer::singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });

            I am not familiar with c++ lambdas right now.
            ^

            P Offline
            P Offline
            Pradeep P N
            wrote on 11 Nov 2020, 03:10 last edited by Pradeep P N 11 Nov 2020, 03:16
            #9

            Hi @User123456

            If i remember i had similar issue with Qt 5.6 or so i guess, but with preset Qt update it works fine (I am using Qt 5.12)

            Can you please try including CONFIG += c++11 in your .pro file if not included.
            AFAIK the Lambdas are included since C++11 .

            then the code should work fine as given by @JonB

                QTimer::singleShot(4000, this, [this] () { 
                    ui->LabelMessage->setText(""); 
                });
            

            Pradeep Nimbalkar.
            Upvote the answer(s) that helped you to solve the issue...
            Keep code clean.

            1 Reply Last reply
            2
            • J JonB
              10 Nov 2020, 14:11

              @User123456
              I don't know how I mis-pasted since I tested it :( But anyway where I had a single colon QTimer: I meant a double colon (and have corrected above):

              QTimer::singleShot(4000, this, [this] () { ui->LabelMessage->setText(""); });
              

              Then again, you seem to have that in your error message, so maybe you had already done that? It works for me, are you at least Qt 5.8 (i.e. not old)?

              U Offline
              U Offline
              User123456
              wrote on 11 Nov 2020, 03:26 last edited by
              #10

              @JonB, I am using an old version, 4.5. I cannot just upgrade it since it is part of the requirements. Could you please give an example which will work on that version?

              P J J 3 Replies Last reply 11 Nov 2020, 04:32
              0
              • U User123456
                11 Nov 2020, 03:26

                @JonB, I am using an old version, 4.5. I cannot just upgrade it since it is part of the requirements. Could you please give an example which will work on that version?

                P Offline
                P Offline
                Pradeep P N
                wrote on 11 Nov 2020, 04:32 last edited by Pradeep P N 11 Nov 2020, 04:53
                #11

                @User123456

                Did you try including CONFIG += c++11 in your .pro file if not included.
                Lambda expressions (since C++11).

                Or for normal Signal Slot simple code below would be fine.
                d0d47cbe-9f08-455e-94e6-da297ae5be97-image.png

                Pradeep Nimbalkar.
                Upvote the answer(s) that helped you to solve the issue...
                Keep code clean.

                U B 2 Replies Last reply 11 Nov 2020, 04:52
                1
                • P Pradeep P N
                  11 Nov 2020, 04:32

                  @User123456

                  Did you try including CONFIG += c++11 in your .pro file if not included.
                  Lambda expressions (since C++11).

                  Or for normal Signal Slot simple code below would be fine.
                  d0d47cbe-9f08-455e-94e6-da297ae5be97-image.png

                  U Offline
                  U Offline
                  User123456
                  wrote on 11 Nov 2020, 04:52 last edited by
                  #12

                  @Pradeep-P-N said in Display text for 4 seconds:

                  CONFIG += c++11

                  yes, it's already included

                  1 Reply Last reply
                  0
                  • P Pradeep P N
                    11 Nov 2020, 04:32

                    @User123456

                    Did you try including CONFIG += c++11 in your .pro file if not included.
                    Lambda expressions (since C++11).

                    Or for normal Signal Slot simple code below would be fine.
                    d0d47cbe-9f08-455e-94e6-da297ae5be97-image.png

                    B Offline
                    B Offline
                    Bonnie
                    wrote on 11 Nov 2020, 05:00 last edited by Bonnie 11 Nov 2020, 05:02
                    #13

                    @Pradeep-P-N said in Display text for 4 seconds:

                    Did you try including CONFIG += c++11 in your .pro file if not included.

                    That doesn't matter. Qt4 doesn't support using lambda as slots at all.
                    Qt4.5 was released even before c++11 was published

                    1 Reply Last reply
                    2
                    • U User123456
                      11 Nov 2020, 03:26

                      @JonB, I am using an old version, 4.5. I cannot just upgrade it since it is part of the requirements. Could you please give an example which will work on that version?

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 11 Nov 2020, 05:16 last edited by jsulm 11 Nov 2020, 05:17
                      #14

                      @User123456 said in Display text for 4 seconds:

                      Could you please give an example which will work on that version?

                      Simply use a normal slot instead of a lambda.
                      There is even an example in the documentation...

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

                      1 Reply Last reply
                      0
                      • U User123456
                        11 Nov 2020, 03:26

                        @JonB, I am using an old version, 4.5. I cannot just upgrade it since it is part of the requirements. Could you please give an example which will work on that version?

                        J Offline
                        J Offline
                        JonB
                        wrote on 11 Nov 2020, 07:53 last edited by JonB 11 Nov 2020, 07:59
                        #15

                        @User123456 said in Display text for 4 seconds:

                        @JonB, I am using an old version, 4.5. I cannot just upgrade it since it is part of the requirements. Could you please give an example which will work on that version?

                        Given that you appear to be a beginner, I am surprised you are using such an old version of Qt. That must mean you have an existing, pretty ancient Qt code base to work with. I understand it's not automatic/trivial to upgrade, but it's shame. If you post any future questions in this forum you should state each time that you are using Qt4, as it may be relevant to answers. Bear in mind when looking through documentation/examples you may need to check for Qt 4 ones/compatibility.

                        Anyway, with an explicit method and not a lambda:

                        // header file:
                        private slots:
                            void onTimeout();
                        
                        // cpp file:
                        void Dialog::onTimeout()
                        {
                            ui->LabelMessage->setText("");
                        }
                        
                        // caller:
                        // next line connects using *new style* syntax, which is nicer...
                        QTimer::singleShot(4000, this, &Dialog::onTimeout);
                        // ... but if that doesn't work in Qt4 you can use *old style* syntax
                        QTimer::singleShot(4000, this, SLOT(onTimeout()));
                        
                        1 Reply Last reply
                        2

                        14/15

                        11 Nov 2020, 05:16

                        • Login

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