博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
qt 实现右键菜单
阅读量:4165 次
发布时间:2019-05-26

本文共 1389 字,大约阅读时间需要 4 分钟。

  右键菜单实现:通过重写contextMenuEvent(QContextMenuEvent *event)事件,QMenu+QAction即可完美实现!

实现方式:createActions用于创建菜单、菜单项,contextMenuEvent用于实现菜单的显示,translateLanguage用于实现菜单的文本(此方法主要设置多语化使用)

void ImageTree::createActions(){    //创建菜单、菜单项    pop_menu = new QMenu();    add_images_action = new QAction(this);     add_folder_action = new QAction(this);    remove_selected_action = new QAction(this);    remove_all_action = new QAction(this);    //连接信号与槽    connect(add_images_action, &QAction::triggered, this, &ImageTree::selectImages);    connect(add_folder_action, &QAction::triggered, this, &ImageTree::selectFolder);    connect(remove_selected_action, &QAction::triggered, this, &ImageTree::removeSelectedImages);    connect(remove_all_action, &QAction::triggered, this, &ImageTree::removeAllImages);}void ImageTree::contextMenuEvent(QContextMenuEvent *event){    //清除原有菜单    pop_menu->clear();    pop_menu->addAction(add_images_action);    pop_menu->addAction(add_folder_action);    pop_menu->addAction(remove_selected_action);    pop_menu->addAction(remove_all_action);    //菜单出现的位置为当前鼠标的位置    pop_menu->exec(QCursor::pos());    event->accept();}void ImageTree::translateLanguage(){    add_images_action->setText(tr("add images"));    add_folder_action->setText(tr("add folder"));    remove_selected_action->setText(tr("remove selected images"));    remove_all_action->setText(tr("remove all images"));}

转载地址:http://qvqxi.baihongyu.com/

你可能感兴趣的文章
(新手向)双向链表的c语言实现
查看>>
栈的应用:c语言求解n皇后问题(迭代版)
查看>>
(补档)vs汇编开发配置(MASM32+Irvine32)
查看>>
栈的应用——缓冲区计算逆波兰式
查看>>
二叉树:广义表搭建二叉树
查看>>
树的应用:求有根树所有子树大小
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第一章)(一)
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第一章)(二)
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第二章)(一)
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第二章)(二)
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第三章)(一)
查看>>
栈的应用:火车调度问题
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第三章)(二)
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第三章)(三)
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第四章)
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第五章)
查看>>
编译原理语法分析“向前看”辨析
查看>>
HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
查看>>
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。
查看>>
《计算机网络 自顶向下方法》(第7版)答案(第六章)(二)
查看>>