在QT中制作动态QMenu
我有一个QMenu,我想在其中添加QActions。问题是,每次单击表时,现有菜单都会添加到自身中。我怎样才能阻止它那样做? 所以当我点击桌子时,每次都应该是新菜单在QT中制作动态QMenu,qt,qmenu,Qt,Qmenu,我有一个QMenu,我想在其中添加QActions。问题是,每次单击表时,现有菜单都会添加到自身中。我怎样才能阻止它那样做? 所以当我点击桌子时,每次都应该是新菜单 void MainWindow::onTableClicked(const QModelIndex &index){ QAction *action; // action->setAutoRepeat(false); if (index.isValid()) { QSt
void MainWindow::onTableClicked(const QModelIndex &index){
QAction *action;
// action->setAutoRepeat(false);
if (index.isValid()) {
QString cellText = index.data().toString();
QString indexRow = QString::number(index.row());
QString indexCol = QString::number(index.column());
ui->textBrowser->setText(indexRow + "\n"+ indexCol);
QSet<int> possiblevalues = findPossibleValues(index.row(),index.column());
for(int n : possiblevalues){
listofPossibleValues.push_back(QString::number(n));
}
for(auto s :listofPossibleValues){
action = myMenu.addAction("Set Value "+s);
}
}
}
它看起来像myMenu.clear不起作用的原因是,在添加menuaction后,我没有删除可能值的列表。我可以通过添加
//At the beginning
myMenu.clear();
//At the end
listofPossibleValues.clear();
,这就是你需要的吗?@thuga我真的不希望整个菜单都被清除。只是行动而已。但“清除”仍然不起作用。我感到困惑。我以为你每次点击桌子都想要一套新的动作。你能解释一下到底是什么问题吗?@thuga是的,我想要一套新的动作,但最好包括其他永久性动作。正在重复的操作是代码action=mymenu.addaction的最后一行。。。我希望每次单击表时都是新的,清除不会删除我的所有操作,所以可能是您的listofPossibleValues列表包含重复的值?