当前位置:网站首页>QT small case "addition calculator"
QT small case "addition calculator"
2022-07-05 07:26:00 【Cukor Chuk】
Qt Small cases 《 adder 》
Header file to be used
- QApplication
- QWidget
- QLabel
- QLineEdit
- QPushButton
- QDoubleValidator
- QHBoxLayout
Case requirements
- Implement an adder ( Simple numerical calculation )
- Horizontal layout , Can be arbitrarily scaled
- x+y=z
- x and y Accept only numbers ;z Only show x+y Result , Cannot edit
- stay x and y The content of is correct, and the equal sign button is disabled
Effect display
- x and y When there are no numbers , The equal sign button is gray ( Forbidden )
- x and y When there are numbers , The equal sign button is on ( Available )
- Press down “=” The result is calculated after the button , The result is displayed in the third blank
Source code
- main.cpp
#include <QApplication>
#include "mywidget.h" // Custom header file , The specific operations of this case are all in this
int main(int argc,char** args)
{
QApplication app(argc,args);
MyWidget w;
w.show();
return app.exec();
}
- mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget> // Control
#include <QPushButton> // Button
#include <QLabel> // label
#include <QLineEdit> // Line edit
#include <QHBoxLayout> // Horizontal layout
#include <QDoubleValidator> // Floating point number checker
class MyWidget : public QWidget
{
Q_OBJECT // Signals and slots are used , So it is Qt Yes C++ Extended grammar of English , So you need to add this macro
public:
explicit MyWidget(QWidget *parent = nullptr); // Constructor declaration
private:
QPushButton* btn; //"="
QLabel* la; //"+"
QLineEdit* x; //x+y=z
QLineEdit* y;
QLineEdit* z;
signals:
public slots: // The specified writing method of slot function
void onxyEdit(); // Respond to x and y Edit content of
void onBtn(); // Respond to "=" Button
};
#endif // MYWIDGET_H
- mywidget.cpp
#include "mywidget.h"
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
// First initialize each component
x=new QLineEdit(this);
y=new QLineEdit(this);
z=new QLineEdit(this);
btn=new QPushButton("=",this);
la=new QLabel("+",this);
// Boxing operation
QHBoxLayout* layout=new QHBoxLayout(this);
layout->addWidget(x);
layout->addWidget(la);
layout->addWidget(y);
layout->addWidget(btn);
layout->addWidget(z);
// Set the characteristics of each component
// Right alignment
x->setAlignment(Qt::AlignRight);
y->setAlignment(Qt::AlignRight);
z->setAlignment(Qt::AlignRight);
//x,y You can only enter Numbers , The others can't
x->setValidator(new QDoubleValidator(this));
y->setValidator(new QDoubleValidator(this));
z->setReadOnly(true);
// The button is disabled at first
btn->setEnabled(false);
// Connect the signal to the slot
//x Trigger the current window when the value of onxyEdit Slot function
connect(x,SIGNAL(textChanged(const QString&)),
this,SLOT(onxyEdit()));
//y Trigger the current window when the value of onxyEdit Slot function
connect(y,SIGNAL(textChanged(const QString&)),
this,SLOT(onxyEdit()));
// Trigger the current window when the button is pressed onBtn Slot function
connect(btn,SIGNAL(clicked(void)),
this,SLOT(onBtn(void)));
}
// When this slot function is triggered, start the button
void MyWidget::onxyEdit()
{
bool xok,yok;
x->text().toDouble(&xok); // If you succeed QString Turn into double be xok==true, otherwise xok==false
y->text().toDouble(&yok); // If you succeed QString Turn into double be yok==true, otherwise yok==false
btn->setEnabled(xok && yok);// If xok and yok All for true be btn The button is activated ( Disable status )
}
// This slot function triggers the calculation of demerits to z In the box
void MyWidget::onBtn()
{
// First convert strings into numbers and then add
double result=x->text().toDouble()+y->text().toDouble();
// The content displayed in the window must be a string , So when the result is calculated, you need to convert the number into a string
QString str=QString::number(result);
// Finally, give the result to z
z->setText(str);
}
边栏推荐
- HDU1231 最大连续子序列(分治or动规or双指针)
- PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
- Unity ugui how to match and transform coordinates between different UI panels or uis
- Raspberry pie 4B arm platform aarch64 PIP installation pytorch
- 并查集理论讲解和代码实现
- Ggplot2 drawing learning notes in R
- Using GEE plug-in in QGIS
- Mipi interface, DVP interface and CSI interface of camera
- [untitled]
- GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
猜你喜欢
Delayqueue usage and scenarios of delay queue
I 用c I 实现队列
DelayQueue延迟队列的使用和场景
IPage can display data normally, but total is always equal to 0
I 用c l 栈与队列的相互实现
[software testing] 03 -- overview of software testing
(tool use) how to make the system automatically match and associate to database fields by importing MySQL from idea and writing SQL statements
[software testing] 06 -- basic process of software testing
The problem of configuring opencv in qt5.13.2 is solved in detail
Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"
随机推荐
Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no
行测--资料分析--fb--高照老师
Ggplot2 drawing learning notes in R
Rough notes of C language (2) -- constants
Graduation thesis project local deployment practice
SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
2022.06.27_ One question per day
Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
Basic knowledge of public security -- FB
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
Unity ugui how to match and transform coordinates between different UI panels or uis
Ugnx12.0 initialization crash, initialization error (-15)
Basic series of SHEL script (III) for while loop
借助 Navicat for MySQL 软件 把 不同或者相同数据库链接中的某数据库表数据 复制到 另一个数据库表中
The mutual realization of C L stack and queue in I
The problem of configuring opencv in qt5.13.2 is solved in detail
【idea】Could not autowire. No beans of xxx type found
selenium 元素定位
【Node】npm、yarn、pnpm 区别
What does soda ash do?