Important: Please read the Qt Code of Conduct -

Using QGraphicsItem in DLL.

  • Hello.
    I have little GUI project. I use there QGrapchicScene with QGraphicsItems on it. And this project is built as DLL. Project which uses my DLL as plugin is able to call dll-interfaces for my dll not only from MainThread. It means that some updatings or manipulatings with QGraphicsItems may occure from WorkThreads. According to docs, QGraphicsItems doesn`t allow this.

    Q1: How to prevent this behaviour?
    Q2: Is there some "strategy" for developing GUI dll in multithreaded environment?

    Thanks in advance for helping!

  • You could post events to yourself notifying you of changes. This guarantees you are always running from the same thread, most likely the main/GUI thread.

  • Define a class which has a slot for every graphics operation you want to trigger. This class must reside in the GUI thread. Now it is safe to trigger these slots from a worker thread via queued conection.

    By the way: A queued connection can be triggered without defining a connection first:
    QMetaObject::invokeMethod(guiObject, "drawMyFancyLine", Qt::QueuedConnection);

  • Thanks guys for answers. But before I implemented that solution developers of Main Project have changed behaviour of theirs project and now it calls my dll only from MainThread. Could I be happy? Yes! But I came to a new problem. All works fine with Qt 4.7.3, but when we use Qt 4.5.3 my DLL is 'blocking'! Main app calls my dll, it creates windows but nothing is happening then - I cant push buttons, repaint isnt happening and etc...
    I have a piece of code from Main app which shows how app manages window events (messages). Maybe it would be useful to understand what`s wrong with it...
    @while(buckets_rendered<numRects && events!=0)
    while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

    stat = MsgWaitForMultipleObjects(numHandles, handles, FALSE, INFINITE, QS_ALLINPUT);
    if(stat == (WAIT_OBJECT_0 + numHandles))
    else // message comes from the events
    id = stat - WAIT_OBJECT_0;

    // ...

  • Did you try pinpointing what exactly the application is blocking on?

  • Im not sure about the blocking point, but I have important additional information. It seems that paintEvent is blocked somehow. Evenmore, when I click left mouse button on some application button nothing is happening, but when next I click right mouse button some events is occures and I see an effect from first left mouse button click and an effect from right button click...Why only right mouse button click force repaint? Im confused...

Log in to reply