首页 > 代码库 > qt QTreeWidget右键菜单函数,以备自己翻阅
qt QTreeWidget右键菜单函数,以备自己翻阅
QModelIndex index=ui.tree_FileLocal->indexAt(pos);
QMenu *pMenu=new QMenu(ui.tree_FileLocal);
if (ui.tree_FileLocal->columnCount()==7)
{
QAction *refresh=pMenu->addAction(tr("刷新(&Y)"));
refresh->setShortcut(Qt::Key_F5);
connect(refresh,SIGNAL(triggered()),this,SLOT(discRefreshLocalFunction()));
pMenu->addSeparator();
QAction *attribute=pMenu->addAction(tr("属性(&Z)"));
connect(attribute,SIGNAL(triggered()),this,SLOT(discInfoLocalFunction()));
}
else
{
if (index==QModelIndex())//空白地方
{
QMenu *checkMenu=new QMenu(ui.tree_FileLocal);
QAction *plain=checkMenu->addAction(tr("平铺(&W)"));
plain->setCheckable(true);
connect(plain,SIGNAL(triggered()),this,SLOT(extendLocalView()));
checkMenu->addSeparator();//分隔线
QAction *icon=checkMenu->addAction(tr("图标(&X)"));
icon->setCheckable(true);
connect(icon,SIGNAL(triggered()),this,SLOT(iconLocalView()));
checkMenu->addSeparator();
QAction *list=checkMenu->addAction(tr("列表(&Y)"));
list->setCheckable(true);
connect(list,SIGNAL(triggered()),this,SLOT(listLocalView()));
checkMenu->addSeparator();
QAction *report=checkMenu->addAction(tr("报表(&Z)"));
report->setCheckable(true);
connect(report,SIGNAL(triggered()),this,SLOT(reportLocalView()));
switch(m_viewLocalFlag)
{
case 0:plain->setChecked(true);break;//勾选
case 1:icon->setChecked(true);break;
case 2:list->setChecked(true);break;
case 3:report->setChecked(true);break;
}
QMenu *newMenu=new QMenu(ui.tree_FileLocal);
QAction *contents=newMenu->addAction(tr("目录(&Z)"));
contents->setCheckable(true);
connect(contents,SIGNAL(triggered()),this,SLOT(createDirLocalFunction()));
QAction *refresh=pMenu->addAction(tr("刷新(&X)"));
refresh->setShortcut(Qt::Key_F5);//添加热键
connect(refresh,SIGNAL(triggered()),this,SLOT(fileRefreshLocalFunction()));
pMenu->addSeparator();
QAction *checks=pMenu->addAction(tr("查看(&Y)"));
checks->setMenu(checkMenu);
pMenu->addSeparator();
if(m_copyFlag)
{
QAction *paste=pMenu->addAction(tr("粘贴(&V)"));
paste->setShortcut(Qt::CTRL+Qt::Key_V);
connect(paste,SIGNAL(triggered()),this,SLOT(pasteFileLocalFunction()));
pMenu->addSeparator();
}
QAction *news=pMenu->addAction(tr("新建(&Z)"));
news->setMenu(newMenu);
}
else//文件
{
QAction *open=pMenu->addAction(tr("打开(&U)"));
open->setShortcut(Qt::Key_F9);
connect(open,SIGNAL(triggered()),this,SLOT(openFileLocalFunction()));
QAction *refresh=pMenu->addAction(tr("刷新(&X)"));
refresh->setShortcut(Qt::Key_F5);
connect(refresh,SIGNAL(triggered()),this,SLOT(fileRefreshLocalFunction()));
pMenu->addSeparator();
QAction *copy=pMenu->addAction(tr("复制(&V)"));
copy->setShortcut(Qt::CTRL+Qt::Key_C);
connect(copy,SIGNAL(triggered()),this,SLOT(copyFileLocalFuntion()));
pMenu->addSeparator();
QAction *alter=pMenu->addAction(tr("改名(&W)"));
alter->setShortcut(Qt::Key_F2);
connect(alter,SIGNAL(triggered()),this,SLOT(renameFileLocalFunction()));
QAction *del=pMenu->addAction(tr("删除(&Y)"));
del->setShortcut(Qt::Key_Delete);
connect(del,SIGNAL(triggered()),this,SLOT(deleteFileLocalFunction()));
pMenu->addSeparator();
QAction *attribute=pMenu->addAction(tr("属性(&Z)"));
connect(attribute,SIGNAL(triggered()),this,SLOT(fileInfoLocalFunction()));
}
}
pMenu->exec(QCursor::pos());//在鼠标位置弹出菜单
本文出自 “xibi199011” 博客,请务必保留此出处http://xibi199011.blog.51cto.com/6859097/1537481