qt实现简单的‘+’ “-” “*‘' ”/“

.h文件中的代码

namespace Ui
{
class Myqt;
}
class Myqt : public QWidget
{
Q_OBJECT


public:
Myqt(QWidget *parent = Q_NULLPTR);



public slots:
void clickedbtton();
private:
Ui::MyqtClass ui;


QLine *lineEdit;
QLine *lineEdit_2;
QLine *lineEdit_3;


QRadioButton *radioButton_add;
QRadioButton *radioButton_dec;
QRadioButton *radioButton_mul;
QRadioButton *radioButton_sur;
QRadioButton *radioButton_div;


QPushButton *rsultpushButton;
QPushButton *pushButton;

};

.cpp中的代码

Myqt::Myqt(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);

connect(ui.rsultpushButton, SIGNAL(clicked()), this, SLOT(clickedbtton()));
}


void Myqt::clickedbtton()
{
double num1 = 0;
double num2 = 0;
double result = 0;
num1 = ui.lineEdit->text().toDouble();
num2 = ui.lineEdit_2->text().toDouble();


if (ui.radioButton_add->isChecked())
{
result = num1 + num2;
}
else if (ui.radioButton_dec->isChecked())
{


result = num1 - num2;
}
else if (ui.radioButton_mul->isChecked())
{
result = num1 * num2;
}
else if (ui.radioButton_div->isChecked())
{
if (num2 == 0)
{
ui.lineEdit_3->setText("E00000");
return;
}
else
{
result = num1 / num2;
}
}

ui.lineEdit_3->setText(QString::number(result, 'g', 12));

}

main.cpp中的代码


#include "Myqt.h"
#include <QtWidgets/QApplication>


#include <QString>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Myqt w;

w.show();

return a.exec();
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页