当前位置:网站首页>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;
};
#endiftest.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_Hcalculatordialog.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省略,最终效果如下:

边栏推荐
- Unity vscode emmylua configuration error resolution
- Install the gold warehouse database of NPC
- Redis introduction complete tutorial: detailed explanation of ordered collection
- Redis入门完整教程:初识Redis
- 攻防世界 MISC 进阶区 Ditf
- 攻防世界 MISC 进阶 glance-50
- Redis入门完整教程:有序集合详解
- P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
- Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
- On-off and on-off of quality system construction
猜你喜欢

Redis introduction complete tutorial: detailed explanation of ordered collection

Redis入门完整教程:有序集合详解

攻防世界 misc 高手进阶区 a_good_idea

MYSQL架构——逻辑架构
![P2181 diagonal and p1030 [noip2001 popularization group] arrange in order](/img/79/36c46421bce08284838f68f11cda29.png)
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order

Erik baleog and Olaf, advanced area of misc in the attack and defense world

新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码

One of the commonly used technical indicators, reading boll Bollinger line indicators

The small program vant tab component solves the problem of too much text and incomplete display

小程序vant tab组件解决文字过多显示不全的问题
随机推荐
【剑指Offer】6-10题
Attack and defense world misc master advanced zone 001 normal_ png
ECS settings SSH key login
Detailed explanation of heap sort code
Advanced area a of attack and defense world misc Masters_ good_ idea
攻防世界 MISC 进阶区 hit-the-core
Redis getting started complete tutorial: hash description
Redis introduction complete tutorial: slow query analysis
Sword finger offer 68 - I. nearest common ancestor of binary search tree
[roommate learned to use Bi report data processing in the time of King glory in one game]
Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
微信小程序显示样式知识点总结
Redis入门完整教程:HyperLogLog
攻防世界 MISC 进阶区 Ditf
Persistence mechanism of redis
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
SQL中MAX与GREATEST的区别
Install the gold warehouse database of NPC
Summary of wechat applet display style knowledge points
Sword finger offer 68 - ii The nearest common ancestor of binary tree