当前位置:网站首页>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 introduction complete tutorial: slow query analysis
- Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
- Summary of wechat applet display style knowledge points
- 剑指Offer 68 - II. 二叉树的最近公共祖先
- 攻防世界 MISC 高手进阶区 001 normal_png
- 【lua】int64的支持
- [sword finger offer] questions 1-5
- Redis入门完整教程:Redis Shell
- Redis introduction complete tutorial: List explanation
猜你喜欢
Unity vscode emmylua configuration error resolution
Advanced area of attack and defense world misc 3-11
Redis入门完整教程:有序集合详解
Duplicate ADMAS part name
攻防世界 MISC 进阶区 Ditf
MYSQL架构——用户权限与管理
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
MySQL Architecture - user rights and management
Detailed explanation of heap sort code
[Jianzhi offer] 6-10 questions
随机推荐
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
Duplicate ADMAS part name
Redis入门完整教程:键管理
A complete tutorial for getting started with redis: hyperloglog
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
A complete tutorial for getting started with redis: getting to know redis for the first time
Summary of wechat applet display style knowledge points
Three stage operations in the attack and defense drill of the blue team
攻防世界 MISC 进阶区 hit-the-core
Editplus-- usage -- shortcut key / configuration / background color / font size
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
Common methods in string class
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
[machine learning] handwritten digit recognition
sobel过滤器
Advanced area of attack and defense world misc 3-11
攻防世界 MISC 高手进阶区 001 normal_png
Feature scaling normalization
[sword finger offer] questions 1-5