Can i use reference in signal slots
-
@
myfirst::myfirst(QObject *parent) :
QObject(parent)
{
connect(this,SIGNAL(set(QString)),this,SLOT(seting(QString),Qt::QueuedConnection);
connect(this,SIGNAL(set(QString)),this,SLOT(seting2(QString)),Qt::QueuedConnection);}
void myfirst::seting(const QString &slot1)
{
qDebug()<<&slot1;
}void myfirst::seting2( const QString &slot2)
{
qDebug()<<&slot2;
}void myfirst::hjk(QString & j) //this is mu public method just emitting signal
{
emit set(j);
}
@
If i am not use reference then both printing same addressesif i use reference then printing different addresses.
what is the difference
-
Is the address important to you? The actual string data will be the same in both slots.
Is your signal marked as const, does it pass const QString reference?
-
If I am not put const then those slots are not connecting.
And second thing I am thinking about memory management. Please tell me .. If you know
And thanks for replay....
-
I don't understand your answer, pleae rephrase.
QString will manage the memory for you, it is implicitly shared (copy on write). You do not need to worry about this at all, it just works. QStrings should be passed by const references whenever possible (const QString &something), including signals, slots, functions.
-
It is creating two temporary memory addresses .
So it will causes the time complexity problem. -
Well, you are using QueuedConnections, so don't be surprised.
I still fail to see any problem here, possibly I am too dumb for this. Or maybe it's the weekend-brain ;-)
-
Hi,
If you use a queued connection, Qt will copy your signal arguments. You don't need to worry about memory management (unless your argument is a pointer).
-
So is it good or bad using references in signal /slot
-
[quote author="praneeth007" date="1405928051"]So is it good or bad using references in signal /slot [/quote]
In case of most of non-QObject classes: it is good and recommended practice.
-
Just use const refernces. Qt makes it work :-)
-
why const compulsory
-
[quote author="praneeth007" date="1406010712"]why const compulsory [/quote]
Without it, you would keep modifying the original string.
-
I want to modify those
-
[quote author="praneeth007" date="1406016204"]I want to modify those[/quote]You can't do that through signals and slots.