开发者

Qt issue passing arguments to slot

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 "开发者_StackOverflow中文版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

}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜