在QT中制作一个动态的QMenu

Making a dynamic QMenu in QT

我有一个 QMenu,我想在其中添加 QActions。问题是,每次单击 table 时,现有菜单都会自动添加。我如何阻止它这样做?
所以当我点击table时,每次都应该是新菜单。

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 不起作用的原因是因为我在添加菜单操作后没有删除 listofPossibleValues。我能够通过添加

来修复它
 //At the beginning 
 myMenu.clear(); 


//At the end
listofPossibleValues.clear();