QT菜单如何创建新窗口?

发布于 2024-11-25 11:52:42 字数 269 浏览 2 评论 0原文

我在 QT Creator 工作。

我想说我是这个软件的初学者。我成功创建了一个菜单,其中包含:编辑和打开。我想在单击菜单栏中的“打开”时创建一个新窗口。到目前为止,当我按“打开”时,我成功地在 QMessageBox 中打印了一条消息。用新窗口链接“打开”的代码是什么?是否可以以某种方式在 mainwindow.ui 中拖放一个新窗口,然后将其与 Open_activated() 操作上的“打开”链接?

谢谢 。欣赏

P.SI AM 在 UBUNTU/C++ 语言中工作

I am working in QT Creator.

I would like to mention that I am beginner in this software. I succeeded to create a menu that has: Edit and Open. I would like to create a new window when " Open " from menu bar is clicked. Till now I succeeded to print a message in QMessageBox when I press Open. What is the code for linking Open clicked with a new window? Is it possible to somehow drag and drop a new window in mainwindow.ui and than link it with Open on action Open_activated()?

THX . Appreciate

P.S I AM WORKING IN UBUNTU/C++ language

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

老街孤人 2024-12-02 11:52:42

这可能已经太晚了,但由于我也是一个初学者,并且为了弄清楚这一点付出了很多努力,我想分享一个对我有用的解决方案,根据我的说法,这是对于初学者来说最简单的解决方案之一。

请点击此链接。将来这个链接可能不存在,因此我正在写下一个详细的版本,尽可能详细,因为我认为对于绝对的初学者(像我自己)来说它可能非常有用。

  1. 默认 mainwindow.cpp

mainwindow.cpp

  1. 转到“mainwindow.ui”(或任何拥有的 .ui)

.ui page

  1. 在窗口外部右键单击并激活“动作编辑器”如果尚未激活:

Action Editor

  1. 右键单击​​窗口并创建“菜单栏”,然后双击“在此处键入” ”您可以将其重命名为“打开”或其他名称,然后会下拉另一个框,您可以在其中再次键入您想要的名称,比如说“消息”。

menu-栏

type-here

menu-item message

  1. 创建菜单项“message”时,将出现相应的项目在“动作编辑器”中

action editor 中的消息

  1. 右键单击​​ Action Editor 中的“message”,然后单击“go to slot”,这将引导您进入 mainwindow.cpp 中的特定槽:

转到插槽

在此处输入图像描述

  1. 在这里您可以创建一个小窗口,其中包含您想要显示的任何消息。

[![创建代码的窗口]11 等在这里。]12

创建迷你窗口并显示一些只读文本的代码。

*** *不忘记包含“QTextEditor”,请转到 mainwindow.h 和 #include "

在此处输入图像描述

在编译和执行时我们将看到:

点击打开->消息->打开新窗口
消息窗口.

This is probably too late to answer but as I am also a beginner and struggled a lot to figure out this, I would like to share a solution that worked for me and according to me this is one of the simplest solution out there for a beginner.

Follow this link. It's possible that in the future that link might not exists, hence I am writing down a detailed version, as detailed as possible because I think for an absolute beginner (like myself) it can be very useful.

  1. Default mainwindow.cpp

mainwindow.cpp

  1. Go to "mainwindow.ui" (or whatever .ui one has)

.ui page

  1. Right-click outside the window and activate "Action Editor" if it wasn't already activated:

Action Editor

  1. Right-click on the window and create "Menu-bar" and then double-clicking on "type here" you can rename it as "Open" or something which will then drop-down another box where you can again type the name you desire, let's say "message".

menu-bar

type-here

menu-item message

  1. When created menu item "message" is created corresponding item will apear in the "Action Editor"

message in the action editor

  1. Right-click on the "message" in the Action Editor and click "go to slot" which will lead you to the specific slot in the mainwindow.cpp:

go to slot

enter image description here

  1. Here you can create a small window with whatever message you want to display.

[![Window creating code]11 etc goes here.]12

Codes that create a mini window and display some read-only text.

****don't forget to include "QTextEditor", go to mainwindow.h and #include "

enter image description here

On compiling & executing we will see :

click open->message->to open new window
message window.

挽清梦 2024-12-02 11:52:42

上一个问题。它留下了一部分开放,即该插槽的调用方式。

好吧,您的回答是:菜单项“打开”是一个 QAction。您可以将该操作连接到该插槽您刚刚创建的。

Most of the question is already answered by this previous question. It leaves one part open, namely how that slot is called.

Well, you answered that: the menu entry "Open" is a QAction. You can connect that action to the slot which you just created.

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