Passing another variable to a SLOT in QObject::connect
-
[quote author="peppe" date="1297240228"]In your (very simple) case, if it's applicable, you can just provide a default argument for the slot. Otherwise, simply create another slot that calls the setText one with the string you want.[/quote]Indeed. With a slot called setText(), providing a default argument can be misleading:
@myLabel->setText();@
What does that do?
Oh well. It's a style issue.
-
bq. Otherwise, simply create another slot that calls the setText one with the string you want.
Any way without creating whole new classes?
-
"QSignalMapper":http://doc.qt.nokia.com/4.7/qsignalmapper.html might also be an option.
-
to solve simple problem like yours I use this solution:
@
connect(button1, SIGNAL(clicked()), SLOT(buttonClicked());
connect(button2, SIGNAL(clicked()), SLOT(buttonClicked());
button1->setProperty("name", "button1");
button2->setProperty("name", "button2");
...
...
MyClass::buttonClicked()
{
QPushButton *button = sender();
if(button->property("name").toString() == "button1")
{
.....
.....
}
else if(button->property("name").toString() == "button2")
{
....
....
}
}
@ -
Luca, that is unsave code.
You don't know what kind of object sender() refers to, or if it is non-0 at all (direct call to buttonClicked()). So, you should at least check that before assuming this. Using a QSignalMapper is the safe way to go, and it still allows other ways to trigger the slot and still work correctly.
-
[quote author="Andre" date="1297288071"]Luca, that is unsave code.
You don't know what kind of object sender() refers to, or if it is non-0 at all (direct call to buttonClicked()). So, you should at least check that before assuming this. Using a QSignalMapper is the safe way to go, and it still allows other ways to trigger the slot and still work correctly.
[/quote]
Yes, I usually check if pointer is a push button with a dynamic_cast:
@
QPushButton button = dynamic_cast<QPushButton>(sender());
if(button==NULL)
{
return;
}
@Mine is only a fast example...
-
Hi there is a tool which do exactly what you need.
See "QSignalMapper":http://doc.trolltech.com/latest/qsignalmapper.html
There is good example how to use it.