信号和槽的一个测试demo--计算球的体积

发布时间:2024年01月21日

在这里插入图片描述

在这里插入图片描述

  • v_sum.pro
#-------------------------------------------------
#
# Project created by QtCreator 2023-08-19T15:54:09
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = v_sum
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        dialog.cpp

HEADERS += \
        dialog.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

  • dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

// 引入标签、命令按钮等对应头文件
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlineedit.h>

class Dialog : public QDialog
{
    Q_OBJECT

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

private:
    QLabel *lab1,*lab2;
    QLineEdit *lEdit;
    QPushButton *pbt;

private slots:
    void CalcBallVolume(); // 槽函数计算圆球的体积

};
#endif // DIALOG_H
  • dialog.cpp

#include "dialog.h"

const static double PI=3.1415;
#include <QGridLayout>  // QGridLayout(表格布局)

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    // 创建第1个标签(提示用户输入圆球的半径)
    lab1=new QLabel(this);
    lab1->setText(tr("请输入圆球的半径:"));

    // 创建第2个标签(专门用来显示计算圆球体积的结果)
    lab2=new QLabel(this);

    // 创建一个编辑框控件(专门用于接收用户的输入圆球半径的值)
    lEdit=new QLineEdit(this);

    // 创建命令按钮
    pbt=new QPushButton(this);
    pbt->setText(tr("计算圆球体积"));

    // 表格布局
    QGridLayout *mLay=new QGridLayout(this);
    mLay->addWidget(lab1,0,0);
    mLay->addWidget(lEdit,0,1);
    mLay->addWidget(lab2,1,0);
    mLay->addWidget(pbt,1,1);

    //connect(lEdit,SIGNAL(textChanged(QString)),this,SLOT(CalcBallVolume()));
    connect(pbt,SIGNAL(clicked(bool)),this,SLOT(CalcBallVolume()));
}

Dialog::~Dialog()
{
}

void Dialog::CalcBallVolume() // 槽函数计算圆球的体积
{
    bool isLoop;
    QString tempStr;
    QString valueStr=lEdit->text();

    int valueInt=valueStr.toInt(&isLoop);
    double dVSum=4.0/3.0*PI*valueInt*valueInt*valueInt;
    lab2->setText(tempStr.setNum(dVSum));
}


  • main.cpp
#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.setWindowTitle("this is my dialog");
    w.show();

    return a.exec();
}

在这里插入图片描述

文章来源:https://blog.csdn.net/mantouyouyou/article/details/135730813
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。