1. 设计ui界面

ui
2.在qt_test1.h中生命槽函数
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_qt_test1.h"//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槽函数
void on_pushButton_clicked();//命名规则就是on_(button的名字)_clicked()
};
3. 在qt_test1.cpp中写入槽函数的响应
#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()是转化为int类型
qDebug()<<ok;
double area = valueInt*valueInt*PI;
ui.label_4->setText(tempStr.setNum(area));
}
4.运行程序

image.png
网友评论