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. [SOLVED] Crash when Updating GUI from thread

[SOLVED] Crash when Updating GUI from thread

Scheduled Pinned Locked Moved General and Desktop
14 Posts 7 Posters 13.8k 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.
  • M Offline
    M Offline
    Mar91
    wrote on last edited by
    #1

    I have the following code:
    @
    void MainWindow::first() {
    QFuture<void> installerthread;
    installerthread = QtConcurrent::run(this, &MainWindow::Installa);
    }
    void MainWindow::Installa() {
    myClass bla=new myClass();
    bla.work();
    connect(bla,SIGNAL(finished(int)),this,SLOT(ProcessoFinito(int)), Qt::QueuedConnection);
    }
    void MainWindow::ProcessoFinito(int status){
    this->ui->progressBar->setValue(1);
    }
    @

    The error which appears is QPixmap: It is not safe to use pixmaps outside the GUI thread
    How can i solve this situation? I know that a GUI update from thread shouldn't happen,but there must be a solution!

    Solution: You can't update a QPixmap from thread! You should use another method..

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      You're going to need signals & slots.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Don't use pixmaps or drawing to the GUI from a thread that is not the GUI thread. The solution is to do the update from the main thread. Your code is not very clear to me though. You already have a signal/slot connection, and that is good, but I can't see if you use it correctly.

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xinevil
          wrote on last edited by
          #4

          So to sum it up:
          you are not allowed to update the GUI from a thread directly. You should use signals/slots to update the GUI instead.

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on last edited by
            #5

            Take a look at how the Mandelbrot example in Qt uses signals/slots across thread boundaries to update the gui with an image calculated in the worker thread.

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on last edited by
              #6

              About the Mandelbrot example: drawing on QImages is perfectly safe in other threads, the only problem being font rendering (which may or may not be supported, QFontDatabase::supportsThreadedFontRendering tells you if it's the case). OTOH, QPixmaps are on the "server side", so can't be touched from any threads but the main thread.

              http://doc.qt.nokia.com/4.7/threads-modules.html#painting-in-threads

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mar91
                wrote on last edited by
                #7

                But i am using signals and slot, why it have to crash?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mar91
                  wrote on last edited by
                  #8

                  Ahh, so do you think that updating QPixmap is not safe also with signal slot?

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    ZapB
                    wrote on last edited by
                    #9

                    QPixmap cannot be touched in any thread other than the main thread.

                    Nokia Certified Qt Specialist
                    Interested in hearing about Qt related work

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      Mar91: it all depends on how your threading setup works. You can trigger an update from another thread, if you have set it up correctly.

                      What does your worker class look like exactly. You did not, for intance subclass QThread directly and defined your signal on that subclass itself?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mar91
                        wrote on last edited by
                        #11

                        i didn't use real thread, but a simpler QFuturevoid :)

                        My class is a simply bunch of function, the critical function is the installer(), which has a list of .exe to execute, and i wanted to execute one by one, so i used the QProcess::waitforfinished(-1) ( Hangs until the QProcess has been completed ). I also could use the signal QProcess::finished(int), but then myclass would have been really hardcoded, because the signal had to trigger again the installer function until the list is empty, and i don't think is a good solution ( if you think is good, i am ready to remove thread , they are only causing me lot of problem :) ). Myclass inside has no thread or QFutureVoid, it has simply some signal\slot, nothing else :)

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          I think, I would remove the threading and go for the async solution you laid out, consecutively executing your processes and triggering the next iteration on QProcess::finished().

                          However, I don't see an obvious mistake in your current approach, other than a memory leak in you not destroying your bla instance.

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Mar91
                            wrote on last edited by
                            #13

                            But it's not my fault.. if you try to update a QPlainTextEdit it works without crash! As you can see from the link posted from peppe, QPixmap updating it's not allowed from thread! I must switch to the signal\slot method!
                            Thanks to everybody :)
                            You can mark has Solved i think

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              giesbert
                              wrote on last edited by
                              #14

                              Only because it does not crash does not mean it's correct.
                              If you see, the thread as solved, click edit on the first post and change the title, please

                              Nokia Certified Qt Specialist.
                              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                              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