qt第二天: MainWindow
|
@ -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
|
||||||
|
|
|
@ -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
|
|
@ -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();
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
#include "widget.h"
|
||||||
|
|
||||||
|
Widget::Widget(QWidget *parent)
|
||||||
|
: QWidget(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget::~Widget()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 7.0 KiB |
After Width: | Height: | Size: 52 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 9.5 KiB |
After Width: | Height: | Size: 5.3 KiB |
After Width: | Height: | Size: 7.7 KiB |
After Width: | Height: | Size: 6.3 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 8.9 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 1.4 MiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 8.7 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 101 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 8.5 KiB |
|
@ -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();
|
||||||
|
}
|
|
@ -0,0 +1,111 @@
|
||||||
|
#include "mainwindow.h"
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
MainWindow::MainWindow(QWidget *parent)
|
||||||
|
: QMainWindow(parent)
|
||||||
|
{
|
||||||
|
// 设置窗口的标题
|
||||||
|
setWindowTitle("正在学习QMainWindow");
|
||||||
|
resize(1200,960);
|
||||||
|
|
||||||
|
// 创建菜单栏(QMenuBar,QMenu,QAction)
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
|
@ -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
|
||||||
|
|
|
@ -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>
|
|
@ -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();
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
|
@ -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
|