- 什么是QT
- QT是一个跨平台的C++图形用户界面应用程序框架。
- 第一个QT应用程序 ---- 计算圆周长的对话框应用程序
- 如图:
- 源码如下:
main.cpp 文件内容:
#include "calcircledialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CalCircleDialog w;
w.show();
return a.exec();
}
calcircledialog.h 文件内容:
#ifndef CALCIRCLEDIALOG_H
#define CALCIRCLEDIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
QT_BEGIN_NAMESPACE
namespace Ui { class CalCircleDialog; }
QT_END_NAMESPACE
#define PI (3.14)
class CalCircleDialog : public QDialog
{
Q_OBJECT
public:
CalCircleDialog(QWidget *parent = nullptr);
~CalCircleDialog();
private slots:
void ShowCirclrRound();
private:
Ui::CalCircleDialog *ui;
private:
QLabel m_labCalParaTip; //输入圆半径提示
QLabel m_labResult;
QLineEdit m_lineCirclePara;
QPushButton m_btnCal;
};
#endif // CALCIRCLEDIALOG_H
calcircledialog.cpp文件内容:
#include "calcircledialog.h"
#include "ui_calcircledialog.h"
CalCircleDialog::CalCircleDialog(QWidget *parent)
: QDialog(parent)
// , ui(new Ui::CalCircleDialog)
{
//ui->setupUi(this);
QGridLayout *mainLayout = new QGridLayout(this);
m_labCalParaTip.setText(tr("请输入圆的半径:"));
m_btnCal.setText(tr("显示圆的周长"));
mainLayout->addWidget(&m_labCalParaTip, 0, 0);
mainLayout->addWidget(&m_lineCirclePara, 0, 1);
mainLayout->addWidget(&m_labResult, 1, 0);
mainLayout->addWidget(&m_btnCal, 1, 1);
connect(&m_btnCal, SIGNAL(clicked()), this, SLOT(ShowCirclrRound()));
connect(&m_lineCirclePara, SIGNAL(textChanged(QString)), this, SLOT(ShowCirclrRound()));
}
CalCircleDialog::~CalCircleDialog()
{
//delete ui;
}
void CalCircleDialog::ShowCirclrRound()
{
QString strValue = m_lineCirclePara.text();
int iValue = strValue.toUInt();
double iRound = 2*iValue*PI;
strValue.setNum(iRound);
m_labResult.setText(strValue);
}