Having problems updating QGraphicsObject in a seperate QThread



  • 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!


  • Lifetime Qt Champion

    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.



  • 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]


  • Moderators

    [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."



  • 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]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.