Qt QtreeWidget树形控件右键菜单

来源:互联网 发布:如何开网络主播公司 编辑:程序博客网 时间:2024/05/16 04:59
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();
}
原创粉丝点击