PDA

View Full Version : Easy Signal-Slot-Problem



nearlyNERD
6th March 2010, 21:25
Hey,

I have a small problem:

I want to connect my QPushButtons to a Slot, which needs a int as argument, so I tried:


QObject::connect(Level1, SIGNAL(clicked()), parent, SLOT(StartLevel(1)));
QObject::connect(Level2, SIGNAL(clicked()), parent, SLOT(StartLevel(2)));
QObject::connect(Level3, SIGNAL(clicked()), parent, SLOT(StartLevel(3)));

But as an application output, I get:


Object::connect: No such slot StartWindow::StartLevel(1)
Object::connect: No such slot StartWindow::StartLevel(2)
Object::connect: No such slot StartWindow::StartLevel(3)

My Slot in a QMainWindow:

void StartWindow::StartLevel(int _level)
{
Level *level = new Level(_level);
qDebug() << this->focusWidget();

this->setCentralWidget(level);
level->grabKeyboard();

}


Is there a easy way to connect the clicked()-Signal to a Slot, which needs an int-argument?

Thanks for your answer...

nearlyNERD

squidge
6th March 2010, 23:13
Use QSignalMapper. You'll connect all your buttons to the mapper and give each one an Id. Then when the button is clicked, it will call your function with that Id as parameter.

toutarrive
6th March 2010, 23:33
QObject::connect(Level1, SIGNAL(clicked()), parent, SLOT(StartLevel()))


The signature of a signal must match the signature of the receiving slot.