qt第二天: MainWindow

This commit is contained in:
flykhan 2023-08-08 14:39:49 +08:00
parent 790228aba6
commit f6d37a40a2
39 changed files with 377 additions and 0 deletions

73
DialogTest/.gitignore vendored Normal file
View File

@ -0,0 +1,73 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

28
DialogTest/DialogTest.pro Normal file
View File

@ -0,0 +1,28 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

11
DialogTest/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

11
DialogTest/widget.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
}
Widget::~Widget()
{
}

14
DialogTest/widget.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H

BIN
qtdemo02/images/Edit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
qtdemo02/images/Frame.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
qtdemo02/images/Left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

BIN
qtdemo02/images/Luffy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

BIN
qtdemo02/images/LuffyQ.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
qtdemo02/images/New.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

BIN
qtdemo02/images/Pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
qtdemo02/images/Right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
qtdemo02/images/Start.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

BIN
qtdemo02/images/Sunny.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
qtdemo02/images/add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
qtdemo02/images/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
qtdemo02/images/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
qtdemo02/images/mario.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
qtdemo02/images/mesage.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
qtdemo02/images/open.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
qtdemo02/images/reload.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
qtdemo02/images/save.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
qtdemo02/images/stop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
qtdemo02/images/sunny.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

BIN
qtdemo02/images/time.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
qtdemo02/images/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

11
qtdemo02/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

111
qtdemo02/mainwindow.cpp Normal file
View File

@ -0,0 +1,111 @@
#include "mainwindow.h"
#include <cstdio>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 设置窗口的标题
setWindowTitle("正在学习QMainWindow");
resize(1200,960);
// 创建菜单栏QMenuBarQMenuQAction
QMenuBar *menu_bar = menuBar();
this->setMenuBar(menu_bar); // 设置当前窗口的菜单栏
// 创建图片空间
QPixmap pix_map;
pix_map.load(":icon/start");
// 创建菜单
QMenu *menu1 = new QMenu("文件",this);
// 创建菜单项
QAction *openAction = menu1->addAction("打开...");
QMenu *sub_menu = new QMenu("最近使用",this); // 子菜单
sub_menu->addAction("d:\\a.txt");
// 添加子菜单(菜单项也可以是一个菜单,即为子菜单)
menu1->addMenu(sub_menu);
QAction *newAction = menu1->addAction("新建...");
menu1->addSeparator();
newAction->setIcon(QIcon(pix_map));
QAction *exitAction = menu1->addAction("退出");
menu_bar->addMenu(menu1);
exitAction->setIcon(QIcon(":icon/stop"));
QMenu *menu2 = new QMenu("编辑",this);
QAction *editAction = menu2->addAction("复制");
menu2->addAction("剪切");
menu2->addAction("粘贴");
menu_bar->addMenu(menu2);
// 菜单项的快捷方式
openAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_O));
newAction->setShortcut(QKeySequence(tr("ctrl+n")));
exitAction->setShortcut(QKeySequence(tr("ctrl+q")));
// 初始化子控件
mEdit = new QTextEdit(this);
mEdit->hide();
mWidget1 = new Widget1;
connect(openAction,&QAction::triggered,[=](){
takeCentralWidget(); // 将当前中心空间从主窗口移除
qDebug()<<"正在打开文件...";
setCentralWidget(mEdit);
mEdit->show();
});
connect(newAction,&QAction::triggered,[=](){
takeCentralWidget();
qDebug()<<"正在新建文件...";
setCentralWidget(mWidget1);
});
connect(exitAction,&QAction::triggered,this,&QMainWindow::close);
// 工具栏
QToolBar *tool_bar = new QToolBar;
tool_bar->setParent(this);
tool_bar->addAction(openAction);
tool_bar->addAction(newAction);
tool_bar->addSeparator();
tool_bar->addAction(exitAction);
tool_bar->setMovable(false); // 禁止拖动
tool_bar->setFloatable(false); // 禁止浮动
tool_bar->setAllowedAreas(Qt::LeftToolBarArea|Qt::RightToolBarArea);
this->addToolBar(tool_bar);
// this->addToolBar(Qt::LeftToolBarArea,tool_bar);
// 创建状态栏
QStatusBar *status_bar = new QStatusBar(this);
setStatusBar(status_bar);
QLabel *label1 = new QLabel("状态: 1",this);
status_bar->addWidget(label1); // 从左向右添加
QLabel *label2 = new QLabel("状态: 2",this);
// label2->setFixedSize(200,40);
// label2->setStyleSheet("background-color: #cf0f13; padding-left: 100px;");
status_bar-> addPermanentWidget(label2); // 从右向左添加
status_bar->addWidget(new QLabel("状态 3: 0",this),1); // 1 是平铺剩余空间
// 创建铆接控件
QDockWidget *dock = new QDockWidget("铆接1",this);
dock->addAction(openAction);
dock->addAction(exitAction);
dock->setWidget(new Widget1(this));
QDockWidget *dock2 = new QDockWidget("铆接2",this);
dock2->setWidget(new QPushButton("点我试试",this));
addDockWidget(Qt::TopDockWidgetArea,dock);
addDockWidget(Qt::TopDockWidgetArea,dock2);
// 中心部件
}
MainWindow::~MainWindow()
{
}

28
qtdemo02/mainwindow.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar> // 菜单栏
#include <QMenu> // 菜单
#include <QAction> // 菜单项
#include <QDebug>
#include <QToolBar> // 工具栏
#include <QStatusBar> // 状态栏
#include <QLabel> // 标签
#include <QDockWidget> // 铆接控件
#include <QPushButton>
#include "widget1.h"
#include <QTextEdit>
#include <QPixmap> // 图片
class MainWindow : public QMainWindow
{
Q_OBJECT
QTextEdit *mEdit;
Widget1 *mWidget1;
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
};
#endif // MAINWINDOW_H

34
qtdemo02/qtdemo02.pro Normal file
View File

@ -0,0 +1,34 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
widget1.cpp
HEADERS += \
mainwindow.h \
widget1.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
res.qrc

8
qtdemo02/res.qrc Normal file
View File

@ -0,0 +1,8 @@
<RCC>
<qresource prefix="/icon">
<file alias="open">images/open.png</file>
<file alias="save">images/save.png</file>
<file alias="start">images/Start.png</file>
<file alias="stop">images/stop.png</file>
</qresource>
</RCC>

26
qtdemo02/widget1.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "widget1.h"
Widget1::Widget1(QWidget *parent) : QWidget(parent)
{
btn = new QPushButton("Ok",this);
close_btn = new QPushButton("退出",this);
label = new QLabel("结果: ",this);
btn->resize(100,50);
btn->move(10,10);
close_btn->resize(100,50);
close_btn->move(120,10);
label->resize(200,50);
label->move(10,70);
connect(btn,&QPushButton::pressed,[&](){
label->setText("结果: 1+1 = 2");
});
connect(close_btn,&QPushButton::pressed,[&](){
this->close();
});
}

22
qtdemo02/widget1.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef WIDGET1_H
#define WIDGET1_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
class Widget1 : public QWidget
{
Q_OBJECT
QPushButton *btn; // 私有
QLabel *label;
QPushButton *close_btn;
public:
explicit Widget1(QWidget *parent = nullptr);
signals:
};
#endif // WIDGET1_H