文章目录
vision:5.14
main文件
#include "mainwindow.h"
#include <QApplication>
//argc命令行变量数,argv命令行参数信息
int main(int argc, char *argv[])
{
QApplication a(argc, argv);//a应用程序对象,有且仅有一个
MainWindow w;//窗口对象
w.show();//显示窗口
return a.exec();//程序对象进入消息循环
}
mainWindow-QPushButton
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPushButton>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//create a button
QPushButton *btn = new QPushButton;
btn->setParent(this);
// btn->show();//show以顶层弹出
btn->setText("button1");
btn->resize(100,50);
QPushButton * btn2 = new QPushButton("btn2",this);
//move btn2
btn2->move(100,100);
//重置窗口大小
resize(600,400);
//固定窗口大小
setFixedSize(600,400);
//重置标题
setWindowTitle("first window");
}
MainWindow::~MainWindow()
{
delete ui;
}
对象树
功能:父类释放时子类一同释放
遵从类的继承关系
指定从QObject或其子类继承则不用管理释放
e.x. Obj->setParent(this);
信号和槽
//demand: click button, then close the window
//connect(信号发送者,信号(function address),signal receiver,槽Slots)
//signals:click/pressed/released/toggled(状态切换)
connect(btn,&QPushButton::clicked,this,&QWidget::close);
自定义信号和槽
demands:
//Teacher class
//Student class
//teacher触发信号:饿了,学生触发响应:请吃饭
create new class:头文件文件夹add new,选择class
//teacher.h
#ifndef TEACHER_H
#define TEACHER_H
#include <QObject>
class Teacher : public QObject
{
Q_OBJECT
public:
explicit Teacher(QObject *parent = nullptr);
signals:
//if return value is void,只需声明,不需实现
//可有参数可重载
void hungry();
};
#endif // TEACHER_H
//student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <QObject>
class Student : public QObject
{
Q_OBJECT
public:
explicit Student(QObject *parent = nullptr);
signals:
public slots:
//early version须写到public slots,higher version可写到public或全局
void treat();
};
#endif // STUDENT_H
//student.cpp
#include "student.h"
#include<Qdebug>//注意头文件!
Student::Student(QObject *parent) : QObject(parent)
{
}
void Student::treat()
{
qDebug()<<"treat teacher";
}
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "teacher.h"
#include "student.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
Teacher* t;
Student* st;
void classIsOver();
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "teacher.h"
#include "student.h"
#include<QPushButton>
//Teacher class
//Student class
//teacher触发信号:饿了,学生触发响应:请吃饭
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->t = new Teacher(this);
this->st = new Student(this);
connect(t,&Teacher::hungry,st,&Student::treat);
classIsOver();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::classIsOver()
{
//emit:key word
emit t->hungry();
}
自定义信号的重载
连接时需要指定接口
QString输出带引号,转cahr*方法:
QStringObj.toUtf8().data();
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->t = new Teacher(this);
this->st = new Student(this);
//指针->address
void(Teacher::*teacherSignal)(QString)=&Teacher::hungry;
void(Student::*studentSlot)(QString)=&Student::treat;
connect(t,teacherSignal,st,studentSlot);
classIsOver();
}
信号与信号连接
demand: click下课按钮再触发下课
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "teacher.h"
#include "student.h"
#include<QPushButton>
#include<Qdebug>
//Teacher class
//Student class
//teacher触发信号:饿了,学生触发响应:请吃饭
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->t = new Teacher(this);
this->st = new Student(this);
QPushButton* btn = new QPushButton("下课",this);
this->resize(600,400);
//click the btn,then class is over
connect(btn,&QPushButton::clicked,this,&MainWindow::classIsOver);
//无参信号和槽连接
void(Teacher::*teacherSignal2)()=&Teacher::hungry;
void(Student::*studdentSignal2)()=&Student::treat;
connect(t,teacherSignal2,st,studdentSignal2);
//signal connect signal
connect(btn,&QPushButton::clicked,t,teacherSignal2);
// //指针->address
// void(Teacher::*teacherSignal)(QString)=&Teacher::hungry;
// void(Student::*studentSlot)(QString)=&Student::treat;
// connect(t,teacherSignal,st,studentSlot);
// classIsOver();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::classIsOver()
{
//emit:key word
qDebug()<<"class is over"<<'\n';
emit t->hungry();
emit t->hungry("a");
}
output(click once):
class is over//来自click与classIsOver连接
treat teacher
treat teacher
lambda
形式:
[capture](parameters)mutable->return-type
{
statement
}
//[]:标志表达式开始,不能省略
// 参数:空
// = :函数体内所有可见局部变量,值传递
// &:引用传递
// this:函数体内可使用lambda所在类的成员变量,类似等号
// a:将a按值传递,不能修改a的拷贝,如需修改,可用mutable修饰符
// &a:将a按引用传递
// a,&b:a按值,b按引用
// =,&a,&b:除a,b,均按值
//e.x.
[=]()
{
btn->setText("a");
}();
QPushButton*myBtn = new QPushButton(this);
myBtn->move(100,100);
int m = 10;
connect(myBtn,&QPushButton::clicked,this,[m]()mutable{m+=10;qDebug()<<m;});
//修改拷贝而非本体
int ret = []()->int{return 1000;}();//注意末尾()表示函数调用
//use lambda to close the window
QPushButton*btn2 = new QPushButton("close",this);
btn2->move(100,0);
connect(btn2,&QPushButton::clicked,this,[=](){this->close();emit st->treat();});
QMainWindow
包含一个菜单栏(menu bar)、多个工具栏(tool bars)、多个锚接部件(dock widgets)、一个状态栏(status bar)及一个中心部件(central widget)
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMenuBar>
#include<QToolBar>
#include<QDebug>
#include<QPushButton>
#include<QStatusBar>
#include<QLabel>
#include<QDockWidget>
#include<QTextEdit>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//create menuBar,at most one
QMenuBar * bar = menuBar();
//set to window
setMenuBar(bar);
//create menu
QMenu* fileMenu = bar->addMenu("文件");
QMenu* editMenu = bar->addMenu("编辑");
QAction* newAction = fileMenu->addAction("新建");
//add分隔符
fileMenu->addSeparator();
QAction* openAction = fileMenu->addAction("打开");
//toolbar
QToolBar *toolBar = new QToolBar(this);
addToolBar(Qt::LeftToolBarArea,toolBar);//停靠区
//后期只允许左右停靠
toolBar->setAllowedAreas(Qt::LeftToolBarArea|Qt::RightToolBarArea);
//设置浮动
toolBar->setFloatable(false);
//设置移动
toolBar->setMovable(false);
//工具栏设置内容
toolBar->addAction(newAction);
toolBar->addSeparator();
toolBar->addAction(openAction);
//add控件
QPushButton *btn = new QPushButton("aa",this);
toolBar->addWidget(btn);
//状态栏 at most one
QStatusBar * stBar = statusBar();
setStatusBar(stBar);
//放标签
QLabel* label = new QLabel("提示信息",this);
stBar->addWidget(label);
//右侧提示信息
QLabel* label2 = new QLabel("右侧提示信息",this);
stBar->addPermanentWidget(label2);
//锚接部件(浮动窗口)可有多个
QDockWidget* dockWidget = new QDockWidget("浮动",this);
addDockWidget(Qt::BottomDockWidgetArea,dockWidget);
//设置后期停靠区
dockWidget->setAllowedAreas(Qt::TopDockWidgetArea|Qt::BottomDockWidgetArea);
//中心部件
//textedit 文本编辑
QTextEdit* edit = new QTextEdit(this);
setCentralWidget(edit);
}
MainWindow::~MainWindow()
{
delete ui;
}
ui-资源文件添加
进入设计界面,可以添加菜单栏已有模块,也可添加新的模块
e.x.
图一:系统自动添加控件actionnew,其名称只能为英文,如需显示中文,则需在text中修改
在主界面右键可添加工具栏并将下面栏内部件拖入主界面,也可在左侧栏寻找部件
在锚接部件的属性栏可以调整停靠区域
添加外部资源:
将资源加入到项目内:
1、将资源拖入项目文件夹中
2、右键项目Add New->Qt->Qt Resource File
3、在resource中open in editor开启编辑
4、添加前缀
5、add files
成功添加示例
//使用资源":+prefix+filename"
ui->actionnew->setIcon(QIcon(":/lenaeye/197.bmp"));
模态和非模态对话框创建
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//click 新建,弹出对话框
connect(ui->actionnew,&QAction::triggered,[=](){
//对话框分类:模态和非模态(可以对其他窗口操作)
//模态 阻塞功能
// QDialog dlg(this);
// dlg.resize(200,100);
// dlg.exec();//弹出
//非模态
QDialog* dlg2 = new QDialog(this);
dlg2->resize(200,100);
dlg2->setAttribute(Qt::WA_DeleteOnClose);//关闭时释放,防止内存泄漏
dlg2->show();
});
}
消息对话框
标准对话框:
e.x. QColorDialog…
消息提示:Question/Information/Warning/Critical
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//click 新建,弹出对话框
connect(ui->actionnew,&QAction::triggered,[=](){
//消息对话框
//错误对话框
// QMessageBox::critical(this,"critical","错误");//parent,title,text
//信息对话框
// QMessageBox::information(this,"info","信息");
//提问对话框
// if(QMessageBox::Save == QMessageBox::question(this,"ques","提问",QMessageBox::Save|QMessageBox::Cancel))
// {
// qDebug()<<"Save";
// }
// else
// {
// qDebug()<<"Cancel";
// }
// //standard button: yes/no,参数4:按键类型,第5个参数:关联回车
//警告
QMessageBox::warning(this,"warning","警告");
});
}
其他标准对话框
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDialog>
#include<QDebug>
#include<QMessageBox>
#include<QColorDialog>
#include<QFileDialog>
#include<QFontDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//click 新建,弹出对话框
connect(ui->actionnew,&QAction::triggered,[=](){
//消息对话框
//错误对话框
// QMessageBox::critical(this,"critical","错误");//parent,title,text
//信息对话框
// QMessageBox::information(this,"info","信息");
//提问对话框
// if(QMessageBox::Save == QMessageBox::question(this,"ques","提问",QMessageBox::Save|QMessageBox::Cancel))
// {
// qDebug()<<"Save";
// }
// else
// {
// qDebug()<<"Cancel";
// }
// //standard button: yes/no,参数4:按键类型,第5个参数:关联回车
//警告
// QMessageBox::warning(this,"warning","警告");
//颜色
// QColor color = QColorDialog::getColor(QColor(255,0,0));//参数1:初始默认颜色rgb
// qDebug()<<"r="<<color.red();
//文件
// QFileDialog::getOpenFileName(this,"打开文件","D:/Desktop","(*.txt)");
// //parent,title,position,filter,return QString(path)
//字体
bool flag;
QFont font = QFontDialog::getFont(&flag,QFont("华文彩云",36));//默认字体字号
//字体 font.family() 字号 font.pointSize() 加粗 font.bold 倾斜 font.italic()
});
}
MainWindow::~MainWindow()
{
delete ui;
}
登录窗口布局
右键可删除模块
设计:
用户名…:label
输入框…:lineedit
按钮:pushbutton
对齐:1.horizonal layout…
2.widget,点击上侧水平布局
布局结果
按钮形状改变:弹簧(spacer)
对spacer:sizeType可固定间距
右键布局可打破布局
用户名、密码可栅格布局
sizepolicy调整widget
windowtitle修改标题
对widget,修改layoutmargin可修改内部间隙
密码框隐藏输入:echoMode设置为password
控件-按钮组
tool button:
图形+文字显示:QToolButton->toolButtonStyle:ToolButtonTextBesideIcon
autoRaise:选中时按钮变透明效果
radio button:
单选钮