当前位置:网站首页>Qt个人学习总结
Qt个人学习总结
2022-07-04 22:34:00 【Yinzz2】
Qt学习视频
学习Qt第一个案例:
#include "QLabel"
#include <QApplication>
#include<QPushButton>
int main(int argc, char *argv[])
{
//创建qt应用程序对象
QApplication a(argc, argv);
//创建标签控件
QLabel label("Hello Qt!");
//显示标签控件
label.show();
//让应用程序进入控件
QPushButton button("wuhu");
button.show();
return a.exec();
}
Qt中文编码问题
Qt5可以正确理解uft-8格式编码,并将其自动转换为内部的unicode编码。但如果使用其它格式的外部编码,比如,windows中常用的GBK编码,将会出现乱码现象
解决方法:
//包含头文件
#include<qtextcodec.h>
//创建编码对象
QTextCodec* coder = QTextCodec::codecForName("gbk");
//将显示gbk编码字符串转换为unicode
QLabel label(coder->toUnicode("爹"));
label.show();
第二个案例父窗口
常用父窗口有三个Qwidget,QMainWindow,Qdialog
#include<QApplication>
#include<QWidget>
#include<QLabel>
#include<QPushButton>
#include<QDialog>
#include<QMainWindow>
int main(int argc,char** argv)
{
QApplication app(argc,argv);
QDialog parent;
parent.resize(320,240);
parent.move(50,50);
//创建标签控件,停靠在父窗口上面
QLabel label("我是标签",&parent);
label.move(20,40);
//创建按钮控件,停靠在父窗口上面
QPushButton button("我是按键",&parent);
button.move(20,100);
button.resize(100,100);
//不需要delete
QPushButton* button2 = new QPushButton("我是按键",&parent);
button2->move(180,100);
button2->resize(100,100);
//父窗口显示,上面的控件一样显示
parent.show();
return app.exec();
}
第三个案例信号与槽
//点击按钮关闭标签
QObject::connect(&button,SIGNAL(clicked(void)),&label,SLOT(close(void)));
//增加退出按钮,实现按住按钮退出应用程序
QObject::connect(&button2,SIGNAL(pressed(void)),&app,SLOT(quit(void)));
//SLOT(closeAllWindows(void))
//&parent,SLOT(close(void))
通过信号与槽,实现滑块(QSlider)与选值框(QSpinBox)同步运行
配置滑块与选值框时候,要学会利用Qt Assistant
int main(int agrc,char** agrv)
{
QApplication app(agrc,agrv);
//设置一个父窗口
QDialog parent;
parent.resize(300,125);
//设置一个滑块,范围1-100
QSlider slider(Qt::Horizontal,&parent);
slider.move(20,75);
slider.setRange(1,100);
//设置一个托条,范围1-100
QSpinBox spin(&parent);
spin.setRange(1,100);
spin.move(120,75);
//相互建立联系
QObject::connect(&slider,SIGNAL(valueChanged(int)),&spin,SLOT(setValue(int)));
QObject::connect(&spin,SIGNAL(valueChanged(int)),&slider,SLOT(setValue(int)));
parent.show();
return app.exec();
}
第四个案例时间获取器
test.h
#ifndef _TEST_H
#define _TEST_H
#include<QLabel>
#include<QDialog>
#include<QPushButton>
#include<QDebug>
#include<QTime>
#include<QVBoxLayout>
class timeDialog:public QDialog
{
Q_OBJECT
public:
timeDialog(void);
public slots:
void get_Time(void);
private:
QLabel* m_label;
QPushButton* m_button;
};
#endif
test.cpp(配置参数多查Qt assistant)
#include<test.h>
#include<QFont>
timeDialog::timeDialog(void)
{
setWindowTitle("时间获取器");
m_label = new QLabel(this);
m_label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
m_label->setLineWidth(2);
m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QFont font;
font.setPointSize(20);
m_label->setFont(font);
m_button = new QPushButton("获取时间",this);
m_button->setFont(font);
QVBoxLayout* layout=new QVBoxLayout(this);
layout->addWidget(m_label);
layout->addWidget(m_button);
setLayout(layout);
connect(m_button,SIGNAL(clicked(void)),this,SLOT(get_Time(void)));
}
void timeDialog::get_Time(void)
{
qDebug("getTime");
QTime time = QTime::currentTime();
QString str = time.toString("hh:mm:ss");
m_label->setText(str);
}
main.cpp
#include "test.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
timeDialog w;
w.show();
return a.exec();
}
第五个案例加法计算器(使用Qt designed)
.ui
主要学习calculatordialog.h
#ifndef CALCULATORDIALOG_H
#define CALCULATORDIALOG_H
#include "ui_dialog.h"
#include "QValidator"
class calculatorDialog:public QDialog
{
Q_OBJECT
public:
calculatorDialog(void);
~calculatorDialog(void);
public slots:
void enableButton(void);
void calcCliked(void);
private:
Ui::Dialog* ui;
};
#endif // CALCULATORDIALOG_H
calculatordialog.cpp
#include "calculatordialog.h"
calculatorDialog::calculatorDialog(void):ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->edit_x->setValidator(new QDoubleValidator(this));
ui->edit_y->setValidator(new QDoubleValidator(this));
connect(ui->edit_x,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));
connect(ui->edit_y,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));
connect(ui->m_button,SIGNAL(clicked(void)),this,SLOT(calcCliked(void)));
}
calculatorDialog::~calculatorDialog(void)
{
delete ui;
}
void calculatorDialog::enableButton(void)
{
bool judge_x,judge_y;
ui->edit_x->text().toDouble(&judge_x);
ui->edit_y->text().toDouble(&judge_y);
ui->m_button->setEnabled(judge_x && judge_y);
}
void calculatorDialog::calcCliked(void)
{
double res = ui->edit_x->text().toDouble() + ui->edit_y->text().toDouble();
QString str = QString::number(res);
ui->edit_z->setText(str);
}
上面在构造函数上架冒号用法是对类成员进行初始化,析构函数一般用于善后。
第六个案例Qt设计师
.ui
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <ui_dialog.h>
#include <QMessageBox>
#include <QDebug>
class testDialog:public QDialog
{
Q_OBJECT
public:
testDialog(void);
~testDialog(void);
public slots:
void ok_solve(void);
void cancel_solve(void);
private:
Ui::Dialog* ui;
};
dialog.cpp
#include "dialog.h"
testDialog::testDialog(void):ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->buttonBox,SIGNAL(accepted(void)),this,SLOT(ok_solve(void)));
connect(ui->buttonBox,SIGNAL(rejected(void)),this,SLOT(cancel_solve(void)));
}
testDialog::~testDialog(void)
{
delete ui;
}
void testDialog::ok_solve(void)
{
if(ui->lineEdit->text()=="ysj" && ui->lineEdit_2->text()=="123456")
{
qDebug("登录成功");
close();
}
else
{
QMessageBox msgBox(QMessageBox::Critical,
"Error",
"输入错误",
QMessageBox::Ok,
this);
msgBox.exec();
}
}
void testDialog::cancel_solve(void)
{
QMessageBox MsgBox(QMessageBox::Question,
"登录",
"确定取消登录?",
QMessageBox::Ok|QMessageBox::No,
this);
if(MsgBox.exec()==QMessageBox::Ok)
{
close();
}
}
main.cpp省略,最终效果如下:
边栏推荐
- Redis入门完整教程:慢查询分析
- 堆排序代码详解
- Google collab trample pit
- [ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
- Three stage operations in the attack and defense drill of the blue team
- Redis入门完整教程:Pipeline
- Attack and Defense World MISC Advanced Area Erik baleog and Olaf
- Insert sort, select sort, bubble sort
- 【剑指Offer】6-10题
- sobel过滤器
猜你喜欢
Three stage operations in the attack and defense drill of the blue team
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
Attack and defense world misc master advanced zone 001 normal_ png
[OpenGL] note 29 anti aliasing (MSAA)
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
Talk about Middleware
攻防世界 MISC 进阶区 hong
Redis getting started complete tutorial: hash description
攻防世界 MISC 进阶区 3-11
Install the gold warehouse database of NPC
随机推荐
Advanced area a of attack and defense world misc Masters_ good_ idea
Redis入门完整教程:客户端通信协议
Mongodb aggregation operation summary
The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
特征缩放 标准化 归一化
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
One of the commonly used technical indicators, reading boll Bollinger line indicators
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
通过Go语言创建CA与签发证书
String类中的常用方法
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
Photoshop批量给不同的图片添加不同的编号
Sword finger offer 68 - I. nearest common ancestor of binary search tree
How to send a reliable request before closing the page
Analysis of environmental encryption technology
Notepad++--编辑的技巧
Principle of lazy loading of pictures
Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet