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. How to "cast" to the main thread from another?
Forum Update on Monday, May 27th 2025

How to "cast" to the main thread from another?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 569 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by
    #1

    Hi,
    I am creating a new thread once I click on a button in my program.
    Inside this new created thread(-object) I call a method on an object

    void MyThread::run(){
        someObject->execute();
    }
    

    where I do

    void SomeClass::execute(){
        someQWidget->setEnabled(false);
    }
    

    And the program crashes, because I cannot call UI methods from this non-main thread.
    So the question is basically: How can I "say" the main thread, that he should call this method instead? I have to cast to the main thread from the created thread and execute this method there (would be my Idea to solve this). How can I do this?
    Thanks for answers!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Emit a signal from the 2nd thread and connect it to a slot in the main thread that calls your UI methods

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      NiagarerN 1 Reply Last reply
      3
      • VRoninV VRonin

        Emit a signal from the 2nd thread and connect it to a slot in the main thread that calls your UI methods

        NiagarerN Offline
        NiagarerN Offline
        Niagarer
        wrote on last edited by
        #3

        @VRonin
        Works perfectly fine, thank you!!
        Can you explain, why this works? I now emit a signal in my custom thread that is connected to a slot in another class. Is it because I only instaciate objects of this other class in the main thread? So only the main thread holds these objects and so the main thread will execute the slot of the objects of this other class?

        VRoninV 1 Reply Last reply
        0
        • NiagarerN Niagarer

          @VRonin
          Works perfectly fine, thank you!!
          Can you explain, why this works? I now emit a signal in my custom thread that is connected to a slot in another class. Is it because I only instaciate objects of this other class in the main thread? So only the main thread holds these objects and so the main thread will execute the slot of the objects of this other class?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @Niagarer said in How to "cast" to the main thread from another?:

          So only the main thread holds these objects and so the main thread will execute the slot of the objects of this other class?

          Correct

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3

          • Login

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