Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Sending setFocus() message from another thread.

    General and Desktop
    2
    6
    834
    Loading More Posts
    • 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.
    • G
      goldstar2154 last edited by

      Hello everyone.
      Sorry for noob question.

      Problem: setting focus to widget from non-GUI threads.
      Details:
      OS: Win10 Pro x64
      QT: 5.9.1 x64 for msvc2015
      Comp: MSVC 2015 x64
      I have QGraphicsScene and QGraphicsView as central widget. Each widget on scene presents some device (like cash-box machine). Each device polled in own thread (not QT thread). When device got an error during polling i want to set focus to device's widget and show error-description. To do this i call

      DeviceWidget->setFocus(Qt::OtherFocusReason);
      

      And get debug assertion:

      QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. ...
      

      Assertion raise when my DeviceWidget send focus event to QGraphicsProxyWidget (that created when you add QWidget to QGraphicsScene).

      When i test release build all work fine. But i know that assertion exists :)

      So main question: how to change focus-widget from different threads (with minimal code)
      And if this is impossible can i ignore this debug assertion?

      Best regards.

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        In your thread, add and emit a signal.

        Then in your view class, connect it to a slot using Qt::QueuedConnection. In that slot, set the focus to the appropriate DeviceWidget.

        (Z(:^

        G 1 Reply Last reply Reply Quote 2
        • G
          goldstar2154 @sierdzio last edited by

          @sierdzio thx for response. This is non QObject threads. Is it possible to signal from them?

          sierdzio 1 Reply Last reply Reply Quote 0
          • sierdzio
            sierdzio Moderators @goldstar2154 last edited by

            @goldstar2154 said in Sending setFocus() message from another thread.:

            @sierdzio thx for response. This is non QObject threads. Is it possible to signal from them?

            That makes it a bit more tricky.

            You can try adding the signal in your view class, connecting (remember to set the connection to Queued explicitly), and then calling it from your thread. It is not an elegant solution, and I'm not sure if it will work, but there is a chance it will :)

            Signals are const, reentrant so with a queued connection you shouldn't run into concurrency issues.

            (Z(:^

            1 Reply Last reply Reply Quote 1
            • sierdzio
              sierdzio Moderators last edited by

              Hm, I wonder... perhaps it could work if you used the new signal-slot syntax. Then you could send the signal from your thread even though it is not a QObject, because function pointers are being connected. Maybe. It'll probably crash horribly or not compile. Here be dragons etc. ;)

              (Z(:^

              1 Reply Last reply Reply Quote 1
              • G
                goldstar2154 last edited by

                Finally i dont found any way to do this think and add to my code message dispather class (derived from QObject). All threads send messages into dispather and dispather can control GUI-thread throug signal/slot mechanics with Qt::QueuedConnection flag.
                Cheers)

                P.S. new slot syntax dont help in my case.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post