当前位置:网站首页>Qt在QTableWidget、View等表格中添加右击菜单
Qt在QTableWidget、View等表格中添加右击菜单
2022-07-30 05:39:00 【林夕07】
先来看效果图,鼠标点在哪里菜单显示在哪里。
实现代码
在构造函数中设置右击菜单。并关联右击的槽函数。
ui.tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);//设置右击菜单
connect(ui.tableWidget, &QTableWidget::customContextMenuRequested, this, &QtTableWidgetMenu::onShowMenu);
再根据右击的pos位置,判断点击的QTableWidget位置中是否有Item。如下图空白这种情况,需要过滤掉。
void QtTableWidgetMenu::onShowMenu(QPoint pos)
{
QTableWidgetItem* selectedItem = ui.tableWidget->itemAt(pos); //获取右击的item
if (nullptr == selectedItem)
{
return;
}
QMenu* menu = new QMenu(this);//创建菜单
QAction* action = new QAction(u8"查看详细资料", this);//创建一个action
menu->addAction(action);//将action放入菜单中
menu->popup(ui.tableWidget->viewport()->mapToGlobal(pos));//将菜单显示到鼠标所在位置
}
边栏推荐
猜你喜欢

Navicat connection MySQL error: 1045 - Access denied for user 'root'@'localhost' (using password YES)

665.非递减数列

Socket通信编程

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

【线性神经网络】线性回归 / 基础优化方法

flask的笔记

个人博客系统(附源码)

Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案

mysql time field is set to current time by default

cnpm installation steps
随机推荐
Different usage scenarios of subqueries as retrieval tables and the question of whether to add aliases
Record Breaker (Google Kickstart2020 Round D Problem A)
破纪录者(Google Kickstart2020 Round D Problem A)
爬虫数据是如何收集和整理的?
Graphic mirror symmetry (schematic diagram)
Numpy 中 np.vstack() 和 np.hstack() 简单解析
MySQL (2)
[Mysql] DATEDIFF函数
Detailed MySQL-Explain
Error: listen EADDRINUSE: address already in use 127.0.0.1:3000
It is enough for MySQL to have this article (37k words, just like Bojun!!!)
Memories · The last system design in the university era
[Other] DS5
871.最低加油次数(动态规划)
[Mysql] CONVERT函数
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
Different lower_case_table_names settings for server ('1') and data dictionary ('0') solution
Solve phpstudy unable to start MySQL service
MySQL笔记(pymysql)
【飞控开发基础教程9】疯壳·开源编队无人机-PWM(电机控制)