在QT中制作动态QMenu

时间:2015-11-04 11:59:19

标签: qt qmenu

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

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

1 个答案:

答案 0 :(得分:0)

它看起来像myMenu.clear的工作原因是因为我在添加menuaction后没有删除listofPossibleValues。我能够通过添加

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


//At the end
listofPossibleValues.clear();