前言
因为学习嵌入式方面的东西,发现Qt设计桌面应用相对简单,本人以前也用过JAVA与Python开发过桌面应用,单都不是特别理想,所有现在就开始学习Qt开发,这个专栏只是记载我个人学习Qt的一些过去和使用的文件这些,后续将持续跟新,也希望对刚入门Qt小伙伴有所帮助吧!😁
Qt的下载
Qt的下载教程有很多,这里我也给大家放一个下载教程,这个教程有详细的下载流程以及下载应该选择那些东西,但是因为后面的教程需要给钱啦,所以我只是使用的他的下载教程,已经下载好了的小伙伴可以直接跳过这一步啦
Qt 版本
我个人使用的版本是Qt 5.9.9,就上面这个教程说的,Qt的5.9系列是持续更新的,可以放心使用
Qt下载教程http://c.biancheng.net/view/3851.html
Qt -- 建立第一个项目(使用代码建立)
下载好软件以后我们就正式进入项目的建立,Qt有两种方式进行界面的编程,因为我个人之前有开发Android studio 的习惯和经历,个人更习惯使用代码直接进行界面布局开发
1.打开软件建立项目
2. 给界面添加控件
打开我们的点dialog.h的文件,在里面添加定义的控件
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
//添加控件头文件
#include <QCheckBox>
#include <QRadioButton>
#include <QPushButton>
#include <QPlainTextEdit>
class Dialog : public QDialog
{
Q_OBJECT
private:
QCheckBox *checkBox1;
QCheckBox *checkBox2;
QCheckBox *chackBox3;
QRadioButton *rBtBlack;
QRadioButton *rBtBlue;
QRadioButton *rBtRed;
QPlainTextEdit *plainTextEdit;
QPushButton *btnOK;
QPushButton *btnCancle;
QPushButton *btnClose;
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
};
#endif // DIALOG_H
在 dialog.cpp 文件中添加文件的控件到布局中
#include "dialog.h"
//添加控件头文件
#include "QHBoxLayout"
#include "QVBoxLayout"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
//获取dialog.h定义的函数并且给他们命名
checkBox1 = new QCheckBox(tr("复选框1"));
checkBox2= new QCheckBox(tr("斜线"));
chackBox3= new QCheckBox(tr("加粗"));
rBtBlack = new QRadioButton(tr("Black"));
rBtBlue = new QRadioButton(tr("Blue"));
rBtRed = new QRadioButton(tr("Red"));
//设置字体(字体放大点)
plainTextEdit = new QPlainTextEdit();
QFont font = plainTextEdit->font();
font.setPointSize(20);
plainTextEdit->setFont(font);
plainTextEdit->setPlainText("Hello Word\n\n");
btnOK = new QPushButton(tr("Ok"));
btnCancle = new QPushButton(tr("Cancle"));
btnClose = new QPushButton(tr("Close"));
//布局获取的控件
QHBoxLayout *Hlayout1 = new QHBoxLayout;
QHBoxLayout *Hlayout2 = new QHBoxLayout;
QHBoxLayout *Hlayout3 = new QHBoxLayout;
//复选框放入布局文件中
Hlayout1->addWidget(checkBox1);
Hlayout1->addWidget(checkBox2);
Hlayout1->addWidget(chackBox3);
//单选框放入布局文件
Hlayout2->addWidget(rBtBlack);
Hlayout2->addWidget(rBtBlue);
Hlayout2->addWidget(rBtRed);
//按键放入布局文件
Hlayout3->addStretch();
Hlayout3->addWidget(btnOK);
Hlayout3->addWidget(btnCancle);
Hlayout3->addWidget(btnClose);
//把垂直布局嵌入水平布局
QVBoxLayout *Vlayout1 = new QVBoxLayout;
Vlayout1->addLayout(Hlayout1);
Vlayout1->addLayout(Hlayout2);
Vlayout1->addWidget(plainTextEdit);
Vlayout1->addLayout(Hlayout3);
setLayout(Vlayout1);
}
Dialog::~Dialog()
{
}
3.运行效果
点击运行程序
运行效果图
4.添加点击事件
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
//添加控件头文件
#include <QCheckBox>
#include <QRadioButton>
#include <QPushButton>
#include <QPlainTextEdit>
class Dialog : public QDialog
{
Q_OBJECT
private:
QCheckBox *checkBox1;
QCheckBox *checkBox2;
QCheckBox *chackBox3;
QRadioButton *rBtBlack;
QRadioButton *rBtBlue;
QRadioButton *rBtRed;
QPlainTextEdit *plainTextEdit;
QPushButton *btnOK;
QPushButton *btnCancle;
QPushButton *btnClose;
void iniUi();
void inSignalSlots();
private slots:
void onCheckunderline(bool checked);
void onCheckItalic(bool checked);
void onCheckBold(bool checked);
void setTextFontColor();
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
};
#endif // DIALOG_H
dialog.c
#include "dialog.h"
//添加控件头文件
#include "QHBoxLayout"
#include "QVBoxLayout"
void Dialog::iniUi()
{
//获取dialog.h定义的函数并且给他们命名
checkBox1 = new QCheckBox(tr("下划线"));
checkBox2= new QCheckBox(tr("斜线"));
chackBox3= new QCheckBox(tr("加粗"));
rBtBlack = new QRadioButton(tr("Black"));
rBtBlue = new QRadioButton(tr("Blue"));
rBtRed = new QRadioButton(tr("Red"));
//设置字体(字体放大点)
plainTextEdit = new QPlainTextEdit();
QFont font = plainTextEdit->font();
font.setPointSize(20);
plainTextEdit->setFont(font);
plainTextEdit->setPlainText("Hello Word\n\n");
btnOK = new QPushButton(tr("Ok"));
btnCancle = new QPushButton(tr("Cancle"));
btnClose = new QPushButton(tr("Close"));
//布局获取的控件
QHBoxLayout *Hlayout1 = new QHBoxLayout;
QHBoxLayout *Hlayout2 = new QHBoxLayout;
QHBoxLayout *Hlayout3 = new QHBoxLayout;
//复选框放入布局文件中
Hlayout1->addWidget(checkBox1);
Hlayout1->addWidget(checkBox2);
Hlayout1->addWidget(chackBox3);
//单选框放入布局文件
Hlayout2->addWidget(rBtBlack);
Hlayout2->addWidget(rBtBlue);
Hlayout2->addWidget(rBtRed);
//按键放入布局文件
Hlayout3->addStretch();
Hlayout3->addWidget(btnOK);
Hlayout3->addWidget(btnCancle);
Hlayout3->addWidget(btnClose);
//把垂直布局嵌入水平布局
QVBoxLayout *Vlayout1 = new QVBoxLayout;
Vlayout1->addLayout(Hlayout1);
Vlayout1->addLayout(Hlayout2);
Vlayout1->addWidget(plainTextEdit);
Vlayout1->addLayout(Hlayout3);
setLayout(Vlayout1);
}
void Dialog::inSignalSlots()
{
//按键的点击事件
connect(btnOK,SIGNAL(clicked()),this,SLOT(accept()));
connect(btnCancle,SIGNAL(clicked()),this,SLOT(reject()));
connect(btnClose,SIGNAL(clicked(bool)),this,SLOT(close()));
//修改字体样式
connect(checkBox1,SIGNAL(clicked(bool)),this,SLOT(onCheckunderline(bool)));
connect(checkBox2,SIGNAL(clicked(bool)),this,SLOT(onCheckItalic(bool)));
connect(chackBox3,SIGNAL(clicked(bool)),this,SLOT(onCheckBold(bool)));
//修改字体颜色
connect(rBtBlack,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
connect(rBtBlue,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
connect(rBtRed,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
}
//下划线
void Dialog::onCheckunderline(bool checked)
{
QFont font = plainTextEdit->font();
font.setUnderline(checked);
plainTextEdit->setFont(font);
}
//字体倾斜
void Dialog::onCheckItalic(bool checked)
{
QFont font = plainTextEdit->font();
font.setItalic(checked);
plainTextEdit->setFont(font);
}
//字体加粗
void Dialog::onCheckBold(bool checked)
{
QFont font = plainTextEdit->font();
font.setBold(checked);
plainTextEdit->setFont(font);
}
//修改字体颜色
void Dialog::setTextFontColor()
{
QPalette plet = plainTextEdit->palette();
if(rBtBlack->isChecked())
plet.setColor(QPalette::Text,Qt::black);
else if (rBtRed->isChecked())
plet.setColor(QPalette::Text,Qt::red);
else if(rBtBlue->isChecked())
plet.setColor(QPalette::Text,Qt::blue);
else
plet.setColor(QPalette::Text,Qt::black);
plainTextEdit->setPalette(plet);
}
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
//初始化ui界面
iniUi();
//初始化点击事件
inSignalSlots();
}
Dialog::~Dialog()
{
}
效果图