【Qt开发】Qt gui添加右键菜单事件
2013-08-09 10:29
531 查看
要在某一种窗体内添加右键菜单栏,比如在QTreeWidget中添加,
可以用到slot函数customContextMenuRequested(QPointpos)。
如果是在Qt Creator中,则操作过程为:右击QTreeWidget-->go to slot--
-->选择customContextMenuRequested(QPointpos)。
然后,在新建的customContextMenuRequested(QPointpos)函数中便可以实现具体的菜单栏了。
为了判断右键点击的位置,即点即不同的item节点,会出现不同的右键菜单,我们可以用API中itemAt()函数实现。
而为了给每个节点赋予不同的键值,可以通过setData()来实现。
我在TreeWidget中添加右键菜单的实现代码:
[cpp] view
plaincopy
void MainWindow::on_treeWidget_customContextMenuRequested(QPoint pos)
{
QTreeWidgetItem* curItem=ui->treeWidget->itemAt(pos); //获取当前被点击的节点
if(curItem==NULL)return; //这种情况是右键的位置不在treeItem的范围内,即在空白位置右击
if(0==curItem->data(0,Qt::UserRole)) //data(...)返回的data已经在之前建立节点时用setdata()设置好
{
QMenu *popMenu =new QMenu(this);//定义一个右键弹出菜单
popMenu->addAction(ui->action_newDB);//往菜单内添加QAction 该action在前面用设计器定义了
popMenu->addAction(ui->action_openDB);
popMenu->addAction(ui->action_delDB);
popMenu->exec(QCursor::pos());//弹出右键菜单,菜单位置为光标位置
}
else
{
QMenu *popMenu =new QMenu(this);//定义一个右键弹出菜单
popMenu->addAction(ui->action_newTable);//往菜单内添加QAction 该action在前面用设计器定义了
popMenu->addAction(ui->action_openTable);
popMenu->addAction(ui->action_designTable);
popMenu->exec(QCursor::pos());//弹出右键菜单,菜单位置为光标位置
}
}
建立TreeWidget根节点的代码实现:
[cpp] view
plaincopy
QTreeWidgetItem *root;
root = new QTreeWidgetItem(ui->treeWidget, QStringList(QString("Connection")));
QVariant var0(0);
root->setData(0,Qt::UserRole,var0);
分享到:
可以用到slot函数customContextMenuRequested(QPointpos)。
如果是在Qt Creator中,则操作过程为:右击QTreeWidget-->go to slot--
-->选择customContextMenuRequested(QPointpos)。
然后,在新建的customContextMenuRequested(QPointpos)函数中便可以实现具体的菜单栏了。
为了判断右键点击的位置,即点即不同的item节点,会出现不同的右键菜单,我们可以用API中itemAt()函数实现。
而为了给每个节点赋予不同的键值,可以通过setData()来实现。
我在TreeWidget中添加右键菜单的实现代码:
[cpp] view
plaincopy
void MainWindow::on_treeWidget_customContextMenuRequested(QPoint pos)
{
QTreeWidgetItem* curItem=ui->treeWidget->itemAt(pos); //获取当前被点击的节点
if(curItem==NULL)return; //这种情况是右键的位置不在treeItem的范围内,即在空白位置右击
if(0==curItem->data(0,Qt::UserRole)) //data(...)返回的data已经在之前建立节点时用setdata()设置好
{
QMenu *popMenu =new QMenu(this);//定义一个右键弹出菜单
popMenu->addAction(ui->action_newDB);//往菜单内添加QAction 该action在前面用设计器定义了
popMenu->addAction(ui->action_openDB);
popMenu->addAction(ui->action_delDB);
popMenu->exec(QCursor::pos());//弹出右键菜单,菜单位置为光标位置
}
else
{
QMenu *popMenu =new QMenu(this);//定义一个右键弹出菜单
popMenu->addAction(ui->action_newTable);//往菜单内添加QAction 该action在前面用设计器定义了
popMenu->addAction(ui->action_openTable);
popMenu->addAction(ui->action_designTable);
popMenu->exec(QCursor::pos());//弹出右键菜单,菜单位置为光标位置
}
}
建立TreeWidget根节点的代码实现:
[cpp] view
plaincopy
QTreeWidgetItem *root;
root = new QTreeWidgetItem(ui->treeWidget, QStringList(QString("Connection")));
QVariant var0(0);
root->setData(0,Qt::UserRole,var0);
分享到:
相关文章推荐
- 【Qt开发】Qt gui添加右键菜单事件
- 二次开发visio添加右键菜单响应事件错误
- Qt gui添加右键菜单事件
- 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单、书签等相关问题
- 在QT中添加右键菜单 .
- 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单等相关问题
- 【ArcEngine 10 二次开发】ITOCControl添加鼠标右键菜单
- MFC中动态创建N级菜单以及响应事件——续——动态添加菜单/菜单项、子菜单、右键菜单
- 在QT中添加右键菜单
- Qt添加右键菜单
- 【转载于<小锋刚>博客园】C#遍历ContextMenuStrip右键菜单中包含子菜单的所有菜单并添加事件
- Qt 中Treewidget添加右键菜单
- [Qt] 在QTableWidget中添加右键菜单 [2013-06-17更新]
- easyui datagrid中添加右键菜单事件
- Qt添加右键菜单
- 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单等相关问题
- Extjs 4.2 panel 添加 click 事件及右键菜单
- easyui datagrid中添加右键菜单事件
- 给JList中的Item添加右键点击弹出菜单事件
- Qt添加右键菜单