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. QDialog question
QtWS25 Last Chance

QDialog question

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 6 Posters 4.6k 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.
  • D DavidPL

    I'll try to explain better. I have some objects moved into dedicated threads in order to avoid the MainWindow from freezing. One of those objects takes data from a gamepad. That data is meant to be displayed in a QDialog. The problem I'm having is that the QDialog breaks the app when I implement it in the mentioned object code (I include the .h and .cpp files of the QDialog, create a pointer, i.e Dialog *mydialog, and state: mydialog->setmodal(false); mydialog->exec.

    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @DavidPL

    Ok, then the updated answer is: all GUI elements have to live in the same (main/GUI) thread.

    you should use signals &slots to exchange data with the objects in the other threads.

    Regards

    Qt has to stay free or it will die.

    Taz742T D 2 Replies Last reply
    1
    • aha_1980A aha_1980

      @DavidPL

      Ok, then the updated answer is: all GUI elements have to live in the same (main/GUI) thread.

      you should use signals &slots to exchange data with the objects in the other threads.

      Regards

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #5

      @aha_1980
      If use signal slot which you recommended @aha_1980 do not use mydialog->exec(); use mydialog->show();

      Do what you want.

      1 Reply Last reply
      1
      • aha_1980A aha_1980

        @DavidPL

        Ok, then the updated answer is: all GUI elements have to live in the same (main/GUI) thread.

        you should use signals &slots to exchange data with the objects in the other threads.

        Regards

        D Offline
        D Offline
        DavidPL
        wrote on last edited by
        #6

        @aha_1980 Ah ok, I didn't know that. Thanks :). Only to know, imagine you need a lot of dialogs in your app or they are updating large amounts of data at a fast rate. In that case is likely to get frozen the mainwindow many times so is there any way to move some dialogs into other threads to avoid the mentioned issue?

        mrjjM 1 Reply Last reply
        0
        • D DavidPL

          @aha_1980 Ah ok, I didn't know that. Thanks :). Only to know, imagine you need a lot of dialogs in your app or they are updating large amounts of data at a fast rate. In that case is likely to get frozen the mainwindow many times so is there any way to move some dialogs into other threads to avoid the mentioned issue?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #7

          @DavidPL
          Hi
          Nothing GUI related can live in other threads.
          The normal design is to use Threads for the data processing. It will then emit
          signals to GUI thread to show something on screen.

          D 1 Reply Last reply
          2
          • mrjjM mrjj

            @DavidPL
            Hi
            Nothing GUI related can live in other threads.
            The normal design is to use Threads for the data processing. It will then emit
            signals to GUI thread to show something on screen.

            D Offline
            D Offline
            DavidPL
            wrote on last edited by
            #8

            @mrjj Ok I understand. But there is another problem I'm getting while emitting the signal. I want to pass an array from one object living in one of the other threads to the dialog. I connect the signal and slots but It says that the size of the array is too large to be put in qeue. So, how could I send the data then? (the array is only [1][21] size).

            JonBJ 1 Reply Last reply
            0
            • D DavidPL

              @mrjj Ok I understand. But there is another problem I'm getting while emitting the signal. I want to pass an array from one object living in one of the other threads to the dialog. I connect the signal and slots but It says that the size of the array is too large to be put in qeue. So, how could I send the data then? (the array is only [1][21] size).

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #9

              @DavidPL
              I have absolutely no idea, but have you tried passing the array via QVariant instead of as-is?

              D 1 Reply Last reply
              0
              • JonBJ JonB

                @DavidPL
                I have absolutely no idea, but have you tried passing the array via QVariant instead of as-is?

                D Offline
                D Offline
                DavidPL
                wrote on last edited by
                #10

                @JonB No, I haven't tried. I attach the code for the signal, slot and the connect line and I'll try the QVariant.
                Signal:

                void update_commandedcontrols(double commanded_controls[1][21]);
                

                Slot:

                void update_commandedcontrols(double commanded_controls[1][21]);
                

                connect:

                connect(gamepad_obj, SIGNAL(update_commandedcontrols(double[1][21])), this, SLOT(update_commandedcontrols(double[1][21])));
                
                JonBJ 1 Reply Last reply
                0
                • D DavidPL

                  @JonB No, I haven't tried. I attach the code for the signal, slot and the connect line and I'll try the QVariant.
                  Signal:

                  void update_commandedcontrols(double commanded_controls[1][21]);
                  

                  Slot:

                  void update_commandedcontrols(double commanded_controls[1][21]);
                  

                  connect:

                  connect(gamepad_obj, SIGNAL(update_commandedcontrols(double[1][21])), this, SLOT(update_commandedcontrols(double[1][21])));
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #11

                  @DavidPL
                  I don't know, but other people don't seem to be complain an array can't be passed. Or, they use things like std::vector.

                  You might like to give the exact text of whatever runtime error you say you're getting?

                  D 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @DavidPL
                    I don't know, but other people don't seem to be complain an array can't be passed. Or, they use things like std::vector.

                    You might like to give the exact text of whatever runtime error you say you're getting?

                    D Offline
                    D Offline
                    DavidPL
                    wrote on last edited by
                    #12

                    @JonB This is the error I'm getting:

                    QObject::connect: Cannot queue arguments of type 'double[1][21]'
                    (Make sure 'double[1][21]' is registered using qRegisterMetaType().)
                    
                    JonBJ 1 Reply Last reply
                    0
                    • D DavidPL

                      @JonB This is the error I'm getting:

                      QObject::connect: Cannot queue arguments of type 'double[1][21]'
                      (Make sure 'double[1][21]' is registered using qRegisterMetaType().)
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #13

                      @DavidPL
                      But David, earlier you wrote:

                      I connect the signal and slots but It says that the size of the array is too large to be put in qeue

                      Where does it say that it is "too large"? What you report now is completely different. And it tells you what you can do about it....

                      D 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @DavidPL
                        But David, earlier you wrote:

                        I connect the signal and slots but It says that the size of the array is too large to be put in qeue

                        Where does it say that it is "too large"? What you report now is completely different. And it tells you what you can do about it....

                        D Offline
                        D Offline
                        DavidPL
                        wrote on last edited by
                        #14

                        @JonB Yeah you're right, I don't know why but I thought it was related to the size, sorry. Nevertheless I'm newbie and I'b be so grateful if you could explain to me how can I solve that error and what does it mean.

                        JonBJ 2 Replies Last reply
                        0
                        • D DavidPL

                          @JonB Yeah you're right, I don't know why but I thought it was related to the size, sorry. Nevertheless I'm newbie and I'b be so grateful if you could explain to me how can I solve that error and what does it mean.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #15

                          @DavidPL
                          OK, that's why the exact error message is so important! :)
                          So you have to search for qRegisterMetaType, and read like:

                          • http://doc.qt.io/qt-5/qmetatype.html#details
                          • http://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType
                          • http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE

                          Basically, Qt needs to know about the types of variables you want to pass through signals/slots. It naturally supports simple types like int or QString, but once you want to pass something like your 'double[1][21] you have to tell it about that before you can pass it.

                          enum QMetaType::Type (in http://doc.qt.io/qt-5/qmetatype.html#details) shows you what types it automatically supports. Either you convert/pass your type to one of these, if that's possible, or you have to do the work to declare your meta-type to Qt first.

                          I can't give you an exact example 'coz I don't use it (and I'm not C++), but there are plenty around if you search for those words.

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            mpergand
                            wrote on last edited by
                            #16

                            Why not use a QList<double> ?

                            Example:

                            class Receiver : public QObject
                            {
                                Q_OBJECT
                            
                                public slots:
                                    void update(QList<double> data)
                                        {
                                        qDebug()<<"update with data: "<<data;
                                        }
                            };
                            
                            class Sender : public QObject
                            {
                                Q_OBJECT
                            
                                public:
                            
                                void sendData()
                                    {
                                    QList<double> d;
                                    d<<1.2<<2.5<<12.78;  // list of double to send
                                    emit update(d);
                            
                                    }
                            
                                signals:
                            
                                    void update(QList<double>);
                            };
                            
                            int main(int argc, char *argv[])
                            {
                                Receiver r;
                                  Sender s;
                            
                                  QObject::connect(&s,&Sender::update, &r, &Receiver::update);
                                  s.sendData(); // send data to r
                            
                                  return 0;
                            }
                            
                            #include "main.moc"  // only needed because QObjects create in main.cpp, not in header .h
                            

                            On the other hand,
                            double[1][21] is a single dim array !
                            and in C when you pass an array as a argument you 're passing an address to the first element of the array.
                            How do you create this array, cause it seems odd ...

                            D 1 Reply Last reply
                            4
                            • D DavidPL

                              @JonB Yeah you're right, I don't know why but I thought it was related to the size, sorry. Nevertheless I'm newbie and I'b be so grateful if you could explain to me how can I solve that error and what does it mean.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #17

                              @DavidPL
                              Like @mpergand just wrote, because yours is double[1][...] it's only really holding a single list of doubles.

                              Then http://doc.qt.io/qt-5/qmetatype.html#details tells you:

                              Some types are registered automatically and do not need this macro:
                              ...
                              QList<T>, QVector<T>, QQueue<T>, QStack<T>, QSet<T> or QLinkedList<T> where T is a registered meta type

                              So if you change your code to use QList<double> for your array you won't have to register your own type.

                              1 Reply Last reply
                              2
                              • M mpergand

                                Why not use a QList<double> ?

                                Example:

                                class Receiver : public QObject
                                {
                                    Q_OBJECT
                                
                                    public slots:
                                        void update(QList<double> data)
                                            {
                                            qDebug()<<"update with data: "<<data;
                                            }
                                };
                                
                                class Sender : public QObject
                                {
                                    Q_OBJECT
                                
                                    public:
                                
                                    void sendData()
                                        {
                                        QList<double> d;
                                        d<<1.2<<2.5<<12.78;  // list of double to send
                                        emit update(d);
                                
                                        }
                                
                                    signals:
                                
                                        void update(QList<double>);
                                };
                                
                                int main(int argc, char *argv[])
                                {
                                    Receiver r;
                                      Sender s;
                                
                                      QObject::connect(&s,&Sender::update, &r, &Receiver::update);
                                      s.sendData(); // send data to r
                                
                                      return 0;
                                }
                                
                                #include "main.moc"  // only needed because QObjects create in main.cpp, not in header .h
                                

                                On the other hand,
                                double[1][21] is a single dim array !
                                and in C when you pass an array as a argument you 're passing an address to the first element of the array.
                                How do you create this array, cause it seems odd ...

                                D Offline
                                D Offline
                                DavidPL
                                wrote on last edited by
                                #18

                                @mpergand Yeah I know it's a single dimension array currently but in the near future it may be multidimensional, and that's the reason I decided to use that.

                                I create the array in this line: double commanded_controls [1][21];

                                But if it's too difficult to use the array I'll try the QList for now. Thank you so much for the example :).

                                mrjjM M 2 Replies Last reply
                                0
                                • D DavidPL

                                  @mpergand Yeah I know it's a single dimension array currently but in the near future it may be multidimensional, and that's the reason I decided to use that.

                                  I create the array in this line: double commanded_controls [1][21];

                                  But if it's too difficult to use the array I'll try the QList for now. Thank you so much for the example :).

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  @DavidPL
                                  Hi
                                  just as a note:
                                  There is (often) very little reason to use a traditional
                                  c array unless you must call older c code.
                                  I recommend you new friend be
                                  http://www.learncpp.com/cpp-tutorial/6-15-an-introduction-to-stdarray/
                                  for a fixed size, very close to c array but with benefits.
                                  Along with std::vector etc. ( QList, QMap )

                                  Anyway, here QList seems better as no need to register anything. (as the others mentions)

                                  D 1 Reply Last reply
                                  2
                                  • D DavidPL

                                    @mpergand Yeah I know it's a single dimension array currently but in the near future it may be multidimensional, and that's the reason I decided to use that.

                                    I create the array in this line: double commanded_controls [1][21];

                                    But if it's too difficult to use the array I'll try the QList for now. Thank you so much for the example :).

                                    M Offline
                                    M Offline
                                    mpergand
                                    wrote on last edited by mpergand
                                    #20

                                    @DavidPL said in QDialog question:

                                    @mpergand Yeah I know it's a single dimension array currently but in the near future it may be multidimensional, and that's the reason I decided to use that.

                                    Not a big deal ;)

                                    Replace QList<double> with QList<QList<Double> that's all !

                                    In my example above, to initialize the list of list :

                                    QList<QList<double>> d;
                                     d<<QList<double>{1.2,2.5,12.78};  // first row
                                     d<<QList<double>{11.2,2.15,1002.78};  // second row
                                    // etc
                                     emit update(d);
                                    

                                    In the receiver:

                                    void update(QList<QList<double>> data)
                                                {
                                                qDebug()<<"update with data: "<<data;
                                                }
                                    

                                    The debug ouput :

                                    update with data:  ((1.2, 2.5, 12.78), (11.2, 2.15, 1002.78))
                                    

                                    You can retrieve each element like that:

                                    double val=data[1][2]; // = 1002.78
                                    

                                    easy isn't it ?

                                    JonBJ D 2 Replies Last reply
                                    1
                                    • mrjjM mrjj

                                      @DavidPL
                                      Hi
                                      just as a note:
                                      There is (often) very little reason to use a traditional
                                      c array unless you must call older c code.
                                      I recommend you new friend be
                                      http://www.learncpp.com/cpp-tutorial/6-15-an-introduction-to-stdarray/
                                      for a fixed size, very close to c array but with benefits.
                                      Along with std::vector etc. ( QList, QMap )

                                      Anyway, here QList seems better as no need to register anything. (as the others mentions)

                                      D Offline
                                      D Offline
                                      DavidPL
                                      wrote on last edited by
                                      #21

                                      @mrjj Thanks a lot for the note haha, I appreciate it so much :).

                                      1 Reply Last reply
                                      0
                                      • M mpergand

                                        @DavidPL said in QDialog question:

                                        @mpergand Yeah I know it's a single dimension array currently but in the near future it may be multidimensional, and that's the reason I decided to use that.

                                        Not a big deal ;)

                                        Replace QList<double> with QList<QList<Double> that's all !

                                        In my example above, to initialize the list of list :

                                        QList<QList<double>> d;
                                         d<<QList<double>{1.2,2.5,12.78};  // first row
                                         d<<QList<double>{11.2,2.15,1002.78};  // second row
                                        // etc
                                         emit update(d);
                                        

                                        In the receiver:

                                        void update(QList<QList<double>> data)
                                                    {
                                                    qDebug()<<"update with data: "<<data;
                                                    }
                                        

                                        The debug ouput :

                                        update with data:  ((1.2, 2.5, 12.78), (11.2, 2.15, 1002.78))
                                        

                                        You can retrieve each element like that:

                                        double val=data[1][2]; // = 1002.78
                                        

                                        easy isn't it ?

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by JonB
                                        #22

                                        @mpergand

                                        Replace QList<double> with QList<QList<Double> that's all !

                                        The way I read the docs meta types description, I did not think QList<QList<>> was covered as a "built-in type" acceptable without adding your own Meta. You saying that does work?

                                        1 Reply Last reply
                                        0
                                        • M mpergand

                                          @DavidPL said in QDialog question:

                                          @mpergand Yeah I know it's a single dimension array currently but in the near future it may be multidimensional, and that's the reason I decided to use that.

                                          Not a big deal ;)

                                          Replace QList<double> with QList<QList<Double> that's all !

                                          In my example above, to initialize the list of list :

                                          QList<QList<double>> d;
                                           d<<QList<double>{1.2,2.5,12.78};  // first row
                                           d<<QList<double>{11.2,2.15,1002.78};  // second row
                                          // etc
                                           emit update(d);
                                          

                                          In the receiver:

                                          void update(QList<QList<double>> data)
                                                      {
                                                      qDebug()<<"update with data: "<<data;
                                                      }
                                          

                                          The debug ouput :

                                          update with data:  ((1.2, 2.5, 12.78), (11.2, 2.15, 1002.78))
                                          

                                          You can retrieve each element like that:

                                          double val=data[1][2]; // = 1002.78
                                          

                                          easy isn't it ?

                                          D Offline
                                          D Offline
                                          DavidPL
                                          wrote on last edited by
                                          #23

                                          @mpergand since you know how to do it yes, easy haha. Thank you again for your help and time, that's really nice of you.

                                          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