문제

I have a QMenuwhich has several menu items built dynamically.

To this end, I iterate over a collection of menu items, containing the name and Action ( which would need to be taken if the menu item were hit), and keep adding them to a the context menu. All the menu items need to be connected to a common slot.

But somehow the trigger action does not happen. i.e. the connect statement is reached, but the control does not pass into the specified SLOT, no action is taken.

for (int i=0; i<Action_List.size();i++)
{
    tempAct1 = Action_List.at(i); //Action List has the list of Actions
    Context_Menu->addAction(tempAct1);
}
if (Context_Menu!=NULL) {
    Context_Menu->exec(QCursor::pos());
    int r = connect(Context_Menu, SIGNAL(triggered(QAction *)), 
                    this, SLOT(SPlusCommand(QAction *)));
}

int P14MainWindow::SPlusCommand ( QAction* Action)
{
    QVariant tempstr = Action->data();
    QString Qs = tempstr.toString();
    return QPwLocalClient::ExecuteCommand(Qs);
}

Can anyone tell me where I'm going wrong with this, please?

도움이 되었습니까?

해결책

It seems like you should move connect before exec():

connect(Context_Menu, SIGNAL(triggered(QAction *)), 
        this, SLOT(SPlusCommand(QAction *)));
Context_Menu->exec(QCursor::pos());

Because exec executes menu synchronously, what means that it will return from this method only when all your interaction with menu is finished — too late to connect something after it.

다른 팁

You will have to connect the individual actions with your slot.

connect(action, SIGNAL(triggered()), this, SLOT(yourSlot())
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top