1. 設(shè)計ui界面

ui
2.在qt_test1.h中生命槽函數(shù)
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_qt_test1.h"http://Ui中聲明了qt_testclass是在Ui的命名空間中的
//加了命名空間之后1.2后的add()后就不是一個方法,跟類的使用方法是一樣的.
class qt_test1 : public QMainWindow
{
Q_OBJECT
public:
qt_test1(QWidget *parent = Q_NULLPTR);
private:
Ui::qt_test1Class ui;//namespace
private slots://聲明為slots槽函數(shù)
void on_pushButton_clicked();//命名規(guī)則就是on_(button的名字)_clicked()
};
3. 在qt_test1.cpp中寫入槽函數(shù)的響應(yīng)
#include "qt_test1.h"
#include<iostream>
#include<QDebug>
using namespace std;
const static double PI = 3.1416;
qt_test1::qt_test1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
void qt_test1::on_pushButton_clicked()
{
bool ok;
QString tempStr;
QString valueStr = ui.lineEdit->text();
int valueInt = valueStr.toInt(&ok);//toInt()是轉(zhuǎn)化為int類型
qDebug()<<ok;
double area = valueInt*valueInt*PI;
ui.label_4->setText(tempStr.setNum(area));
}
4.運行程序

image.png