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. Why My timer is not emit timeout signal ?

Why My timer is not emit timeout signal ?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.8k 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.
  • J Offline
    J Offline
    joeQ
    wrote on 28 Apr 2017, 08:50 last edited by
    #1

    if we have a long time to procress some thing, we can use the QProgressDialog to show the schedule. it is very easy and soon if you know the times of your do some thing. like numFiles:

    QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
    progress.setWindowModality(Qt::WindowModal);
    
    for (int i = 0; i < numFiles; i++) {
        progress.setValue(i);
    
        if (progress.wasCanceled())
            break;
        //... copy one file
    }
    progress.setValue(numFiles);
    

    According to my want, i subclass QProgressDialog to ProgressDialog, like:

    ProgressDialog::ProgressDialog(const QString &labelText, QWidget *parent):
        QProgressDialog(parent)
    {
        setLabelText(labelText);
        setCancelButtonText(tr("cancel btn"));
    
        setRange(0,100);
    
        setWindowModality(Qt::WindowModal);
    
        /** I want to use the timer to control progress bar value add */
        _t = new QTimer(this);
        _t->setInterval(500);
        connect(_t,&QTimer::timeout,this,&ProgressDialog::StTimeOut);
        _t->start();
    }
    
    void ProgressDialog::StTimeOut()
    {
        int nVal = value();
        qDebug() << "---" << nVal;
        nVal++;
        setValue(nVal);
    }
    

    In Dialog file

    void Dialog::on_btnNoTime_clicked()
    {
        ProgressDialog progress("Copying files...",this);
    
        /** To simulate do long some thing */
        int nSleep = 1000;
        QThread::msleep(nSleep);
        qDebug("Run 1!");
        QThread::msleep(nSleep);
        qDebug("Run 2!");
        QThread::msleep(nSleep);
        qDebug("Run 3!");
        QThread::msleep(nSleep);
        qDebug("Run 4!");
        QThread::msleep(nSleep);
        qDebug("Run 5!");
        QThread::msleep(nSleep);
        qDebug("Run 6!");
        QThread::msleep(nSleep);
        qDebug("Run 7!");
        QThread::msleep(nSleep);
        qDebug("Run 8!");
        QThread::msleep(nSleep);
        qDebug("Run 9!");
        QThread::msleep(nSleep);
        qDebug("Run ok!");
    }
    

    I don't want write timer in Dialog class.

    I want to wrap them in ProgressDialog class, let the interface becomes simple.

    My timer not work.

    Any help is greately appreciated. Thanks.

    Just do it!

    J 1 Reply Last reply 28 Apr 2017, 08:54
    0
    • J joeQ
      28 Apr 2017, 08:50

      if we have a long time to procress some thing, we can use the QProgressDialog to show the schedule. it is very easy and soon if you know the times of your do some thing. like numFiles:

      QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
      progress.setWindowModality(Qt::WindowModal);
      
      for (int i = 0; i < numFiles; i++) {
          progress.setValue(i);
      
          if (progress.wasCanceled())
              break;
          //... copy one file
      }
      progress.setValue(numFiles);
      

      According to my want, i subclass QProgressDialog to ProgressDialog, like:

      ProgressDialog::ProgressDialog(const QString &labelText, QWidget *parent):
          QProgressDialog(parent)
      {
          setLabelText(labelText);
          setCancelButtonText(tr("cancel btn"));
      
          setRange(0,100);
      
          setWindowModality(Qt::WindowModal);
      
          /** I want to use the timer to control progress bar value add */
          _t = new QTimer(this);
          _t->setInterval(500);
          connect(_t,&QTimer::timeout,this,&ProgressDialog::StTimeOut);
          _t->start();
      }
      
      void ProgressDialog::StTimeOut()
      {
          int nVal = value();
          qDebug() << "---" << nVal;
          nVal++;
          setValue(nVal);
      }
      

      In Dialog file

      void Dialog::on_btnNoTime_clicked()
      {
          ProgressDialog progress("Copying files...",this);
      
          /** To simulate do long some thing */
          int nSleep = 1000;
          QThread::msleep(nSleep);
          qDebug("Run 1!");
          QThread::msleep(nSleep);
          qDebug("Run 2!");
          QThread::msleep(nSleep);
          qDebug("Run 3!");
          QThread::msleep(nSleep);
          qDebug("Run 4!");
          QThread::msleep(nSleep);
          qDebug("Run 5!");
          QThread::msleep(nSleep);
          qDebug("Run 6!");
          QThread::msleep(nSleep);
          qDebug("Run 7!");
          QThread::msleep(nSleep);
          qDebug("Run 8!");
          QThread::msleep(nSleep);
          qDebug("Run 9!");
          QThread::msleep(nSleep);
          qDebug("Run ok!");
      }
      

      I don't want write timer in Dialog class.

      I want to wrap them in ProgressDialog class, let the interface becomes simple.

      My timer not work.

      Any help is greately appreciated. Thanks.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Apr 2017, 08:54 last edited by
      #2

      @joeQ said in Why My timer is not emit timeout signal ?:

      QThread::msleep

      It is not working because you're blocking the Qt event loop with all those QThread::msleep calls...

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

      J 1 Reply Last reply 28 Apr 2017, 08:59
      3
      • J jsulm
        28 Apr 2017, 08:54

        @joeQ said in Why My timer is not emit timeout signal ?:

        QThread::msleep

        It is not working because you're blocking the Qt event loop with all those QThread::msleep calls...

        J Offline
        J Offline
        joeQ
        wrote on 28 Apr 2017, 08:59 last edited by
        #3

        @jsulm How to simulate do spend a long time , not block Qt event loop?

        Just do it!

        J J V 3 Replies Last reply 28 Apr 2017, 09:01
        0
        • J joeQ
          28 Apr 2017, 08:59

          @jsulm How to simulate do spend a long time , not block Qt event loop?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 28 Apr 2017, 09:01 last edited by
          #4

          @joeQ Why do you want to simulate this in the first place? Qt is an event driven framework - you should not do any long lasting calculations in the GUI thread. Move such calculations to other threads - then your UI will not block.

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

          J 1 Reply Last reply 28 Apr 2017, 09:04
          3
          • J jsulm
            28 Apr 2017, 09:01

            @joeQ Why do you want to simulate this in the first place? Qt is an event driven framework - you should not do any long lasting calculations in the GUI thread. Move such calculations to other threads - then your UI will not block.

            J Offline
            J Offline
            joeQ
            wrote on 28 Apr 2017, 09:04 last edited by
            #5

            @jsulm Ok, you are right. I am wrong.

            Just do it!

            1 Reply Last reply
            0
            • J joeQ
              28 Apr 2017, 08:59

              @jsulm How to simulate do spend a long time , not block Qt event loop?

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 28 Apr 2017, 09:05 last edited by
              #6

              @joeQ said in Why My timer is not emit timeout signal ?:

              @jsulm How to simulate do spend a long time , not block Qt event loop?

              here a quick example, anyway:

              QProgressBar *pbar = new QProgressBar();
              
                  pbar->setMaximum(100);
                  QTimer *timer = new QTimer();
                  timer->setInterval(1000);
                  connect(timer, &QTimer::timeout,pbar,[pbar]{pbar->setValue(pbar->value()+1);});
              
                  pbar->show();
                  timer->start();
              

              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
              0
              • J joeQ
                28 Apr 2017, 08:59

                @jsulm How to simulate do spend a long time , not block Qt event loop?

                V Offline
                V Offline
                VRonin
                wrote on 28 Apr 2017, 09:11 last edited by VRonin
                #7

                @joeQ said in Why My timer is not emit timeout signal ?:

                How to simulate do spend a long time , not block Qt event loop?

                const int nSleep = 1000;
                QEventLoop delayer;
                QTimer delayTimer;
                delayTimer.setSingleShot(true);
                QObject::connect(&delayTimer,&QTimer::timeout,&delayer,&QEventLoop::quit);
                delayTimer.start(nSleep);
                delayer.exec();
                

                I'm not saying you should use this design though.

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                J 1 Reply Last reply 28 Apr 2017, 09:56
                1
                • V VRonin
                  28 Apr 2017, 09:11

                  @joeQ said in Why My timer is not emit timeout signal ?:

                  How to simulate do spend a long time , not block Qt event loop?

                  const int nSleep = 1000;
                  QEventLoop delayer;
                  QTimer delayTimer;
                  delayTimer.setSingleShot(true);
                  QObject::connect(&delayTimer,&QTimer::timeout,&delayer,&QEventLoop::quit);
                  delayTimer.start(nSleep);
                  delayer.exec();
                  

                  I'm not saying you should use this design though.

                  J Offline
                  J Offline
                  joeQ
                  wrote on 28 Apr 2017, 09:56 last edited by
                  #8

                  @VRonin @J-Hilk

                  I know ,maybe I am some annoying. But,I have some questions.

                  I don't clear QProgressDialog well, How the QProgressDialog was not block gui when QProgressDialog was set Qt::WindowModal ?

                  Qt::WindowModal:The window is modal to a single window hierarchy and blocks input to its parent window, all grandparent windows, and all siblings of its parent and grandparent windows.

                  I know that, when dialog was set modal show, like:

                  {
                  QDialog dlg;
                  dlg.setModal(true);
                  dlg.show(); ///< we know dlg will show bad, it is gui was blocked., if using `QProgressDialog`, is can show well. not have the gui block.
                  
                  /** spend a long time at here */
                  }
                  

                  what method is used in QProgressDialog? QProgressDialog is in another thread to show ?

                  Just do it!

                  J 1 Reply Last reply 28 Apr 2017, 10:42
                  0
                  • J joeQ
                    28 Apr 2017, 09:56

                    @VRonin @J-Hilk

                    I know ,maybe I am some annoying. But,I have some questions.

                    I don't clear QProgressDialog well, How the QProgressDialog was not block gui when QProgressDialog was set Qt::WindowModal ?

                    Qt::WindowModal:The window is modal to a single window hierarchy and blocks input to its parent window, all grandparent windows, and all siblings of its parent and grandparent windows.

                    I know that, when dialog was set modal show, like:

                    {
                    QDialog dlg;
                    dlg.setModal(true);
                    dlg.show(); ///< we know dlg will show bad, it is gui was blocked., if using `QProgressDialog`, is can show well. not have the gui block.
                    
                    /** spend a long time at here */
                    }
                    

                    what method is used in QProgressDialog? QProgressDialog is in another thread to show ?

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 28 Apr 2017, 10:42 last edited by J.Hilk
                    #9

                    @joeQ With Qt, anything that is drawn/shown on your screen is handlet in the main GUI-Thread. You can't, without a lot of work, draw/paint anything on your screen outside that main thread.

                    To your Modal-Question:

                    If you create a Dialog and start it with Exec, the default behaviour is, that a new Window is shown, the Dialoag, and the old Window is disabled, That means the gui will not accept any input from your touchscreen, mouse or keyboard as long as the Dialog is not finished/aborded.

                    A modal window is one that blocks input to other windows of your applications. If you set it to NonModal for example, you can than access your other windows while the dialog is up and running.

                    This has nothing to do with threads and blocking the GuiThread

                    Edit:
                    I'll have to rectify myself. IIrc, when you start your QDialog with exec() than that Dialog is indeed started in local event loop.


                    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.

                    J 1 Reply Last reply 29 Apr 2017, 01:44
                    0
                    • J J.Hilk
                      28 Apr 2017, 10:42

                      @joeQ With Qt, anything that is drawn/shown on your screen is handlet in the main GUI-Thread. You can't, without a lot of work, draw/paint anything on your screen outside that main thread.

                      To your Modal-Question:

                      If you create a Dialog and start it with Exec, the default behaviour is, that a new Window is shown, the Dialoag, and the old Window is disabled, That means the gui will not accept any input from your touchscreen, mouse or keyboard as long as the Dialog is not finished/aborded.

                      A modal window is one that blocks input to other windows of your applications. If you set it to NonModal for example, you can than access your other windows while the dialog is up and running.

                      This has nothing to do with threads and blocking the GuiThread

                      Edit:
                      I'll have to rectify myself. IIrc, when you start your QDialog with exec() than that Dialog is indeed started in local event loop.

                      J Offline
                      J Offline
                      joeQ
                      wrote on 29 Apr 2017, 01:44 last edited by
                      #10

                      @J.Hilk Thank u, I get it.

                      Just do it!

                      1 Reply Last reply
                      0

                      2/10

                      28 Apr 2017, 08:54

                      8 unread
                      • Login

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