Qt QtreeWidget树形控件右键菜单

QTreeWidget *treeView;//QTreeWidget对象


connect(treeView, SIGNAL(customContextMenuRequested(const QPoint)), this, SLOT(showMouseRightButton(const QPoint)));
void showMouseRightButton(const QPoint)为响应右击树形节点的响应槽函数
void showMouseRightButton(const QPoint &point)
{
QMenu *qMenu = new QMenu(treeView);
QModelIndex indexSelect = treeView->indexAt(point);//获得当前右击事件的节点
QModelIndex indexParent = indexSelect.parent();//获得当前右击事件节点的父节点
int row = indexSelect.row();//获得当前右击事件的树形节点所在的行
QModelIndex indexData = indexParent.child(row,1);//获得当前右击事件节点父节点的第row行的第二列的节点
QString QStr;
QStr = indexSelect.data().toString();//获得当前右击事件的节点数据
if(QStr != "")//如果当前右击事件在节点范围内,即树形节点上则显示右键的菜单
{
QAction* showAction = new QAction("&菜单显示",this);
qMenu->addAction(showAction);
connect(showAction, SIGNAL(triggered()), this, SLOT(showCurve()));//showCurve为点击菜单右键“菜单显示”的槽函数
qMenu->exec(QCursor::pos());
}
}


void showCurve()
{
QMessageBox msg;
msg.setText("右键菜单");
msg.exec();
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值