3

我有一个QMenu动态构建的几个菜单项。

为此,我遍历了一组菜单项,其中包含名称和操作(如果菜单项被点击,则需要执行),并继续将它们添加到上下文菜单中。所有菜单项都需要连接到一个公共插槽

但不知何故,触发动作没有发生。即到达连接语句,但控制没有传递到指定的SLOT,不采取任何行动。

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);
}

谁能告诉我我哪里出了问题,拜托?

4

2 回答 2

5

看来您应该connect exec()搬家:

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

因为同步exec执行菜单,这意味着只有当您与菜单的所有交互都完成时,它才会从这个方法返回——在它之后连接东西已经太晚了。

于 2013-02-05T14:05:04.277 回答
-2

您必须将各个操作与您的插槽连接起来。

connect(action, SIGNAL(triggered()), this, SLOT(yourSlot())
于 2013-02-05T13:50:29.653 回答