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 pass data to the thread of a modal dialog
Forum Updated to NodeBB v4.3 + New Features

How to pass data to the thread of a modal dialog

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.2k 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
    gav007
    wrote on last edited by
    #1

    Here is what I did :

    MyObject * pObj = theSingleton.create();
    MyDialog dialog( pObj, this );
    dialog.exec();
    

    Since the instance of MyObject is created in the current thread, I get crash while accessing its data in the dialog thread. This is "normal" in the frame of Qt.
    MyObject is plenty of attributes of type QList, QString and QMap.

    How could I elegantly pass the MyObject data, created in the main thread, to the modal dialog ?

    Many thanks in adavance for your support.

    Best regards.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @gav007 said in How to pass data to the thread of a modal dialog:

      How could I elegantly pass the MyObject data, created in the main thread, to the modal dialog ?

      You can't have a dialog outside the main thread.
      Passing data around through thread boundaries works either with locking mechanisms or signals and slots

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      G 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @gav007 said in How to pass data to the thread of a modal dialog:

        How could I elegantly pass the MyObject data, created in the main thread, to the modal dialog ?

        You can't have a dialog outside the main thread.
        Passing data around through thread boundaries works either with locking mechanisms or signals and slots

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

        @Christian-Ehrlicher
        Many thanks.
        I prevent from the concurrent access to the data by design. Hence I avoid the use of locking mechanism.

        About signals and slots, the object to be passed is so full of data, I do not find elegant to pass all the data with signals.

        Is the problem of thread ownership solved with such a code :

        MyObject * pObj = theSingleton.create();
        MyDialog dialog( this );
        connect( this, &MainWindow::theObjectToHandle, &dialog, &MyDialog::HandleObject );
        connect( &dialog, &MyDialog::objectModified, this, &MainWindow::getObjectModified );
        emit theObjectToHandle( pObj );
        dialog.exec();
        

        and in the dialog :

        void MyDialog::HandleObject( MyObject * pObj )
        {
          ... modify the object
        
          emit objectModified( pObj );
          accept();
        }
        

        I will test this code, but I'm interrested by your answer anyway.

        Best regards.

        jsulmJ 1 Reply Last reply
        0
        • G gav007

          @Christian-Ehrlicher
          Many thanks.
          I prevent from the concurrent access to the data by design. Hence I avoid the use of locking mechanism.

          About signals and slots, the object to be passed is so full of data, I do not find elegant to pass all the data with signals.

          Is the problem of thread ownership solved with such a code :

          MyObject * pObj = theSingleton.create();
          MyDialog dialog( this );
          connect( this, &MainWindow::theObjectToHandle, &dialog, &MyDialog::HandleObject );
          connect( &dialog, &MyDialog::objectModified, this, &MainWindow::getObjectModified );
          emit theObjectToHandle( pObj );
          dialog.exec();
          

          and in the dialog :

          void MyDialog::HandleObject( MyObject * pObj )
          {
            ... modify the object
          
            emit objectModified( pObj );
            accept();
          }
          

          I will test this code, but I'm interrested by your answer anyway.

          Best regards.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @gav007 Why don't you simply pass the data by reference to the constructor of your dialog? There is no need to use signals/slot for this in this case.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          G 1 Reply Last reply
          2
          • jsulmJ jsulm

            @gav007 Why don't you simply pass the data by reference to the constructor of your dialog? There is no need to use signals/slot for this in this case.

            G Offline
            G Offline
            gav007
            wrote on last edited by gav007
            #5

            @jsulm Thanks for this suggestion.

            Do you mean that, if I change my original code as follow (changing the pointer to reference), the thread ownership of my object shall be solved ?

            MyObject * pObj = theSingleton.create();
            MyDialog dialog( *pObj, this );
            dialog.exec();
            

            Where the dialog constructor looks like this:

            MyDialog::MyDialog( MyObject & obj, QWidget *parent = nullptr )
            

            Regards

            jsulmJ 1 Reply Last reply
            0
            • G gav007

              @jsulm Thanks for this suggestion.

              Do you mean that, if I change my original code as follow (changing the pointer to reference), the thread ownership of my object shall be solved ?

              MyObject * pObj = theSingleton.create();
              MyDialog dialog( *pObj, this );
              dialog.exec();
              

              Where the dialog constructor looks like this:

              MyDialog::MyDialog( MyObject & obj, QWidget *parent = nullptr )
              

              Regards

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @gav007 Yes, like this, or as pointer.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              G 1 Reply Last reply
              0
              • jsulmJ jsulm

                @gav007 Yes, like this, or as pointer.

                G Offline
                G Offline
                gav007
                wrote on last edited by
                #7

                @jsulm said in How to pass data to the thread of a modal dialog:

                as pointer.

                That was I did in the original code and in such a case, I can tell that all the data of my object are not available to the dialog thread. This sounds normal to me since my object is owned by the main thread and not by the dialog thread.

                Do I miss something ?

                Regards

                jsulmJ 1 Reply Last reply
                0
                • G gav007

                  @jsulm said in How to pass data to the thread of a modal dialog:

                  as pointer.

                  That was I did in the original code and in such a case, I can tell that all the data of my object are not available to the dialog thread. This sounds normal to me since my object is owned by the main thread and not by the dialog thread.

                  Do I miss something ?

                  Regards

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @gav007 As @Christian-Ehrlicher already said: you can't run dialog outside of the UI thread. And in your code I can't see where you start other threads. So, why do you think your dialog is running in another thread? Dialogs do NOT run in their own threads, they run in the main/UI thread.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply
                  4
                  • jsulmJ jsulm

                    @gav007 As @Christian-Ehrlicher already said: you can't run dialog outside of the UI thread. And in your code I can't see where you start other threads. So, why do you think your dialog is running in another thread? Dialogs do NOT run in their own threads, they run in the main/UI thread.

                    G Offline
                    G Offline
                    gav007
                    wrote on last edited by gav007
                    #9

                    @jsulm OK, my mistake
                    I thought that the exec() call creates its own thread with its own event loop.

                    Then why all the QString, QList and QMap of my object are not available when processing the object in my dialog ?

                    PS: "Not accessible" is what my debugger said at the crash time

                    jsulmJ 1 Reply Last reply
                    0
                    • G gav007

                      @jsulm OK, my mistake
                      I thought that the exec() call creates its own thread with its own event loop.

                      Then why all the QString, QList and QMap of my object are not available when processing the object in my dialog ?

                      PS: "Not accessible" is what my debugger said at the crash time

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

                      @gav007 said in How to pass data to the thread of a modal dialog:

                      PS: "Not accessible" is what my debugger said at the crash time

                      Where exactly did your app crash? Somewhere inside your dialog? Sometimes the debugger fails to show content of variables in QtCreator, but this does not mean that they are not available in general.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gav007
                        wrote on last edited by
                        #11

                        I found my mistakes: a hide and seek game between destructor and copy constructor made my object data unavailable. "Not accessible" data were due to a former destruction (this highlight a very bad design ! ). 1st mistake.

                        Usually, when I met the "not accessible" issue from the debugger, it was due to invalid concurrent access. I made the 2d mistake to directly invoke this reason.

                        Well ! Many thanks to @jsulm and @Christian-Ehrlicher for their support.

                        Very instructive topic for me :-)

                        Best regards

                        1 Reply Last reply
                        2

                        • Login

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