QT QTableView添加右鍵菜單選項


首先需要設計這個菜單的樣式,需要在頭文件中添加

  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());//菜單出現位置為鼠標右鍵點擊的位置
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM