当前位置:网站首页>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 :
边栏推荐
- SPH中的粒子初始排列问题(两张图解决)
- Tweenmax emoticon button JS special effect
- 高通WLAN框架学习(30)-- 支持双STA的组件
- Redis introduction complete tutorial: detailed explanation of ordered collection
- The caching feature of docker image and dockerfile
- vim编辑器知识总结
- 位运算符讲解
- 初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
- Redis入门完整教程:Bitmaps
- Redis入门完整教程:有序集合详解
猜你喜欢
随机推荐
CTF competition problem solution STM32 reverse introduction
Redis:Redis消息的发布与订阅(了解)
Duplicate ADMAS part name
SHP data making 3dfiles white film
The Chinese output of servlet server and client is garbled
Redis: redis transactions
VIM editor knowledge summary
一次edu证书站的挖掘
Common methods in string class
Phpcms paid reading function Alipay payment
One of the commonly used technical indicators, reading boll Bollinger line indicators
OSEK标准ISO_17356汇总介绍
Network namespace
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
Actual combat simulation │ JWT login authentication
A complete tutorial for getting started with redis: redis shell
Wechat official account solves the cache problem of entering from the customized menu
Redis入门完整教程:有序集合详解
Servlet+JDBC+MySQL简单web练习