Solved QDialog question
-
@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.
-
@DavidPL
OK, that's why the exact error message is so important! :)
So you have to search forqRegisterMetaType
, 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
orQString
, 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.
-
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 ... -
@DavidPL
Like @mpergand just wrote, because yours isdouble[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 typeSo if you change your code to use
QList<double>
for your array you won't have to register your own type. -
@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 :).
-
@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)
-
@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 ?
-
@mrjj Thanks a lot for the note haha, I appreciate it so much :).
-
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? -
@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.