使用 创建上下文菜单Qt Designer(Qt Creator IDE)
如何在 Qt Designer (1.3) 中创建上下文菜单? 当然,我想创建它而不需要编写一行代码!
How can i create a Context menu in Qt Designer (1.3)?
Certainly I want to create it with out writing one line code!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在 Qt Designer 中执行两个步骤,并在表单构造函数中添加几行代码:
将小部件的
contextMenuPolicy
设置为值ActionsContextMenu
。使用操作编辑器选项卡创建操作。
对于您在 Qt Designer 中创建的每个操作,在表单构造函数中添加如下一行:
ui->yourwidget->addAction(ui->youraction);
You need two steps in Qt Designer and a few lines of code in the form constructor:
Set the
contextMenuPolicy
of your widget to the valueActionsContextMenu
.Create actions using the action editor tab.
For each action you created in Qt Designer, put a line such as the following in the form constructor:
ui->yourwidget->addAction(ui->youraction);
我可以建议一种方法,允许您手动编写几行通用代码,然后仅使用 Qt Creator 为表单上的任意数量的组件添加上下文菜单。例如,表单上有三个组件:QLabel lbl1、QPushButton btn1 和 QTextEdit ed1。我们需要为每个菜单添加它自己的上下文菜单。为此,请执行以下操作:
myContextMenuHandler(QPoint)
槽添加到表单 (QMainWindow)。这些项目中的每一个都必须有一组自己的子项目,这些子项目将显示为相应组件的上下文菜单(例如“lbl1”项目将具有“Item1”、“Item2”和“ Item3”子项;“btn1”-“Item4”和“Item5”;“ed1”-“Item6”)。
customContextMenuRequested(QPoint)
信号连接到表单的myContextMenuHandler(QPoint)
插槽。contextMenuPolicy
属性设置为“CustomContextMenu”上述所有操作(除了我们编写代码的两个操作)都可以在Qt Creator的设计模式下执行。为新组件添加新的上下文菜单不需要编写代码。此外,如有必要,不同的上下文菜单可以包含共享的 QAction。
I can suggest a method that allows you to write a few lines of general code manually and then add context menus for any number of components on the form using only Qt Creator. For example, we have three components on the form: QLabel lbl1, QPushButton btn1, and QTextEdit ed1. We need to add it's own context menu to each of them. To do this:
myContextMenuHandler(QPoint)
slot to the form (QMainWindow).Each of these items must have the set of its own subitems, which will be displayed as a context menu for the corresponding component (e.g. "lbl1" item will have "Item1", "Item2" and "Item3" subitems; "btn1" - "Item4" and "Item5"; "ed1" - "Item6").
customContextMenuRequested(QPoint)
signal of lbl1, btn1 and ed1 components tomyContextMenuHandler(QPoint)
slot of form.contextMenuPolicy
property of lbl1, btn1 and ed1 components to "CustomContextMenu"All of the above actions (except for the two where we wrote the code) can be executed in Design Mode of Qt Creator. Adding new context menus for new components does not require writing code. Also if necessary different context menus can contain shared QActions.
你唯一能做的就是设置 contextMenuPolicy 但我怀疑这就是你正在寻找的。
Only thing you can do is set contextMenuPolicy but I doubt that it is what you are looking for.