Qt右击菜单栏实现
2016-02-17 09:18
405 查看
1.以treeview控件为例,首先,设置treeview的右击属性,
ui->treeView_2->setContextMenuPolicy(Qt::CustomContextMenu);
2.创建treeview右击菜单槽函数,如:
void MainWindow::on_treeView_2_customContextMenuRequested(const QPoint &pos)
{
QAction* common = nullptr;
common = new QAction(QIcon(":/TreeIcon/TreeIcon/refresh.png"), tr("刷新列表"), this);
QAction* cc_1 = nullptr;
cc_1 = new QAction(QIcon(":/TreeIcon/TreeIcon/changelib.png"), tr("修改设备"), this);
QAction* cc_2 = nullptr;
cc_2 = new QAction(QIcon(":/TreeIcon/TreeIcon/property.png"), tr("设备属性"), this);
QMenu *menu = new QMenu(this);
menu->addAction(common);
menu->addAction(cc_1);
menu->addAction(cc_2);
connect(common, &QAction::triggered, this, &MainWindow::UpdateDevList); //每个Action信号相应一个槽函数
connect(cc_1, &QAction::triggered, this, &MainWindow::changedev);
connect(cc_2, &QAction::triggered, this, &MainWindow::showdevsta);
menu->setStyleSheet("QMenu{background-color:rgb(255,255,255);color:rgb(35, 103, 174);font:10pt ""Microsoft YaHei"";}"
"QMenu::item:selected{background-color:#CCDAE7;}"
"QMenu::item:disabled{color:#BFBFBF;}");
QModelIndex index = ui->treeView_2->indexAt(pos);
if(index.data().toString().isEmpty())
{
cc_1->setDisabled(true);
cc_2->setDisabled(true);
menu->exec(QCursor::pos());
}else if(!index.parent().isValid())
{
cc_1->setDisabled(true);
cc_2->setDisabled(true);
menu->exec(QCursor::pos());
}else if(index.parent().isValid())
{
/*QModelIndex cur_p = index.parent();
int selectrow_p = cur_p.row();
int selectrow_c = index.row();
QStandardIt a318 emModel* model = static_cast<QStandardItemModel*>(ui->treeView->model());
QStandardItem* item = model->item(selectrow_p,0)->child(selectrow_c,0);
libm_storied = item->accessibleText().toStdString();
QList<facelist*>::iterator it2;
for(it2 = facewin.begin();it2 != facewin.end();++it2)
{
if(QString::fromStdString(libm_storied).toInt() == (*it2)->storeid)
{
libm_confide = QString::number((*it2)->confide).toStdString();
break;
}
}*/
menu->exec(QCursor::pos());
}
}
相关文章推荐
- QT设置窗口屏幕居中
- PyQt4入门教程(2)_PyQt4的第一个程序
- Qt QTextEdit根据行号移动光标
- 使用Qt设计师实现对话框
- Qt4 使用QJson库
- PyQt4入门教程(1)_PyQt介绍及准备工作
- Qt之设置窗口背景
- PyQt4入门教程(0)_译者说
- Qt 中定义单例对象
- Qt应用程序打包
- Qt环境搭建
- Qt颜色选择编辑demo
- 使用QT实现右击菜单实例
- QT 登陆界面设计
- QT自动下载网页每天更新的文件
- 用Qt实现一个桌面弹幕程序(三)--实现一个弹幕②
- qtdeclarative 源码略读
- PyQt5教程(十)——自定义控件
- Qt多线程事件管理的正确打开方式
- 为QTreeView添加右键菜单