Passing data between signals and slots
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()
It doesn't work this way. You can't execute code inside
SLOT(). You need to create a signal that emits a QString, say
void isIteratedStr(QString s);and then connect this signal to the slot you mentioned:
connect(myclassobjpointer, SLOT(isIteratedStr(QString)), ui->labelname, SLOT(setText(QString)));
@Wieland How to pass integer instead of string? QString::number()?
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.
@Wieland I am trying to connect signal from one thread to slot in second thread, it can be done this way you described?
@Zola Yes, connections are thread-safe. No problem.
@Wieland Thanks man!
@Zola You're welcome :-)
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(...).
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.