1. QT思维导图
2. QT文件简析
2.1 xxx.pro文件
QT += core gui //含义为添加本工程所使用的模块 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets //QT版本号,4版本 CONFIG += c++11 //C++版本号
2.2 xxx.h文件
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT //宏:用来支持信号与槽 public: MainWindow(QWidget *parent = nullptr); //有默认参数的有参构造,parent为父对象指针 ~MainWindow(); }; #endif // MAINWINDOW_H
2.3 xxx.cpp文件
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { } MainWindow::~MainWindow() { }
2.4 main.cpp文件
#include "mainwindow.h" #include <QApplication> //core模块的核心类 int main(int argc, char *argv[]) { QApplication a(argc, argv); //QT中只能有一个QApplication a对象; MainWindow w; w.show(); return a.exec(); //进入事件循环监测 }
2.5 界面属性
1)move(x,x):以屏幕左上角为坐标原点进行移动;
2)resize(x,y):重置界面长宽;
3)setFixedSize(x,y):设置界面固定尺寸;
4)QPalette:启用画板,格式:
QPalette p = this->palette();
p.setBrush(QPalette::window,图片路径);
this->setPalette(p);
4)setWindowIcon(QIcon("路径")); 设置窗口小图标;
5)setWindowTitle("xxxx"); 设置窗口名字;
6)signal:自定义信号
例:signal:
void mysignal(); // 创建一个mysignal的信号
3. QT各种常用模块
3.1 QPushButton
设置按钮操作。
在xxx.h文件中定义按钮指针:
QPushButton *loginbtn;
在xxx.cpp文件中对按钮进行操作:
1)开辟栈区空间,并设置参数:loginbtn = new QPushButton("登录",this); //this为父对象,为了回收空间;也可以分步进行: loginbtn = new QPushButton(this);
loginbtn->setText("登录");
2)设置按钮位置:loginbtn->move(x,y);基于界面左上角为坐标轴原点移动x,y;
3.2 connect:信号与槽
connect(const QObject *sender,const char *signal,const QObject *receiver,const char *method);
用法:connect(信号发出者,信号,接收者,接收者的槽函数);
要求:信号与槽参数要保持一致;
例子:public slots: //slots是为了成功转换const char*,不然会报错;
connect(this->loginBtn,&QPushButton::clicked,this,[=](){});
【】里可以填写=或者&,表示拷贝赋值的方式获取外部信号,{}里可以直接编写函数体,也可以用QWidget自带的槽函数:&QWudget::close等。
可以使用自定义的信号与槽函数实现控件之间交互:设定一个自定义信号,一个页面发送,另一个接收,并设定相应的槽函数。
3.3 布局管理器
手册中通过搜索addWatchedService. QHBoxLayout(水平管理), QVBoxLayout(垂直管理),QGridLayout(网格布局)
1)QLabel *usernamelabel; 定义标签
2) QLineEdit *usernameline; 定义可填方框
3)QGridLayout *uplayout; 定义网格布局
4) QHBoxLayout *bottomlayout; 定义水平布局
5) QVBoxLayout *mainlayout; 定义垂直布局
6)设定网格布局:this->uplayout->addWidget(usernamelabel,0,0); 后两个参数为相应的坐标;
7)水平布局与垂直布局根据定义的先后顺序,依次排列;
3.4 回显模式
通过EchoMode查询相应的模式,来设置输入的内容是否显示。
3.5 正则表达式
通常用来限定用户输入的内容,可以通过QRegExp来检索手册:
正则表达式 – 语法 | 菜鸟教程 (runoob.com)
4. 模态与非模态
可以在手册中,通过QMessageBox查找相应的指令。
4.1 QMessageBox 成员
1)StandardButton QMessageBox::information(QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
2)StandardButton QMessageBox::critical(QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)
3)StandardButton QMessageBox::question(QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = StandardButtons( Yes | No ),
StandardButton defaultButton = NoButton)
4)StandardButton QMessageBox::warning(QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)
各个参数的含义是:
- parent:指定消息对话框的父窗口,消息提示框会作为一个独立的窗口显示在父窗口的前面。消息提示框从弹出到关闭的整个过程中,用户无法操作父窗口,更不能删除父窗口;
- title:指定消息对话框的标题;
- text:指定消息对话框的具体内容;
- buttons:指定消息对话框中包含的按钮。默认情况下,消息对话框只包含一个按钮。根据需要,我们可以用
|
按位或运算符在消息对话框中设置多个按钮,例如QMessageBox::Ok|QMessageBox::Cancel
; - defaultButton:指定 Enter 回车键对应的按钮,用户按下回车键时就等同于按下此按钮。注意,defaultButton 参数的值必须是 buttons 中包含的按钮,当然也可以不手动指定,QMessageBox 会自动从 buttons 中选择合适的按钮作为 defaultButton 的值。
枚举值 | 含 义 |
---|---|
QMessageBox::Ok | 标有 "OK" 字样的按钮,通常用来表示用户接受或同意提示框中显示的信息。 |
QMessageBox::Open | 标有 "Open" 字样的按钮。 |
QMessageBox::Save | 标有 "Save" 字样的按钮。 |
QMessageBox::Cancel | 标有 "Cancel" 字样的按钮。点击此按钮,通常表示用户拒绝接受提示框中显示的信息。 |
QMessageBox::Close | 标有 "Close" 字样的按钮。 |
QMessageBox::Discard | 标有 "Discard" 或者 "Don't Save" 字样的按钮,取决于运行平台。 |
QMessageBox::Apply | 标有 "Apply" 字样的按钮。 |
QMessageBox::Reset | 标有 "Reset" 字样的按钮。 |
QMessageBox::Yes | 标有 "Yes" 字样的按钮。 |
QMessageBox::No | 标有 "No" 字样的按钮。 |
5. QMianWindow类
主界面应用程序开发类。
利用此类开发一个文本编辑器流程:
1)#include <QMenuBar> //菜单栏
#include <QMenu> //菜单 最上面那些选项
#include <QAction> //菜单选项 点击菜单之后跳出来那些
添加格式:菜单选项定义的名字 = 菜单名->addAction("菜单选项中文名");
#include <QToolBar> //工具栏 菜单下面那栏
#include <QToolButton> //工具按钮