void SomeClass::mySlot(MyClass *var){
...
}
void SomeClass::SomeFunction(){
MyClass *myVar;
QPushButton *button = new QPushButton(this);
connect(button, SIGNAL(clicked()), this, SLOT(mySlot(myVar)));
}
ボタンをクリックしたときに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
コピー貼り付けの間違いでした。私は 'connect'を修正しました。ご意見ありがとうございます! – Eddie