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. How to call .appendPlainText() from non GUI-thread

How to call .appendPlainText() from non GUI-thread

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 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.
  • C Offline
    C Offline
    Cube777
    wrote on last edited by
    #1

    Hi guys, I've been stuck with this problem for a while now, I keep getting runtime errors when I try to call a
    @QPlainTextEdit::appendPlainText()@

    function from the non-main GUI thread, how will I get past this? I'm not sure if I should but I am using a mutex to keep it synchronized with the other threads. Is there another thread-safe(is this the correct term to use?) function I can use? Thanks in advance for any help! Sorry I'm still very new to Qt and I'm not sure if I should use signals and slots...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can't access GUI related function from secondary thread. However you can use signals and slots for that purpose.

      Hope it helps

      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
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        So, don't call that function. Notice that that function is declared as a slot. That means that you can connect to it, and Qt is nice enough to make that thread safe for you.

        So, give your thread a signal that emits a QString, and connect that signal to your QPlainTextEdit::appendPlainText slot. Now, instead of the direct call to appendPlainText, emit your signal instead.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cube777
          wrote on last edited by
          #4

          Thank you for all the help! I'm still a noob sorry :P So will I declare a signal and connect it like this?:

          @connect(this, SIGNAL(someSignal(QString), plainTextEditPointer, SLOT(appendPlainText(QString)))@

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

            Esssentially, yes, though I usually prefer to do the connection at the other end. I think the thread (worker) should not need to care what happens with the results it produces. They may get logged to file, discarded, send over the network, or in this case displayed in a plain text edit: it should not matter to the code producing the data.

            Instead, I'd make the connection at the GUI-side of things, so you'd end up with something like:
            @
            connect(m_worker, SIGNAL(newDataAvailable(QString)), m_ui->pteDataView, SLOT(appendPlainText(QString)));

            //or, Qt 5 syntax:
            connect(m_worker, &MyWorker::newDataAvailable,
            m_ui->pteDataView, &QPlainTextEdit::appendPlainText);
            @

            The advantage of the second syntax is that it will give you compile-time warnings if the connection is incorrect. The downside is that adding overloads to the signal or slot will break your connection code.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Cube777
              wrote on last edited by
              #6

              Thank you very much! Finally this worked, again thanks for the help!

              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