Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QProgressDialog doubt

    General and Desktop
    5
    24
    17414
    Loading More Posts
    • 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.
    • G
      goetz last edited by

      If you set the range from 0 to 0 there is obviously no work to be done, so the dialog will never show up.

      I'd try this:
      @
      progress->setWindowModality(Qt::WindowModal);
      progress->setLabelText("Please wait...");
      progress->setRange(0,10);
      // always show the dialog
      // do no assumed time calculation
      progress->setMinimumDuration(0);
      // disable the cancel button at all
      progress->setCancelButton(0);
      // eventually show the dialog
      progress->setValue(1);
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply Reply Quote 0
      • D
        dasmoeh last edited by

        You can use exec to show your dialog. But it works only for me if i use a new Thread for my function. I'm looking for a solution without threads, but can't find something.

        @
        QProgressDialog dlg("Running...", QString(), 0, 0, this, Qt::CustomizeWindowHint | Qt::WindowTitleHint);
        dlg.setRange(0,0);
        MyThread* work = new MyThread();
        connect(work, SIGNAL(statusText(QString)), &dlg, SLOT(setLabelText(QString)));
        connect(work, SIGNAL(finished()), &dlg, SLOT(cancel()));
        connect(work, SIGNAL(errorText(QString,QWaitCondition*)), this, SLOT(workError(QString,QWaitCondition*)));
        work->start();
        dlg.exec();
        @

        1 Reply Last reply Reply Quote 0
        • D
          DenisKormalev last edited by

          Use single @ tag before and after code. I've fixed it in this comment, but don't forget about it in future posts.

          What do you mean by "solution without threads"?

          1 Reply Last reply Reply Quote 0
          • G
            goetz last edited by

            If you do your work in the main thread, then that thread is blocked with your work. You want to process the pending events from time to time, eg. with "QCoreApplication::processEvents() ":http://doc.qt.nokia.com/4.7/qcoreapplication.html#processEvents

            If you do the work in a separate thread, the main thread's event loop continues to run and everything is updated automatically.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply Reply Quote 0
            • D
              dasmoeh last edited by

              As long as i am waiting for a TIMEOUT
              @
              _tcpSocket.waitForConnected(TIMEOUT)
              @
              i want to show the QProgressDialog.

              Something like
              @
              QProgressDialog dlg("Running...", QString(), 0, 0, this, Qt::CustomizeWindowHint | Qt::WindowTitleHint);
              dlg.setRange(0,0);
              //dlg.exec()
              _tcpSocket.waitForConnected(TIMEOUT)
              dlg.cancel()
              @
              but if i use exec only the ProgressBar is running forever.

              Thank u for helping with the code tag. Hope its better now.

              1 Reply Last reply Reply Quote 0
              • G
                goetz last edited by

                this snippet works for me:

                @
                QProgressDialog pd;
                pd.setLabelText("waiting for connect");

                pd.setWindowModality(Qt::WindowModal);
                pd.setRange(0,0);
                // always show the dialog
                // do no assumed time calculation
                pd.setMinimumDuration(0);
                // disable the cancel button at all
                pd.setCancelButton(0);
                // eventually show the dialog
                pd.setValue(1);

                QTcpSocket sock;
                connect(&_tcpScoket, SIGNAL(connected()), &pd, SLOT(cancel()));
                _tcpScoket.connectToHost("host", 80);
                _tcpScoket.waitForConnected(30000);
                @

                Be aware that there is no need to explicitly exec the progress dialog (in contrary to an "ordinary" QDialog).

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply Reply Quote 0
                • D
                  dasmoeh last edited by

                  @Volker: Thank you. With processEvents() i can get the dialog visible. But it doesn't show title or progressbar. Only blank window. Some time ago i tried to put the tcpsocket in another thread, but it wasn't easy so it remained in the main thread. Is there a way to start the progressdialog in a new thread?

                  edit: Just seen your answer. I will try it.

                  1 Reply Last reply Reply Quote 0
                  • G
                    goetz last edited by

                    No, you must start gui elements in the main thread.

                    It depends on you application what to do exactly. If the app should block until the connection is done you can try something like this:

                    @
                    QProgressDialog pd;
                    // setup the dialog like posted

                    QEventLoop el;
                    connect(&_tcpScoket, SIGNAL(connected()), &el, SLOT(quit()));

                    QTcpSocket sock;
                    connect(&_tcpScoket, SIGNAL(connected()), &pd, SLOT(cancel()));
                    _tcpScoket.connectToHost("host", 80);
                    el.run();
                    pd.cancel();
                    @

                    This runs a local event loop, it keeps the QApplication running, but blocks the flow of control in the method until the socket is connected (and thus the event loop stops).

                    You will have to add some error handling (eg. connect to the error signal of the socket) and/or add a QTimer to setup a timeout.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply Reply Quote 0
                    • D
                      dasmoeh last edited by

                      I tried your code, but it did not work.
                      The first shows only for a second a pop-up after it finished waiting.
                      With the second i don't know where to call @_tcpScoket.waitForConnected(30000) @? I think el.run() should be el.exec()?

                      1 Reply Last reply Reply Quote 0
                      • G
                        goetz last edited by

                        Ahm. The progress dialog is only shown until the initial TCP connection has been set up. This usually takes less than a second on a decent internet connection... How long do you expect that to last in your case?

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply Reply Quote 0
                        • D
                          dasmoeh last edited by

                          Mh, i tried to connect to a not given IP. I want to see the dialog while
                          @
                          _tcpScoket.waitForConnected(30000)
                          @
                          is active till it times out. At 30000 ms.

                          In not working code:
                          @
                          dlg.show()
                          _tcpScoket.waitForConnected(30000)
                          dlg.cancel()
                          @

                          But in this 30000ms i don't get updates to my gui. Like i expected. But i'm looking for a way, to have my progressdialog running during this 30000 ms without calling waitForConnected from another thread because i don't want to move my socket from the main thread.
                          I think it's not possible?

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post