Qt issue passing arguments to slot

user375566 picture user375566 · Nov 12, 2010 · Viewed 19.5k times · Source

I can't seem to pass an argument to a slot. If I don't pass an argument, the function rolls through fine. If I pass an argument (integer), I get the errors "No such name type" and "No such slot" when I compile.

In my header, I declare:

private slots:
void addButton(int);
signals:
void clicked(int)

in my Main.cpp, I do:

int count;
int count = 0;
QPushButton* button = new QPushButton("Button");
_layout->addWidget(button);
connect(button, SIGNAL(clicked(count), this, SLOT(addButton(count)));

....

void Main::addButton(int count) {

//do stuff with count

}

Answer

cgmb picture cgmb · Nov 12, 2010

Sebastian is correct that you cannot do this in the way you're trying, however Qt does provide a class that gives you the functionality you want.

Check out the QSignalMapper. It allows you to associate an integer with an object/signal pair. You then connect to its signals instead of directly to the button.