QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...
-
@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)); -
@opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:
i think normally it's not safe to pass-by-reference across threads, right?
Depends who you ask. The chair of the C++ standard committee is of the opinion that as STL uses
const
as implying thread safety* the entire language should conform to that convention. In that case usingcons&
as argument is safe even across threads. On the other hand it's a jungle out there and it's very easy to break that convention so I would not bet my life on thread safety of const references*source: https://herbsutter.com/2013/01/01/video-you-dont-know-const-and-mutable/
-
@opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:
it's not safe to pass-by-reference across threads, right?
@SGaist already explained in this thread that the parameter is copied when using queued connection between threads, so it IS safe...