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. Multithreading Qt: Cannot access a Qt object after app.exec()
Forum Updated to NodeBB v4.3 + New Features

Multithreading Qt: Cannot access a Qt object after app.exec()

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.6k Views 3 Watching
  • 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.
  • J Offline
    J Offline
    justiliang
    wrote on last edited by
    #1

    So I have a class called QtWindowInit, that initializes some QWidgets in a seperate thread. It instantiates the QApplicaton object in this seperate thread. In the example below it initializes a LED that I created.

    
    class QtWindowInit {
    
    public:
    
    inline void Init() {
    
        _qtThread = boost::thread(&QtWindowInit::StartThread, this);
    
    }
    
    inline void StartThread() {
    
        char *argv[] = {"program name", "arg1", "arg2", NULL};
        int argc = sizeof(argv) / sizeof(char*) - 1;
    
        QApplication app(argc, argv);
    
        // Some code to initialize the window
    
        _led = new QGraphicsEllipseItem(50, 50, 50, 50);
        _led->setBrush(Qt::gray);
    
        // Some code to add it to the window and then call show()
    
        app.exec();
    
      }
    

    Then I have a main:

    int main(int argc, char *argv[]) {
       
        QtWindowInit LED();
        LED.StartThread();
    
        LED._led->setBrush(Qt::red); // This line of code causes the gui to show nothing
    
    }
    

    With the above line of code where I set the brush color, the gui does not even show up. It doesn't crash and I don't get any errors. Without that line, the gui works and the led shows up but it is gray. Could it be because I am trying to set the color after app.exec()? Is there a work around?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      Hi and welcome to devnet,

      keep in mind that QApplication::exec() never returns.

      In your code everything is executed in the main thread; Qt suggest to create the application object and the GUI in the main thread.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @justiliang said:

        LED._led->setBrush(Qt::red); // This line of code causes the gui to show nothing
        

        As I said in http://forum.qt.io/topic/53905/ , the methods of GUI-related objects can only be called in the thread that created QApplication.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        J 1 Reply Last reply
        1
        • JKSHJ JKSH

          @justiliang said:

          LED._led->setBrush(Qt::red); // This line of code causes the gui to show nothing
          

          As I said in http://forum.qt.io/topic/53905/ , the methods of GUI-related objects can only be called in the thread that created QApplication.

          J Offline
          J Offline
          justiliang
          wrote on last edited by justiliang
          #4

          @JKSH Do you know of a work around for my case? I need somehow be able to change the color outside of the thread.

          JKSHJ 1 Reply Last reply
          0
          • J justiliang

            @JKSH Do you know of a work around for my case? I need somehow be able to change the color outside of the thread.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @justiliang said:

            @JKSH Do you know of a work around for my case? I need somehow be able to change the color outside of the thread.

            Yes, there are 2 ways to do handle this use-case:

            1. Emit a signal from your secondary thread.
            2. Call QMetaObject::invokeMethod() in your secondary thread.

            For both ways listed above, use a Qt::QueuedConnection to connect your request to a QObject that lives in the GUI thread. When that QObject receives your request, it should call setBrush() for you. This way, setBrush() is executed in the correct thread.

            See also the section about QObject thread affinity.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0

            • Login

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