点击按钮计算,获取输入的数量和单价,相乘计算后将结果显示到文本框里
QtGuiApplication6.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication6.h"
class QtGuiApplication6 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication6(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication6Class ui;
private slots:
void on_pushButton_clicked();
};
QtGuiApplication6.cpp
#include "QtGuiApplication6.h"
#include <qmessagebox.h>
QtGuiApplication6::QtGuiApplication6(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
void QtGuiApplication6::on_pushButton_clicked()
{
int num = ui.lineEdit->text().toInt();
float price = ui.lineEdit_2->text().toFloat();
float total = num * price;//相乘计算
QString str;//
str=str.setNum(total,'f',2); //浮点数2位小数
str=str.sprintf("%.2f",total); //格式化输出浮点数
ui.lineEdit_3->setText(str);//在文本框里显示 editTotal为组件名
}
结果: