qt tableview how to add a right-click menu and can not edit the cells

QTableView is a more practical classes teach you how to add the following to the right-click menu in QTableView in.

#include <The QMenu>
#include <The QAction>

QTableView, * tableView;
The QMenu * rightMenu; // context menu
QAction * cutAction; // shear
QAction * copyAction; // copy
QAction * pasteAction; // paste
QAction * deleteAction; // delete

Private slots:
void clicked_rightMenu (const QPoint & POS); // right signal slot function

 

tableView-> setEditTriggers (QAbstractItemView :: NoEditTriggers); // set the cell is not editable

tableview-> setContextMenuPolicy (Qt :: CustomContextMenu); // little sentence, there is no right response.

createRightMenu (); // create a context menu
connect (tableview, SIGNAL (customContextMenuRequested ( QPoint)), this, SLOT (clicked_rightMenu (QPoint)));

Which createRightMenu function:

rightMenu = new QMenu;
cutAction = new QAction("剪切",this);
copyAction = new QAction("复制",this);
pasteAction = new QAction("粘贴",this);
deleteAction = new QAction("删除",this);

rightMenu->addAction(cutAction);
rightMenu->addAction(copyAction);
rightMenu->addAction(pasteAction);
rightMenu->addAction(deleteAction);
clicked_rightMenu槽函数:


rightMenu->exec(QCursor::pos());

Guess you like

Origin www.cnblogs.com/tsh292278/p/11131557.html