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