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

QTimer in a GUI application

Scheduled Pinned Locked Moved General and Desktop
25 Posts 5 Posters 9.7k Views 1 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.
  • H Offline
    H Offline
    HamiltonQ
    wrote on 23 Jan 2015, 00:24 last edited by
    #1

    Hello, everyone!

    I'm starting to produce applications with Qt In one of my applications I've come across an unexpected output:

    QObject :: startTimer: QTimer can only be used with threads started with QThread.

    I'm starting my timer (timer-> start ()) in the class that contains the UI elements (QMainWindow). I do this out of the constructor, in the function below:

    void client :: showPorts () {

    SettingsDialog showDialogPorts * = new SettingsDialog (this);
          serial = new QSerialPort ();
          timer = new QTimer ();
          connect (time, SIGNAL (timeout ()), this, SLOT (doMyTask ()));
          timer-> start (20);
         
          .
          .
          .
          .
          .

    }

    What's wrong with using QTimer in the GUI class (QMainWindow)? Thanks!!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Jan 2015, 00:43 last edited by
      #2

      Hi and welcome to devnet,

      Are you sure it's that timer that triggers the message ? Not any thread using it ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HamiltonQ
        wrote on 23 Jan 2015, 00:50 last edited by
        #3

        Hey, man! You are fast, thanks!! I'm not sure, but I want to control my event from a timer that I have control, so I used the QTimer * timer = new QTimer ();

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HamiltonQ
          wrote on 23 Jan 2015, 00:53 last edited by
          #4

          It seems to me that there is some problem when I use in class that contains access to UI, am I right?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JKSH
            Moderators
            wrote on 23 Jan 2015, 02:49 last edited by
            #5

            [quote author="HamiltonQ" date="1421974393"]It seems to me that there is some problem when I use in class that contains access to UI, am I right?[/quote]No, that's not a problem.

            How many threads does your program have?

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • H Offline
              H Offline
              HamiltonQ
              wrote on 23 Jan 2015, 03:35 last edited by
              #6

              I don't initiated any thread purposely. I do not know if the application automatically starts some thread to call some method or to start some object.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                HamiltonQ
                wrote on 23 Jan 2015, 03:37 last edited by
                #7

                I just want to start a count to trigger a function every 20 ms in the same class that contains the call to the UI. It's possible?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alex_malyu
                  wrote on 23 Jan 2015, 03:53 last edited by
                  #8

                  It should not be a problem unless doMyTask () can initiate another call to showPorts () in which cay you may keep creating timers until system is exhausted

                  Alex.

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    HamiltonQ
                    wrote on 23 Jan 2015, 14:52 last edited by
                    #9

                    [quote author="alex_malyu" date="1421985237"]It should not be a problem unless doMyTask () can initiate another call to showPorts () in which cay you may keep creating timers until system is exhausted

                    Alex.[/quote]

                    Thanks, Alex!

                    But, why doMyTask() have to call showPorts()? I just wanna call doMyTask every 20 ms.

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      HamiltonQ
                      wrote on 23 Jan 2015, 18:06 last edited by
                      #10

                      I changed of place the connect ( connect (time, SIGNAL (timeout ()), this, SLOT (doMyTask ())); ), I put him on constructor, but nothing changed, I keep getting the output:

                      QObject :: startTimer: QTimer can only be used with threads started with QThread.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kuzulis
                        Qt Champions 2020
                        wrote on 23 Jan 2015, 18:59 last edited by
                        #11

                        do you have an QApplication instance?

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          HamiltonQ
                          wrote on 23 Jan 2015, 19:42 last edited by
                          #12

                          Hi!

                          QApplication instance is in the main.cpp file, but I'm starting the Timer on client.cpp file, which contains the call to the UI

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            kuzulis
                            Qt Champions 2020
                            wrote on 23 Jan 2015, 20:02 last edited by
                            #13

                            Then you can google by the "QTimer can only be used with threads started with QThread." pattern. ;)

                            1 Reply Last reply
                            -1
                            • H Offline
                              H Offline
                              HamiltonQ
                              wrote on 23 Jan 2015, 20:38 last edited by
                              #14

                              For three days I am trying to solve this. I have seen numerous topics, but none of them do what I want to do, which is to use the QTimer in the class that contains the UI. The example that the documentation give is for NON-GUI application. But I wanna exactly this

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 23 Jan 2015, 21:05 last edited by
                                #15

                                Then let's first get back to the basics.

                                Do you still see this message if you create a default Qt Widget application and add

                                @
                                mainwindow.h

                                class MainWindow: public QWidget
                                {
                                // default code

                                public slots:
                                void testTimer();
                                }

                                mainwindow.cpp

                                MainWindow:MainWindow(QWidget *parent)
                                : QWidget(parent)
                                {
                                QTimer *timer = new QTimer(this);
                                connect(timer, SIGNAL(timeout()), SLOT(testTimer()));

                                timer->start(20);
                                }

                                void MainWindow::testTimer()
                                {
                                qDebug() << "Timed out !";
                                }
                                @

                                to the default code ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • H Offline
                                  H Offline
                                  HamiltonQ
                                  wrote on 23 Jan 2015, 22:33 last edited by
                                  #16

                                  Hi, Samuel!

                                  This works, I tested. But my base class is QMainWindow. I tried to change the base class for QWiget but it happened several errors.

                                  Thanks, man!

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 23 Jan 2015, 23:23 last edited by
                                    #17

                                    Just keep your QMainWindow based class, the only thing that was important was the part related to the QTimer

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • H Offline
                                      H Offline
                                      HamiltonQ
                                      wrote on 23 Jan 2015, 23:37 last edited by
                                      #18

                                      I have to change the base class to use QTimer?

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        JKSH
                                        Moderators
                                        wrote on 23 Jan 2015, 23:42 last edited by
                                        #19

                                        Can you show us your code for doWork()?

                                        Note that in newer versions of Qt, the actual message is "QObject::startTimer: Timers can only be used with threads started with QThread". ("Timers", not "QTimer"!)

                                        All QObjects have built-in timers. I don't think the message came from your QTimer, but rather from your QSerialPort.

                                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 24 Jan 2015, 00:09 last edited by
                                          #20

                                          No you don't, QMainWindow is a QWidget as well

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          1/25

                                          23 Jan 2015, 00:24

                                          • Login

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