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. Sending setFocus() message from another thread.

Sending setFocus() message from another thread.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.1k 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.
  • G Offline
    G Offline
    goldstar2154
    wrote on last edited by
    #1

    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
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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
      2
      • sierdzioS sierdzio

        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.

        G Offline
        G Offline
        goldstar2154
        wrote on last edited by
        #3

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

        sierdzioS 1 Reply Last reply
        0
        • G goldstar2154

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

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @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
          1
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            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
            1
            • G Offline
              G Offline
              goldstar2154
              wrote on last edited by
              #6

              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
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved