发布时间:2023-01-10 15:30
因为学习嵌入式方面的东西,发现Qt设计桌面应用相对简单,本人以前也用过JAVA与Python开发过桌面应用,单都不是特别理想,所有现在就开始学习Qt开发,这个专栏只是记载我个人学习Qt的一些过去和使用的文件这些,后续将持续跟新,也希望对刚入门Qt小伙伴有所帮助吧!
Qt的下载教程有很多,这里我也给大家放一个下载教程,这个教程有详细的下载流程以及下载应该选择那些东西,但是因为后面的教程需要给钱啦,所以我只是使用的他的下载教程,已经下载好了的小伙伴可以直接跳过这一步啦
我个人使用的版本是Qt 5.9.9,就上面这个教程说的,Qt的5.9系列是持续更新的,可以放心使用
下载好软件以后我们就正式进入项目的建立,Qt有两种方式进行界面的编程,因为我个人之前有开发Android studio 的习惯和经历,个人更习惯使用代码直接进行界面布局开发
打开我们的点dialog.h的文件,在里面添加定义的控件
#ifndef DIALOG_H
#define DIALOG_H
#include
//添加控件头文件
#include
#include
#include
#include
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()
{
}
点击运行程序
运行效果图
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
//添加控件头文件
#include
#include
#include
#include
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()
{
}
效果图
使用Qt的显示出Hello Wordhttp://c.biancheng.net/view/1817.html
【前端】解决Bootstrap3与Bootstrap4同时使用的CSS冲突问题
JavaWeb-Mysql 3-1 Maven--项目构建工具、安装配置、基本使用、IDEA配置Maven、依赖管理
深度学习与TensorFlow:TensorFlow最佳环境配置
【Prometheus二次开发课程 】01 Prometheus简介 (学习笔记)
vs2015+opencv440实现YOLOv4,并训练自己的数据集(完全零基础)
一文剖析HTML块和内联元素以及DIV容器,运维开发必备前端技能,基本功强化训练。