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. Having problems updating QGraphicsObject in a seperate QThread
Qt 6.11 is out! See what's new in the release blog

Having problems updating QGraphicsObject in a seperate QThread

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.9k 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.
  • I Offline
    I Offline
    IneedAuserName
    wrote on last edited by
    #1

    Hi All,

    I've been using Qt's signal and slot mechanism for a while and I really like it. However I encountered some weird issue and I couldn't find an explanation anywhere. I want to run a QGraphicsObject in a seperate Thread than the main UI thread, and have the main thread's timer signals the QGraphicsObject to update the plot. Here's the code where I start the new thread:

    @ QThread *plotThread = new QThread(this);
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), plot, SLOT(updatePlot()));
    connect(this, SIGNAL(stopPlotting()), timer, SLOT(stop()));
    connect(this, SIGNAL(stopPlotting()), timer, SLOT(deleteLater()));
    plot->moveToThread(plotThread);
    timer->start(100);
    plotThread->start();
    @

    And for the updatePlot slot, it simply calls the update function. The CSignalPlotting class is a QGraphicsObject:

    @void CSignalPlotting::updatePlot()
    {
    qDebug() << "asd";
    this->update();
    }@

    I have overloaded the following protected functions
    @QRecctF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem , QWidget );
    @
    The issue is that,
    the paint function is called once after a random interval
    (The interval could be 5 seconds or minute long, nowhere near the set interval of 100ms), but the updatePlot slot is called regularly. Such issue doesn't exist when I don't run the QGraphicsObject in a separate thread.

    For the purpose of my project, I could put the QGraphicsObject into the same thread as the main UI thread, but I really wish to understand why this issue is happening.

    Thanks!

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

      Hi and welcome to devnet,

      You can't modify GUI objects from another thread. What you can do is move the data processing in another thread but all painting must be done in the main (aka GUI) thread.

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

        Thanks for your reply. Anywhere I can find the documentation that indicates this limitation?

        [quote author="SGaist" date="1386800906"]Hi and welcome to devnet,

        You can't modify GUI objects from another thread. What you can do is move the data processing in another thread but all painting must be done in the main (aka GUI) thread.[/quote]

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          [quote author="IneedAuserName" date="1386803985"]Thanks for your reply. Anywhere I can find the documentation that indicates this limitation?[/quote]http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html

          "Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread."

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

          1 Reply Last reply
          0
          • I Offline
            I Offline
            IneedAuserName
            wrote on last edited by
            #5

            Thanks, although it took me a while to understand :D

            [quote author="JKSH" date="1386809168"][quote author="IneedAuserName" date="1386803985"]Thanks for your reply. Anywhere I can find the documentation that indicates this limitation?[/quote]http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html

            "Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread."
            [/quote]

            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