February 9, 2011

manasij7479 manasij7479
Lab Rat
12 posts

Passing another variable to a SLOT in QObject::connect

 

Suppose there are two widgets, a QPushButton and a QLabel
How can I write the connecting code, so that when the button is pressed down, a specific string is displayed in the Label?

  1. QObject::connect(....,SIGNAL(pressed(),......,SLOT(setText("My String")));

shows an error…
If I put:
  1. QObject::connect(....,SIGNAL(pressed(),......,SLOT(setText(QString)));

where would the value for the string come from?
…I am just starting to learn using Qt…

 Signature 

“Error, no keyboard — press F1 to continue.”

14 replies

February 9, 2011

peppe peppe
Ant Farmer
1025 posts

Both statements are invalid.

In short:

  • connect() wants only the SIGNATURE of the signal/slot (without the return type), not the argument names, nor anything else;
  • a slot cannot have more arguments than the ones carried by the signal you’re connecting to it (where should they come from?);
  • passing values inside connect() is an error, although it’s pretty clear what you’d like to do;
  • simply use a “proxy” slot that calls the target slot with the arguments, or read the docs about QSignalMapper.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

February 9, 2011

manasij7479 manasij7479
Lab Rat
12 posts

simply use a “proxy” slot that calls the target slot with the arguments….

How do I do that?

 Signature 

“Error, no keyboard — press F1 to continue.”

February 9, 2011

Franzk Franzk
Lab Rat
830 posts

  1. //...
  2.     connect(button, SIGNAL(pressed)), SLOT(handleButtonPress()));
  3. //...
  4.  
  5. void MyClass::handleButtonPress()
  6. {
  7.     someLabel->setText("My String");
  8. }

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

February 9, 2011

peppe peppe
Ant Farmer
1025 posts

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.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

February 9, 2011

Franzk Franzk
Lab Rat
830 posts

peppe wrote:
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.
Indeed. With a slot called setText(), providing a default argument can be misleading:

  1. myLabel->setText();

What does that do?

Oh well. It’s a style issue.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

February 9, 2011

manasij7479 manasij7479
Lab Rat
12 posts

Otherwise, simply create another slot that calls the setText one with the string you want.

Any way without creating whole new classes?

 Signature 

“Error, no keyboard — press F1 to continue.”

February 9, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3130 posts

QSignalMapper [doc.qt.nokia.com] might also be an option.

February 9, 2011

Luca Luca
Ant Farmer
589 posts

to solve simple problem like yours I use this solution:

  1. connect(button1, SIGNAL(clicked()), SLOT(buttonClicked());
  2. connect(button2, SIGNAL(clicked()), SLOT(buttonClicked());
  3. button1->setProperty("name", "button1");
  4. button2->setProperty("name", "button2");
  5. ...
  6. ...
  7. MyClass::buttonClicked()
  8. {
  9.    QPushButton *button = sender();
  10.    if(button->property("name").toString() == "button1")
  11.    {
  12.         .....
  13.         .....
  14.    }
  15.    else if(button->property("name").toString() == "button2")
  16.    {
  17.          ....
  18.          ....
  19.    }
  20. }

February 9, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

February 10, 2011

Franzk Franzk
Lab Rat
830 posts

Safe or not safe, it’s arguably not even simpler than the QSignalMapper approach. Likewise, if a slot is going to make (significant) behavioral changes based on the sender(), then it is probably time to review the design anyway.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

February 10, 2011

Luca Luca
Ant Farmer
589 posts

Andre wrote:
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.


Yes, I usually check if pointer is a push button with a dynamic_cast:
  1. QPushButton *button = dynamic_cast<QPushButton*>(sender());
  2.     if(button==NULL)
  3.     {
  4.         return;
  5.     }

Mine is only a fast example…

February 10, 2011

Franzk Franzk
Lab Rat
830 posts

qobject_cast might be more thorough.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

February 10, 2011

MarekR22 MarekR22
Lab Rat
14 posts

Hi there is a tool which do exactly what you need.
See QSignalMapper [doc.trolltech.com]
There is good example how to use it.

February 10, 2011

Volker Volker
Robot Herder
5428 posts

If you’re in the same class (or have access to the possible sender objects pointers) you simply can compare pointers:

  1. void MyClass::buttonClicked()
  2. {
  3.     if(sender() == button1) {
  4.         // ....
  5.     } else if(sender() == button2) {
  6.         // ....
  7.     }
  8. }

 
  ‹‹ QProcess as another user?      [Solved] QComboBox duplicates ››

You must log in to post a reply. Not a member yet? Register here!