简体   繁体   中英

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 "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.

信号和插槽必须具有相同数量和类型的参数,并且您只能将信号的参数传递给插槽,而不是您想要的任何变量或值。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM