QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...
-
QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...
//------------------------------------------------ 1st time: (this is all in non-mainThread) QMetaObject::invokeMethod(g_pMainWin, "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data)); //// namespace QtMetaTypePrivate { template <typename T, bool Accepted = true> struct QMetaTypeFunctionHelper { static void *Construct(void *where, const void *t) { if (t) return new (where) T(*static_cast<const T*>(t)); //// Data::Data(const Data& other) {
//------------------------------------------------ 2nd time:(this is all in mainThread) void DataWidget::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a) { if (_c == QMetaObject::InvokeMetaMethod) { auto *_t = static_cast<DataWidget *>(_o); Q_UNUSED(_t) switch (_id) { case 0: _t->slotUpdate((reinterpret_cast< Data()>(_a[1]))); break; //// Data::Data(const Data& other) {
-
Q_DECLARE_METATYPE(Data); //int id = qRegisterMetaType<Data>("Data"); LONG WINAPI CallBack(const Data &data) {//non-mainThread assert(g_pMainWin && g_pMainWin ->GetDataWidget()); bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data)); assert(b); return 0; }
-
Hi,
Why is your slot getting a copy of Data while the other method uses a const reference ?
-
@opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:
@SGaist i have to pass-by-value when multi-threads, right?any better way?
That formulation is way too generic. With Qt signals and slots, the internal mechanism does a copy before sending the data to the other thread.
Also, I didn't wrote about your callback, I wrote about the
slotUpdate
slot. -
@SGaist
bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));
public slots:
void slotUpdate(Data data)pass-by-value should write like the above, right? if so, i donot know why it called twice the: Data::Data(const Data& other)
-
The first copy comes from the fact that you are using a queued connection which means that the data passed to it must be copied.
Then your
slotUpdate
method pass data by value which means that it is a copy again. Hence the two copies. -
@opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:
void slotUpdate(const Data& data)//is this ok???
Yes it is and this is what @SGaist already suggested...
-
-
class DataWidget : public QWidget
{
Q_OBJECT
public slots:
void slotUpdate(const Data& data)//changed to ref
{
update(data);
}However, i found these 2 style, by Data or const Data&, they both work, and call Data::Data(const Data& other) 1 time.
bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));//why this works while the slot is const Data&
bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(const Data&, data)); -
another question, i found:
//Q_DECLARE_METATYPE(Data);//i can delete this line, and only with qRegisterMetaType that will still work...//int id = qRegisterMetaType<Data>("Data"); //qRegisterMetaType<Data>("Data");//which should i use, as i pass const Data& in slotUpdate... qRegisterMetaType<Data>("const Data&");//which should i use, as i pass const Data& in slotUpdate...
-
@opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:
why this works while the slot is const
Why should it not? data will be const in the called slot. It doesn't matter whether it is const or not in the caller.
There is no need to register "const Data&". If Data is registered everything else (const, &, *, ...) is simply an "attribute" for an already known type. It is like with the compiler: if the compiler knows what Data is it knows what const Data& is.
-
@opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:
so i can always pass-by-ref in Slot, whether it's in one-thread or two-thread
be careful that you do not explicitly define the connection as a DirectConnection. I'm not 100% sure, but I think then it will be passed by reference even across threads.
-
@J.Hilk said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:
e careful that
actually even i use "const Data&" as slot's param, it's still safe while signal and slot are in different slots.
Data::Data(const Data& other) is called 1 time in invokeMethod
bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(const Data&, data));