void SomeClass::mySlot(MyClass *var){
...
}
void SomeClass::SomeFunction(){
MyClass *myVar;
QPushButton *button = new QPushButton(this);
connect(button, SIGNAL(clicked()), this, SLOT(mySlot(myVar)));
}
我希望mySlot在单击按钮时接收myVar。 是否有可能做这样的事情?我不想在SomeClass中存储myVar指针。Qt。可以在连接SLOT()中使用局部变量吗?
更新(我的解决方案):
void SomeClass::mySlot(){
QPushButton *button = static_cast<QPushButton*>(sender());
MyClass *myVar = qobject_cast<MyClass*>(qvariant_cast<QObject *>(button->property("myVar")));
...
}
void SomeClass::SomeFunction(){
MyClass *myVar;
QPushButton *button = new QPushButton(this);
button->setProperty("myVar", QVariant::fromValue((QObject*)myVar));
connect(button, SIGNAL(clicked()), this, SLOT(mySlot()));
}
您的解决方案不可能工作。 'connect'调用将失败。 – TonyK 2012-02-03 11:37:00
我错了,而复制粘贴。我纠正了'connect'。谢谢你的评论! – Eddie 2012-02-03 11:39:11