第一章 : Qt 安装
- 下载地址
- 安装
- 打开 cmd 运行镜像 :
qt-unified-windows-x64-4.6.0-online.exe --mirror https://mirrors.aliyun.com/qt
Hello
- 因为是qmake 所以是
.pro
Ctrl + R
直接运行
第二章 GUI程序设计基础
main文件
-
*.ui
: 有UI设计器自动生成,存储了组件和布局类的名称是 -
ui_widget.h
: 根据组件、信ui widget.h:号与信号槽自动生成
2.2 可视化 UI设计
信号槽
-
sender : 谁发的
-
SIGNAL : 发的什么信号
-
receiver : 谁接收
-
SLOT : 处理函数
-
Qt
项目 构建过程基本原理
-
QT中信号和槽的简单解释
-
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class QCheckBox;
class QPushButton;
class QPlainTextEdit;
class QRadioButton;
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private slots: // 槽 : 基本都是void 函数
void do_chkBoxUnder(bool checked);
void do_chkBoxItalic(bool checked);
void do_chkBoxBold(bool checked);
void do_setFontColor();
private:
QCheckBox *chkBoxUnder;
QCheckBox *chkBoxItalic;
QCheckBox *chkBoxBold;
QRadioButton *radioBlack;
QRadioButton *radioRed;
QRadioButton *radioBlue;
QPlainTextEdit *txtEdit;
QPushButton *btnOK;
QPushButton *btnCancel;
QPushButton *btnClose;
};
#endif // DIALOG_H
- dialog.cpp
#include "dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDialog>
#include <QCheckBox>
#include <QRadioButton>
#include <QPlainTextEdit>
#include <QPushButton>
void Dialog::do_chkBoxUnder(bool checked) {
QFont font = txtEdit->font();
font.setUnderline(checked);
txtEdit->setFont(font);
}
void Dialog::do_chkBoxItalic(bool checked)
{
QFont font = txtEdit->font();
font.setItalic(checked);
txtEdit->setFont(font);
}
void Dialog::do_chkBoxBold(bool checked)
{
QFont font = txtEdit->font();
font.setBold(checked);
txtEdit->setFont(font);
}
void Dialog::do_setFontColor() {
QPalette plet = txtEdit->palette();
if(radioBlack->isChecked()) plet.setColor(QPalette::Text, Qt::black);
if(radioRed->isChecked()) plet.setColor(QPalette::Text, Qt::red);
if(radioBlue->isChecked()) plet.setColor(QPalette::Text, Qt::blue);
txtEdit->setPalette(plet);
}
Dialog::Dialog(QWidget *parent ) : QDialog(parent) {
chkBoxUnder = new QCheckBox("下划线");
chkBoxItalic = new QCheckBox("斜体");
chkBoxBold = new QCheckBox("加粗");
QHBoxLayout *Hlayl = new QHBoxLayout();
Hlayl->addWidget(chkBoxUnder);
Hlayl->addWidget(chkBoxBold);
Hlayl->addWidget(chkBoxItalic);
radioBlack = new QRadioButton("黑色");
radioRed= new QRadioButton("红色");
radioBlue= new QRadioButton("蓝色");
QHBoxLayout * Hlay2 = new QHBoxLayout();
Hlay2->addWidget(radioBlack);
Hlay2->addWidget(radioRed);
Hlay2->addWidget(radioBlue);
txtEdit = new QPlainTextEdit;
txtEdit->setPlainText("HelloWorld\n 手工创建!");
QFont font = txtEdit->font();
font.setPointSize(20);
txtEdit->setFont(font);
btnOK = new QPushButton("确定");
btnCancel = new QPushButton("取消");
btnClose = new QPushButton("关闭");
QHBoxLayout *Hlay3 = new QHBoxLayout();
Hlay3->addWidget(btnOK);
Hlay3->addStretch();
Hlay3->addWidget(btnCancel);
Hlay3->addStretch();
Hlay3->addWidget(btnClose);
QVBoxLayout *Vlay = new QVBoxLayout();
// add 后会对内存进行回收
Vlay->addLayout(Hlayl);
Vlay->addLayout(Hlay2);
// Vlay->addLayout(font);
Vlay->addWidget(txtEdit);
Vlay->addLayout(Hlay3);
setLayout(Vlay);
// 设置槽
connect(chkBoxUnder,SIGNAL(clicked(bool)), this, SLOT(do_chkBoxUnder(bool)));
connect(chkBoxItalic,SIGNAL(clicked(bool)), this, SLOT(do_chkBoxItalic(bool)));
connect(chkBoxBold,SIGNAL(clicked(bool)), this, SLOT(do_chkBoxBold(bool)));
connect(radioBlack, SIGNAL(clicked(bool)), this, SLOT(do_setFontColor()));
connect(radioRed, SIGNAL(clicked(bool)), this, SLOT(do_setFontColor()));
connect(radioBlue, SIGNAL(clicked(bool)), this, SLOT(do_setFontColor()));
connect(btnOK, SIGNAL(clicked()), this, SLOT(accept()));
connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
connect(btnClose, SIGNAL(clicked()), this, SLOT(close()));
setWindowTitle("people UI !");
}
Dialog::~Dialog()
{
// delete ui;
}
使用CMakeGUI 创建 VS工程
- 参考博客
Qt Creator 使用技巧