April 14, 2012

betmens betmens
Lab Rat
18 posts

How to create virtual SLOT for action

 

Is it possible to send fonction for SLOT?

  1. //get actions
  2. void get_actions(QWidget *widget, QStringList list, ??? slot){
  3.  foreach (QString t, list){
  4.   QAction *a = new QAction("&"+t, widget);
  5.   a->setObjectName("act_"+t);
  6.   widget->addAction(a);
  7.   connect(a, SIGNAL(triggered()), ???, SLOT(slot));
  8.  }
  9. }
  10. void Project::do_something_1(){...}
  11. void Project::do_something_2(){...}
  12. ...
  13. get_actions(ui->menu_actions_1, QStrinList()<<"name_1"<<"name_2", do_something_1());
  14. ...
  15. get_actions(ui->menu_actions_2, QStrinList()<<"name_3"<<"name_4", do_something_2());

Thanks

3 replies

April 14, 2012

Rahul Das Rahul Das
Hobby Entomologist
436 posts

What exactly are you trying to do ? Slot can be treated as any other c++ functions. Looks like you might want to look at QSignalMapper

 Signature 

——————————-

    Rahul Das

——————————-

April 14, 2012

RaubTieR RaubTieR
Lab Rat
42 posts

As we look into the qobject.h file for the “connect” declaration, it becomes clear what to do.

  1. bool connect(const QObject *sender, const char *signal, const char *member // , ...

I’ve simplified the declaration since it was big. But here we see that both SIGNAL and SLOT are const char pointer. Hence your code of choice is:

  1. //get actions
  2. void get_actions(QWidget *widget, QStringList list, const char *slot){
  3.  foreach (QString t, list){
  4.   QAction *a = new QAction("&"+t, widget);
  5.   a->setObjectName("act_"+t);
  6.   widget->addAction(a);
  7.   connect(a, SIGNAL(triggered()), slot);
  8.  }
  9. }
  10. void Project::do_something_1(){...}
  11. void Project::do_something_2(){...}
  12. ...
  13. get_actions( ui->menu_actions_1, QStrinList()<<"name_1"<<"name_2", SLOT(do_something_1()) );
  14. ...
  15. get_actions( ui->menu_actions_2, QStrinList()<<"name_3"<<"name_4", SLOT(do_something_2()) );

April 14, 2012

betmens betmens
Lab Rat
18 posts

Thanks RaubTieR.
Works great.

 
  ‹‹ is the QStandarditemModel - finditems() fast?      how to place an widget over the desktop screen using qt ››

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