Passing data between signals and slots
-
wrote on 14 Jan 2016, 13:11 last edited by Zola
I have one class called myclass, which has member function counter(), and signal isIterrated()
That member function counter() iterates integer and when iterated it emits signal isItterated. Only I don't understand is how to pass a that counter integer value through signal isIterrated() to slot setText(QString::number(that_int)
I tried with this but it won't work
connect(myclassobjpointer, SLOT(isIterrated(int)), ui->labelname, SLOT(setText(QString::number(int)));
Can someone help me with passing data between signals and slots, I am confused.
I get error:
QObject::connect: No such signal for myclass::isItterated()
-
I have one class called myclass, which has member function counter(), and signal isIterrated()
That member function counter() iterates integer and when iterated it emits signal isItterated. Only I don't understand is how to pass a that counter integer value through signal isIterrated() to slot setText(QString::number(that_int)
I tried with this but it won't work
connect(myclassobjpointer, SLOT(isIterrated(int)), ui->labelname, SLOT(setText(QString::number(int)));
Can someone help me with passing data between signals and slots, I am confused.
I get error:
QObject::connect: No such signal for myclass::isItterated()
wrote on 14 Jan 2016, 13:19 last edited by A Former User@Zola Hi!
SLOT(setText(QString::number(int))
It doesn't work this way. You can't execute code inside
SLOT()
. You need to create a signal that emits a QString, sayvoid isIteratedStr(QString s);
and then connect this signal to the slot you mentioned:connect(myclassobjpointer, SLOT(isIteratedStr(QString)), ui->labelname, SLOT(setText(QString)));
-
@Zola Hi!
SLOT(setText(QString::number(int))
It doesn't work this way. You can't execute code inside
SLOT()
. You need to create a signal that emits a QString, sayvoid isIteratedStr(QString s);
and then connect this signal to the slot you mentioned:connect(myclassobjpointer, SLOT(isIteratedStr(QString)), ui->labelname, SLOT(setText(QString)));
wrote on 14 Jan 2016, 13:20 last edited by Zola@Wieland How to pass integer instead of string? QString::number()?
-
wrote on 14 Jan 2016, 13:25 last edited by
You have 3 options:
- What I described above.
- Create a proxy object with a slot that takes an integer, transforms it into a string and then emits this string.
- Create a new label class that inherits QLabel and add your own setTextFromInt(int) slot to that class.
-
wrote on 14 Jan 2016, 13:34 last edited by
@Wieland I am trying to connect signal from one thread to slot in second thread, it can be done this way you described?
-
@Wieland I am trying to connect signal from one thread to slot in second thread, it can be done this way you described?
wrote on 14 Jan 2016, 13:34 last edited by@Zola Yes, connections are thread-safe. No problem.
-
@Zola Yes, connections are thread-safe. No problem.
wrote on 14 Jan 2016, 13:44 last edited by@Wieland Thanks man!
-
wrote on 14 Jan 2016, 13:45 last edited by
@Zola You're welcome :-)
-
wrote on 15 Jan 2016, 08:35 last edited by Zola
I am having problem passing QPixmap from one thread via emit, to main thread's ui->label slot setPixmap, is there a difference between passing QString and QPixmap objects?
I found this 'All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.' at Qt site
-
Yes, UI related classes should only be used in the GUI thread.
You can pass the pixmap as binary data (QByteArray or uchar*) and then create the QPixmap in the GUI thread using loadFromData(...). -
wrote on 15 Jan 2016, 10:12 last edited by A Former User
Please keep in mind that sending large objects via signals by value might decrease the performance of your program. So avoid copying large objects unless you really need local copies. For a large object it's better to have only a single copy, protect access to its state via mutexes (et al.) and pass around references.
3/11