14
votes

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

}
3

3 Answers

17
votes

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.

8
votes

The signal and the slot must have the same number and type(s) of argument(s), and you can only pass the argument(s) of the signal to the slot, not any variable or value that you want.

3
votes

I can see three problems with this.

Firstly, the clicked() signal is emitted by QPushButton (with no parameters), but you're trying to redefine it in your own class (with an int parameter). If you want to do this:

SignalClass* objectWithSignals = new SignalClass;
SlotClass* objectWithSlots = new SlotClass;
connect(objectWithSignals, SIGNAL(a()), objectWithSlots, SLOT(b()));

then you can only connect to the signals already defined in SignalClass. In other words, the signal a() must belong to SignalClass, not SlotClass.

(In fact, clicked() is defined in QPushButton's base class QAbstractButton.)

Secondly, inside the connect() function, you need to specify the signal and slot signatures with their parameter types. Where you have count inside the connect() function, it should be int.

And thirdly, there's a bracket missing in your call to connect: SIGNAL(clicked(count)).

Hope that helps.