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