PDA

View Full Version : Passing an integer to a slot



bizmopeen
27th October 2009, 21:37
Hello, all;

Does anyone know the best way to pass an integer to a slot? I'm currently having trouble doing so and am receiving a “no such slot” message when I run my program.
It compiles and runs fine if I don't try to pass anything to it (i.e. declare it “void”), and even compiles when I do; it's only upon execution that I get the error message. Here are the relevant code snippets I'm using for the signal connection and slot in the .cpp file:



int currentFreq1 = frequency_ch1_SB->value();

qDebug() << currentFreq1 << " Frequency value to pass to Start IO #1\n";

connect( taStartPB, SIGNAL( clicked() ), this, SLOT( startIO(currentFreq1) ));




void ConfigurationPage::startIO(int currentFreq1)
{
...

}


...and in the .h file, the slot is defined this way:



class ConfigurationPage : public QWidget
{
Q_OBJECT

public:
ConfigurationPage(QWidget *parent = 0);

private slots:
void startIO(int);

};


The specific error message I'm getting is: “No such slot ConfigurationPage::startIO(currentFreq1) in toneaud_pages.cpp:348
”. If anyone has any suggestions, I'd certainly appreciate it.

Thanks,

Allan

calhal
27th October 2009, 21:46
Try this:

connect( taStartPB, SIGNAL( clicked() ), this, SLOT( startIO(int) ));

bizmopeen
27th October 2009, 21:54
Try this:

connect( taStartPB, SIGNAL( clicked() ), this, SLOT( startIO(int) ));
Yeah, I thought of that as well, but then I get a "QObject::connect: Incompatible sender/receiver arguments
QPushButton::clicked() --> ConfigurationPage::startIO(int)
" message. Plus, if I don't tell the signal which integer to pass, how will the slot know which value to work with?

squidge
27th October 2009, 22:04
The signal must emit the integer which the slot will use, you can't specify the value in the connect call, you only specify the function signature there, and this signature must be the same as that specified in your header file.


connect( taStartPB, SIGNAL( clicked(int) ), this, SLOT( startIO(int) ));

You'll also notice that SIGNAL and SLOT are just macros which turn there argument into a string, therefore passing a variable or value is useless. If you look into the generated moc_*.cpp files you'll see these signatures inside the qt_meta_stringdata array.

calhal
27th October 2009, 23:29
Ups, I forgot about something ;)

axeljaeger
30th October 2009, 07:17
You can use QSignalMapper between your pushbutton and the receiver to add that integer to your data flow.

redkite
30th October 2009, 09:51
Instead of using a QSignalMapper, which I think don't suit you(the int is not always the same, you will need to call setMapping each time...), I suggest you simply create an additional slot (void) which will call startIO(currentFreq1). (and simply put startIO private)

So connect the QPushButton::clicked() to the additional slot:

QObject::connect(taStartPB, SIGNAL( clicked() ), this, SLOT(additionalSlot());
Then you can call startIO(int) :

void ConfigurationPage::additionalSlot()
{
int currentFreq1 = frequency_ch1_SB->value();
startIO(currentFreq1)
}

I think it 's a straightforward way, isn't it? :cool:

Hope to be useful...