当前位置:网站首页>QT personal learning summary
QT personal learning summary
2022-07-04 23:15:00 【Yinzz2】
Qt Learning video
Study Qt The first case :
#include "QLabel"
#include <QApplication>
#include<QPushButton>
int main(int argc, char *argv[])
{
// establish qt Application object
QApplication a(argc, argv);
// Create a label control
QLabel label("Hello Qt!");
// Show label controls
label.show();
// Let the application enter the control
QPushButton button("wuhu");
button.show();
return a.exec();
}
Qt Chinese coding problem
Qt5 It can be understood correctly uft-8 Format encoding , And automatically convert it into internal unicode code . But if you use external coding in other formats , such as ,windows Commonly used GBK code , There will be garbled code
resolvent :
// Include header file
#include<qtextcodec.h>
// Create coding objects
QTextCodec* coder = QTextCodec::codecForName("gbk");
// Will be displayed gbk The encoded string is converted to unicode
QLabel label(coder->toUnicode(" Daddy "));
label.show();
The second case is the parent window
There are three common parent windows 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);
// Create a label control , Dock above the parent window
QLabel label(" I'm the label ",&parent);
label.move(20,40);
// Create button control , Dock above the parent window
QPushButton button(" I'm a button ",&parent);
button.move(20,100);
button.resize(100,100);
// Unwanted delete
QPushButton* button2 = new QPushButton(" I'm a button ",&parent);
button2->move(180,100);
button2->resize(100,100);
// The parent window displays , The above controls display
parent.show();
return app.exec();
}
The third case is signal and slot
// Click the button to close the tab
QObject::connect(&button,SIGNAL(clicked(void)),&label,SLOT(close(void)));
// Add exit button , Press and hold the button to exit the application
QObject::connect(&button2,SIGNAL(pressed(void)),&app,SLOT(quit(void)));
//SLOT(closeAllWindows(void))
//&parent,SLOT(close(void))
Through signal and slot , Implement slider (QSlider) And the selection box (QSpinBox) Synchronous operation
When configuring the slider and value selection box , Learn to use Qt Assistant
int main(int agrc,char** agrv)
{
QApplication app(agrc,agrv);
// Set a parent window
QDialog parent;
parent.resize(300,125);
// Set a slider , Range 1-100
QSlider slider(Qt::Horizontal,&parent);
slider.move(20,75);
slider.setRange(1,100);
// Set a brace , Range 1-100
QSpinBox spin(&parent);
spin.setRange(1,100);
spin.move(120,75);
// Connect with each other
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();
}
The fourth case is time acquisition
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( Check the configuration parameters more Qt assistant)
#include<test.h>
#include<QFont>
timeDialog::timeDialog(void)
{
setWindowTitle(" Time acquisition device ");
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(" Acquisition time ",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();
}
The fifth case is the addition calculator ( Use Qt designed)
.ui
Main learning 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);
}
The colon usage on the constructor above is Initialize class members , Destructors are generally used to deal with aftermath .
The sixth case Qt The designer
.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(" Login successful ");
close();
}
else
{
QMessageBox msgBox(QMessageBox::Critical,
"Error",
" Input error ",
QMessageBox::Ok,
this);
msgBox.exec();
}
}
void testDialog::cancel_solve(void)
{
QMessageBox MsgBox(QMessageBox::Question,
" Sign in ",
" Confirm to cancel login ?",
QMessageBox::Ok|QMessageBox::No,
this);
if(MsgBox.exec()==QMessageBox::Ok)
{
close();
}
}
main.cpp Omit , The end result is as follows :
边栏推荐
- 微信公众号解决从自定义菜单进入的缓存问题
- heatmap. JS picture hotspot heat map plug-in
- Redis入门完整教程:Pipeline
- [try to hack] wide byte injection
- 一次edu证书站的挖掘
- Question brushing guide public
- Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
- 【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
- 字体设计符号组合多功能微信小程序源码
- Google collab trample pit
猜你喜欢
[sword finger offer] questions 1-5
Redis: redis transactions
Redis入門完整教程:Pipeline
Redis: redis configuration file related configuration and redis persistence
Redis introduction complete tutorial: detailed explanation of ordered collection
Editplus-- usage -- shortcut key / configuration / background color / font size
Redis入门完整教程:键管理
EditPlus--用法--快捷键/配置/背景色/字体大小
Redis getting started complete tutorial: Geo
【二叉树】节点与其祖先之间的最大差值
随机推荐
Basic use and upgrade of Android native database
Object detection based on OpenCV haarcascades
Redis入门完整教程:慢查询分析
[crawler] jsonpath for data extraction
Summary of wechat applet display style knowledge points
[crawler] XPath for data extraction
Photoshop批量给不同的图片添加不同的编号
机器学习在房屋价格预测上的应用
MP进阶操作: 时间操作, sql,querywapper,lambdaQueryWapper(条件构造器)快速筛选 枚举类
Redis入门完整教程:Redis Shell
A complete tutorial for getting started with redis: hyperloglog
HMS core unified scanning service
Mysql database backup and recovery -- mysqldump command
Insert sort, select sort, bubble sort
Redis introduction complete tutorial: slow query analysis
Redis getting started complete tutorial: Geo
法国学者:最优传输理论下对抗攻击可解释性探讨
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
UML图记忆技巧
【图论】拓扑排序