使用 ‪ 创建上下文菜单Qt Designer(Qt Creator IDE)

发布于 2024-08-30 14:30:01 字数 58 浏览 6 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

王权女流氓 2024-09-06 14:30:01

您需要在 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 value ActionsContextMenu.

  • 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);

甜`诱少女 2024-09-06 14:30:01

我可以建议一种方法,允许您手动编写几行通用代码,然后仅使用 Qt Creator 为表单上的任意数量的组件添加上下文菜单。例如,表单上有三个组件:QLabel lbl1、QPushButton btn1 和 QTextEdit ed1。我们需要为每个菜单添加它自己的上下文菜单。为此,请执行以下操作:

  • myContextMenuHandler(QPoint) 槽添加到表单 (QMainWindow)。
  • 在表单的 cpp 文件中,为此槽编写以下代码:
void MainWindow::myContextMenuHandler(QPoint pt)
{
    QMenu *mnu = ui->menuPopupMenus->findChild<QMenu *>("menu" + sender()->objectName());
    if (mnu)
        mnu->popup(dynamic_cast<QWidget *>(sender())->mapToGlobal(pt));
}
  • 将一个顶级菜单项(标题=“PopupMenus”和名称=“menuPopupMenus”(名称由 Qt Creator 自动生成)添加到菜单栏)为此
  • 菜单项创建三个子项:
    • title="lbl1"(与我们的 QLabel 相同),name=menulbl1(自动生成)
    • title="btn1"(与我们的 QPushButton 相同),name=menubtn1(自动生成)
    • title="ed1"(与我们的 QTextEdit 相同),name=menued1(自动生成)

这些项目中的每一个都必须有一组自己的子项目,这些子项目将显示为相应组件的上下文菜单(例如“lbl1”项目将具有“Item1”、“Item2”和“ Item3”子项;“btn1”-“Item4”和“Item5”;“ed1”-“Item6”)。

  • 将 lbl1、btn1 和 ed1 组件的 customContextMenuRequested(QPoint) 信号连接到表单的 myContextMenuHandler(QPoint) 插槽。
  • 将 lbl1、btn1 和 ed1 组件的 contextMenuPolicy 属性设置为“CustomContextMenu”
  • 将以下代码行添加到表单类构造函数中:
ui->menuPopupMenus->menuAction()->setVisible(false);

上述所有操作(除了我们编写代码的两个操作)都可以在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:

  • Add the myContextMenuHandler(QPoint) slot to the form (QMainWindow).
  • In the cpp-file of the form, write the following code for this slot:
void MainWindow::myContextMenuHandler(QPoint pt)
{
    QMenu *mnu = ui->menuPopupMenus->findChild<QMenu *>("menu" + sender()->objectName());
    if (mnu)
        mnu->popup(dynamic_cast<QWidget *>(sender())->mapToGlobal(pt));
}
  • Add a top-level menu item (with title="PopupMenus" and name="menuPopupMenus" (name is autogenerated by Qt Creator) to the Menu Bar of form.
  • Create three subitems for this menu item:
    • title="lbl1" (same as our QLabel), name=menulbl1 (autogenerated)
    • title="btn1" (same as our QPushButton), name=menubtn1 (autogenerated)
    • title="ed1" (same as our QTextEdit), name=menued1 (autogenerated)

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").

  • connect customContextMenuRequested(QPoint) signal of lbl1, btn1 and ed1 components to myContextMenuHandler(QPoint) slot of form.
  • set contextMenuPolicy property of lbl1, btn1 and ed1 components to "CustomContextMenu"
  • add the following line of code to the form class constructor:
ui->menuPopupMenus->menuAction()->setVisible(false);

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.

霞映澄塘 2024-09-06 14:30:01

你唯一能做的就是设置 contextMenuPolicy 但我怀疑这就是你正在寻找的。

Only thing you can do is set contextMenuPolicy but I doubt that it is what you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文