PDA

View Full Version : pass data to SLOT



Surfman19
9th July 2015, 08:44
Hi,

I would like to pass an additional boolean parameter with the following slot:


QObject::connect(fs, SIGNAL(fileChanged(QString)), this, SLOT(readInfo(QString)));


I saw QSignalMapper might be the the way to go...can you show an example?

Thanks,
Surf

yeye_olive
9th July 2015, 09:36
From the documentation of QSignalMapper, line 2 of the detailed description (emphasis is mine):

This class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal.
This will not do for your fileChanged(QString) signal.

You have to implement the solution yourself. Connect fileChanged(QString) to a slot (in some "proxy" QObject) with a QString parameter only, which then emits another signal adding the boolean value you want.

anda_skoa
9th July 2015, 09:37
No, signal mapper does not do this at all.

It is used for connecting multiple signal sources to the mapper and then the mapper to a slot and have it pass an identifier or data associated with the original sender.

What is it that you want to achieve?

Cheers,
_

Surfman19
9th July 2015, 09:49
i would like to pass a bool as shown here (written as pseudo like code, since it will not compile):


void MainWindow::onReadStdOut() {
bool info = true; // info will be set by another function
QObject::connect(fs, SIGNAL(fileChanged(QString)), this, SLOT(readInfo(info, QString)));
}


my current solution (which works fine):


void MainWindow::onReadStdOut() {
info = true; // info will be set by another function
QObject::connect(fs, SIGNAL(fileChanged(QString)), this, SLOT(readInfo(QString)));
}

void MainWindow::readInfo(QString filename) {
// here i can read info, bc its a member variable
}

yeye_olive
9th July 2015, 10:05
I cannot see what is wrong with your current solution. As far as I know, 'info' seems to be a part of the state of MainWindow, just like the background color of the window, for instance. Why should any of them be passed to readInfo() as a parameter?

anda_skoa
9th July 2015, 10:07
Do you want to associate a boolean with the "fs" object?
And the boolean is changed by some other code during runtime?

Cheers,
_

Surfman19
9th July 2015, 10:28
the bool is changed by information from ReadStdOut...

anda_skoa
9th July 2015, 12:28
If the data is not associated with the "fs" object, then a member variable is definitely the way to go.

Btw, onReadStdOut() sounds like a slot, be sure you really want to do the connect() in there, i.e. every time the slot is invoked.

Cheers,
_