QtGui实现计算圆的面积

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QtWidgets/QDialog>
#include <QtWidgets/QLabel>
#include <QtWidgets//QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QGridLayout>
const static double PI = 3.1416;
class Dialog : public QDialog
{
	Q_OBJECT//启动Qt元对象系统(比如支持SIGNAL/SLOT)

public:
	Dialog(QWidget *parent = 0);
	~Dialog();

private:
	QLabel *label1, *label2;
	QLineEdit *lineEdit;
	QPushButton *button;

private slots:
	void showArea();
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
	: QDialog(parent)
{
	label1 = new QLabel(this);
	label1->setText(QStringLiteral("请输入圆的半径:"));

	lineEdit = new QLineEdit(this);
	
	label2 = new QLabel(this);
	label2->setFrameStyle(QFrame::Panel | QFrame::Sunken);

	button = new QPushButton(this);
	button->setText(QStringLiteral("显示对应圆的面积:"));

	QGridLayout *mainLayout = new QGridLayout(this);
	mainLayout->addWidget(label1, 0, 0);
	mainLayout->addWidget(lineEdit, 0, 1);
	mainLayout->addWidget(label2, 1, 0);
	mainLayout->addWidget(button, 1, 1);

	connect(button, SIGNAL(clicked()), this, SLOT(showArea()));
	//connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(showArea()));
}

Dialog::~Dialog()
{

}

void Dialog::showArea()
{
	bool ok;
	QString tempStr;
	QString valueStr = lineEdit->text();
	int valueInt = valueStr.toInt(&ok);
	double area = valueInt * valueInt * PI;
	label2->setText(tempStr.setNum(area));
}


Qt5已经删除了setCodecxx,同样滥用tr的副作用也消除了。

在遇到中文时,可以用

QStringLiteral

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值