How to create virtual SLOT for action



  • Is it possible to send fonction for SLOT?
    @//get actions
    void get_actions(QWidget *widget, QStringList list, ??? slot){
    foreach (QString t, list){
    QAction *a = new QAction("&"+t, widget);
    a->setObjectName("act_"+t);
    widget->addAction(a);
    connect(a, SIGNAL(triggered()), ???, SLOT(slot));
    }
    }
    void Project::do_something_1(){...}
    void Project::do_something_2(){...}
    ...
    get_actions(ui->menu_actions_1, QStrinList()<<"name_1"<<"name_2", do_something_1());
    ...
    get_actions(ui->menu_actions_2, QStrinList()<<"name_3"<<"name_4", do_something_2());@

    Thanks



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



  • As we look into the qobject.h file for the "connect" declaration, it becomes clear what to do.
    @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:
    @//get actions
    void get_actions(QWidget *widget, QStringList list, const char *slot){
    foreach (QString t, list){
    QAction *a = new QAction("&"+t, widget);
    a->setObjectName("act_"+t);
    widget->addAction(a);
    connect(a, SIGNAL(triggered()), slot);
    }
    }
    void Project::do_something_1(){...}
    void Project::do_something_2(){...}
    ...
    get_actions( ui->menu_actions_1, QStrinList()<<"name_1"<<"name_2", SLOT(do_something_1()) );
    ...
    get_actions( ui->menu_actions_2, QStrinList()<<"name_3"<<"name_4", SLOT(do_something_2()) );@



  • Thanks RaubTieR.
    Works great.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.