首先需要设计这个菜单的样式,需要在头文件中添加
QMenu *tableviewMenue; QAction *appendAction; QAction *deleteAction; QAction *prependAction; QAction *translationAction;
然后在源文件中进行初始化,设置好他们的选项功能
tableviewMenue = new QMenu(ui->tableView); appendAction = new QAction(); deleteAction = new QAction(); prependAction = new QAction(); translationAction = new QAction(); appendAction->setText(tr("添加脉冲")); deleteAction->setText(tr("删除脉冲")); prependAction->setText(tr("首行前添加")); translationAction->setText(tr("脉冲平移")); tableviewMenue->addAction(prependAction); tableviewMenue->addAction(appendAction); tableviewMenue->addAction(deleteAction); tableviewMenue->addAction(translationAction); connect(appendAction,SIGNAL(triggered()),this,SLOT(appendAction_triggered())); connect(prependAction,SIGNAL(triggered()),this,SLOT(prependAction_triggered())); connect(deleteAction,SIGNAL(triggered()),this,SLOT(deleteAction_triggered())); connect(translationAction,SIGNAL(triggered()),this,SLOT(translationAction_triggered()));
最后通过QTableView的槽函数进行调用
void Widget::on_tableView_customContextMenuRequested(QPoint pos) { QModelIndex index = ui->tableView->indexAt(pos); if(index.isValid()) { appendAction->setEnabled(true); deleteAction->setEnabled(true); prependAction->setEnabled(true); translationAction->setEnabled(true); } else { appendAction->setEnabled(false); deleteAction->setEnabled(false); prependAction->setEnabled(true); translationAction->setEnabled(false); } tableviewMenue->exec(QCursor::pos());//菜单出现位置为鼠标右键点击的位置 }
每个按钮实现什么功能的槽函数可以自己去定义
补充 选中多行数据之后弹出菜单
在头文件中添加
QMenu *tableviewMenue2; QAction *deleteSelectionAction; QAction *translationSelectionAction;
在源文件中添加
tableviewMenue2 = new QMenu(ui->tableView); deleteSelectionAction = new QAction(); translationSelectionAction = new QAction(); deleteSelectionAction->setText(tr("删除选中行")); translationSelectionAction->setText(tr("移动选中行")); tableviewMenue2->addAction(deleteSelectionAction); tableviewMenue2->addAction(translationSelectionAction); connect(deleteSelectionAction,SIGNAL(triggered()),this,SLOT(deleteSelectionAction_triggered())); connect(translationSelectionAction,SIGNAL(triggered()),this,SLOT(translationSelectionAction_triggered()));
同样,这两部分都只是在执行初始化的功能
将前面的菜单槽函数改写为
void Widget::on_tableView_customContextMenuRequested(QPoint pos) { QModelIndex index = ui->tableView->indexAt(pos); if(index.isValid())//模型中有数据时,即模型索引可以获取到,根据选中的函数是否大于一判断出现菜单1还是菜单2 { QItemSelectionModel *selections = ui->tableView->selectionModel();//获取被选中的指针列表 QModelIndexList selected = selections->selectedIndexes(); QMap <int, int> rowMap; foreach (QModelIndex index, selected) { rowMap.insert(index.row(), 0); //QModelIndex 有更多数据可用 } if(rowMap.count() == 1)//当只选中了一行时弹出菜单1 { appendAction->setEnabled(true); deleteAction->setEnabled(true); prependAction->setEnabled(true); translationAction->setEnabled(true); tableviewMenue1->exec(QCursor::pos()); } else//选中多行时,弹出菜单2 { num1 = rowMap.firstKey();//要操作的数据起始行 num2 = rowMap.count();//要操作的数据的行数 tableviewMenue2->exec(QCursor::pos()); } } else//模型中没有数据时,即模型索引获取不到,出现菜单一 { appendAction->setEnabled(false); deleteAction->setEnabled(false); prependAction->setEnabled(true); translationAction->setEnabled(false); tableviewMenue1->exec(QCursor::pos());//菜单出现位置为鼠标右键点击的位置 } }