mainwindow_showcase 教学示例:补 Designer 双版本表单(mainwindow2)+ 表单维护

- 新增 mainwindow2.{h,cpp,ui}:用 Qt Designer 生成的第二套 QMainWindow 表单
  (File/Edit/Help 菜单 + actionOpen/actionAbout_Me),供课堂对比「代码搭界面」
  与「Designer 拖界面」两条路线。
- 新增子目录 CMakeLists.txt:供单独打开该示例构建(顶层 add_subdirectory 不递归,
  与 lineedit_eye_toggle / mandelbrot_renderer 同模式)。
- mainwindow.ui / mainwindow.qrc:经 Designer 重新保存后的表单与资源清单维护
  (iconset 去掉 resource= 显式引用、属性顺序由 Designer 重排)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
张宗平
2026-07-09 16:51:33 +08:00
parent 01b264fe97
commit d3e2e50f0e
6 changed files with 170 additions and 21 deletions
@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.5)
project(qt_mainwindow_showcase LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.
#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()
find_package(Qt5 COMPONENTS Widgets REQUIRED)
if(ANDROID)
add_library(qt_mainwindow_showcase
main.cpp
mainwindow_showcase.cpp
mainwindow_showcase.h
mainwindow2.cpp
mainwindow2.h
mainwindow2.ui
mainwindow.ui
)
else()
add_executable(qt_mainwindow_showcase
main.cpp
mainwindow_showcase.cpp
mainwindow_showcase.h
mainwindow2.cpp
mainwindow2.h
mainwindow2.ui
mainwindow.ui
mainwindow.qrc)
endif()
target_link_libraries(qt_mainwindow_showcase PRIVATE Qt5::Widgets)
@@ -6,4 +6,8 @@
<file>icons/about.png</file>
<file>icons/dock.png</file>
</qresource>
<qresource prefix="/new/prefix1">
<file>icons/about.png</file>
</qresource>
<qresource prefix="/new/prefix2"/>
</RCC>
@@ -14,10 +14,9 @@
<string>QMainWindow 教学综合示例</string>
</property>
<property name="windowIcon">
<iconset resource="mainwindow.qrc">
<iconset>
<normaloff>:/icons/app.png</normaloff>:/icons/app.png</iconset>
</property>
<!-- 5.5 中心部件:QTextEdit(编辑器主体) -->
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="centralLayout">
<property name="spacing">
@@ -46,14 +45,13 @@
</item>
</layout>
</widget>
<!-- 5.1 菜单栏:文件 / 视图 / 帮助 -->
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>720</width>
<height>22</height>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@@ -95,9 +93,7 @@
<addaction name="menuView"/>
<addaction name="menuHelp"/>
</widget>
<!-- 5.3 状态栏(.ui 占位,永久标签在 .cpp 用 addPermanentWidget 添加) -->
<widget class="QStatusBar" name="statusbar"/>
<!-- 5.2 工具栏:复用同一批 QAction(QAction 是菜单项与工具按钮的公共抽象) -->
<widget class="QToolBar" name="mainToolBar">
<property name="windowTitle">
<string>主工具栏</string>
@@ -113,7 +109,6 @@
<addaction name="actionToggleDockFloating"/>
<addaction name="actionAbout"/>
</widget>
<!-- 5.4 铆接部件(浮动窗口):可拖出浮动、停靠到边缘 -->
<widget class="QDockWidget" name="dockWidget">
<property name="windowTitle">
<string>浮动窗口</string>
@@ -138,36 +133,35 @@
</layout>
</widget>
</widget>
<!-- 5.1 QAction:菜单项与工具按钮的公共抽象。图标取自 5.6 资源文件 -->
<action name="actionNew">
<property name="icon">
<iconset resource="mainwindow.qrc">
<iconset>
<normaloff>:/icons/new.png</normaloff>:/icons/new.png</iconset>
</property>
<property name="text">
<string>新建</string>
</property>
<property name="shortcut">
<string>Ctrl+N</string>
</property>
<property name="statusTip">
<string>清空编辑区(5.5 中心部件)</string>
</property>
<property name="shortcut">
<string>Ctrl+N</string>
</property>
</action>
<action name="actionQuit">
<property name="icon">
<iconset resource="mainwindow.qrc">
<iconset>
<normaloff>:/icons/quit.png</normaloff>:/icons/quit.png</iconset>
</property>
<property name="text">
<string>退出</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
<property name="statusTip">
<string>退出程序</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
<action name="actionToggleToolBar">
<property name="checkable">
@@ -213,7 +207,7 @@
</action>
<action name="actionToggleDockFloating">
<property name="icon">
<iconset resource="mainwindow.qrc">
<iconset>
<normaloff>:/icons/dock.png</normaloff>:/icons/dock.png</iconset>
</property>
<property name="text">
@@ -281,7 +275,7 @@
</action>
<action name="actionAbout">
<property name="icon">
<iconset resource="mainwindow.qrc">
<iconset>
<normaloff>:/icons/about.png</normaloff>:/icons/about.png</iconset>
</property>
<property name="text">
@@ -292,8 +286,6 @@
</property>
</action>
</widget>
<resources>
<include location="mainwindow.qrc"/>
</resources>
<resources/>
<connections/>
</ui>
@@ -0,0 +1,14 @@
#include "mainwindow2.h"
#include "ui_mainwindow2.h"
MainWindow2::MainWindow2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow2)
{
ui->setupUi(this);
}
MainWindow2::~MainWindow2()
{
delete ui;
}
@@ -0,0 +1,22 @@
#ifndef MAINWINDOW2_H
#define MAINWINDOW2_H
#include <QMainWindow>
namespace Ui {
class MainWindow2;
}
class MainWindow2 : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow2(QWidget *parent = nullptr);
~MainWindow2();
private:
Ui::MainWindow2 *ui;
};
#endif // MAINWINDOW2_H
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow2</class>
<widget class="QMainWindow" name="MainWindow2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>&amp;File</string>
</property>
<addaction name="actionOpen"/>
</widget>
<widget class="QMenu" name="menuEdit">
<property name="title">
<string>Edit</string>
</property>
<addaction name="actionOpen"/>
</widget>
<widget class="QMenu" name="menuAbout">
<property name="title">
<string>Help</string>
</property>
<addaction name="actionOpen"/>
<addaction name="actionAbout_Me"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
<addaction name="menuAbout"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen">
<property name="text">
<string>&amp;Open</string>
</property>
<property name="shortcut">
<string>Alt+O</string>
</property>
</action>
<action name="actionAbout_Me">
<property name="text">
<string>About Me</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>