December 22, 2010

Dieter Dieter
Lab Rat
94 posts

[Solved] What is the signature of a function used as SLOT

 

I was wondering what signature (type of the parameter theSlot) must be used for function that will be used as a slot:
I know that connect actually gets the slot as a simple const char* but the following example does not work and the compiler reports theSlot as an unreferenced parameter:

  1. void addMenuItem(QMenu* theMenu, const QString& theTitle, const char* theSlot)
  2. {
  3.  QAction* aAction = new QAction(theTitle, this);
  4.  connect(aAction, SIGNAL(triggered()), this, SLOT(theSlot));
  5.  theMenu->addAction(aAction);
  6. }
  7.  
  8. void buildMenu(QMenu* theMenu)
  9. {
  10.  addMenuItem(theMenu, "Open file...", "slot_open_file()");
  11. }

4 replies

December 22, 2010

VCsala VCsala
Lab Rat
339 posts

You have missunderstood the thing a bit. If you use SLOT (which is a kind of macro understood by the moc compiler) it should be a method name and its parameter types without quotes. However connect’s 4th parameter has type const char*, so in your sample the SLOT is not required.

December 22, 2010

VCsala VCsala
Lab Rat
339 posts

You can read more about it here [doc.qt.nokia.com]

December 22, 2010

Dieter Dieter
Lab Rat
94 posts

I played around a little and actually the const char* parameter type and the use of SLOT was OK but I used slot on the wrong argument.

The following example does work as expected:

  1. void addMenuItem(QMenu* theMenu, const QString& theTitle, const char* theSlot)
  2. {
  3.  QAction* aAction = new QAction(theTitle, this);
  4.  connect(aAction, SIGNAL(triggered()), this, theSlot);
  5.  theMenu->addAction(aAction);
  6. }
  7.  
  8. void buildMenu(QMenu* theMenu)
  9. {
  10.  addMenuItem(theMenu, "Open file...", SLOT(slot_open_file()));
  11. }

December 23, 2010

Gerolf Gerolf
Robot Herder
3287 posts

That is the correct usage. Then you don’t care how SLOT is done :-)

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

 
  ‹‹ Explain: bool _showArgumentNames: 1;      HowTo resize Subclasses of QGraphicsItem ››

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