gpt4 book ai didi

c++ - 使用 QMenu 并传递参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:52:01 24 4
gpt4 key购买 nike

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

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

但是不知何故触发 Action 并没有发生。即到达连接语句,但控制未传递到指定的 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);
}

谁能告诉我哪里出了问题,好吗?

最佳答案

看起来你应该移动 connect before exec():

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

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

关于c++ - 使用 QMenu 并传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14708490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com