构造函数中添加
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this,SIGNAL(customContextMenuRequested(const QPoint &)),this, SLOT(slotCustomContextMenu(const QPoint &)));
槽函数
void MainWindow::slotCustomContextMenu(const QPoint &point)
{
if(ui->treeView->hasFocus())
{
QMenu *menu;
QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\
QMenu::item{padding:3px 20px 3px 20px;}\
QMenu::indicator{width:13px;height:13px;}\
QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\
QMenu::separator{height:1px;background:#757575;}";
menu = new QMenu(this);
menu->setStyleSheet(qss);
QAction *a1 = new QAction(tr("打开"));
menu->addAction(a1);
connect(a1, SIGNAL(triggered()), this, SLOT(openFile()));
QAction *a2 = new QAction(tr("移动"));
menu->addAction(a2);
QAction *a3 = new QAction(tr("复制"));
menu->addAction(a3);
QAction *a4 = new QAction(tr("删除"));
menu->addAction(a4);
QAction *a5 = new QAction(tr("分享"));
menu->addAction(a5);
QAction *a6 = new QAction(tr("刷新列表"));
menu->addAction(a6);
connect(a6, SIGNAL(triggered()), this, SLOT(updataFileList()));
menu->exec(this->mapToGlobal(point));
}
}