• Qt5开发从入门到精通——第四篇(标准输入对话框类)


    欢迎小伙伴的点评✨✨,相互学习、互关必回、全天在线🍳🍳🍳
    博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩‍🚀


    前言

    本章节将会给大家带来标准输入对话框的详细使用方法


    一、标准输入对话框类

    1.1、对话框四种数据类型

    标准输入对话框提供四种数据类型的输入,包括字符串、下拉列表框的条目、 int 数据类型和 double 数据类型。

    1.2、标准字符串输入对话框

    标准字符串输入对话框通过 QInputDialog 类的静态函数 getText()完成, getText()函数形式如下:

    QString getText
    (
    	QWidget * parent,               //标准输入对话框的父窗口
    	const QString& title,           //标准输入对话框的标题名
    	const QString& label,           //标准输入对话框的标签提示
    	QLineEdit::EchoMode mode = QLineEdit::Normal, //指定标准输入对话框中 QLineEdit 控件的输入模式
    	const QString& text = QString(),     //标准字符串输入对话框弹出时QLineEdit控件中默认出现的文字
    	bool *ok =0,                        //注
    	Qt::WindowFlags flags = 0           //指明标准输入对话框的窗体标识
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    注:指示标准输入对话框的哪个按钮被触发,若为 true, 则表示用户单击了 “OK” (确定)按钮;若为 false,则表示用户单击了 “Cancle” (取消)按钮。

    1.3、标准条目选择对话框

    标准条目选择对话框是通过 QinputDialog 类的静态函数 getltem()来完成的, getltem()函数形式如下:

    QString getItem
    (
    	QWidget * parent,           //标准输入对话框的父窗口
    	const QString& title,       //标准输入对话框的标题名
    	const QString& label,       //标准输入对话框的标签提示
    	const QStringList& items,   //注(1)
    	int current=0,             //注(2)
    	bool editable = true,      //指定QComboBox控件中显示的文字是否可编辑
    	bool * ok=0,               //注(3)
    	Qt::WindowFlags flags=0    //指明标准输入对话框的窗体标识
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    注:
    (1) 指定标准输入对话框中 QComboBox 控件显示的可选条目为一个 QStringList 对象。
    (2) 标准条目选择对话框弹出时 QComboBox 控件中默认显示的条目序号。
    (3) 指示标准输入对话框的哪个按钮被触发,若 ok 为 true, 则表示用户单击了 “OK”
    ( 确定)按钮;若 ok为 false, 则表示用户单击了 “Cancle” (取消)按钮。

    1.4、标准int类型输入对话框

    标准 int 类型输入对话框是通过 QlnputDialog 类的静态函数 getlnt()来完成的, getlnt()函数形式如下:

    int getInt
    (
    	QWidget * parent,        //标准输入对话框的父窗口
    	const QString& title,    //标准输入对话框的标题名
    	const QString& label,    //标准输入对话框的标签提示
    	int value=0,             //指定标准输入对话框中QSpinBox控件的默认显示值
    	int min = -2147483647,   //指定QSpinBox控件的数值范围
    	int max = 2147483647,    
    	int step=1,              //指定QSpinBox控件的步进值
    	bool* ok=0,              //注
    	Qt::WindowFlags flags=0  //指明标准输入对话框的窗口标识
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.5、标准double 类型输入对话框

    标准 double 类型输入对话框是通过 QlnputDialog 类的静态函数 getDouble()来完成的,getDouble() 函数形式如下:

    double getDouble
    (
    	QWidget* parent,            //标准输入对话框的父窗口
    	const QString& title,       //标准输入对话框的标题名
    	const QString& label,       //标准输入对话框的标签提示
    	double value=0,             //指定标准输入对话框中QSpinBox控件默认的显示值
    	double min = -2147483647,   //指定QSpinBox控件的数值范围
    	double max = 2147483647,
    	int decimals=1,            //指定QSpinBox控件的步进值
    	bool* ok=0,                //注
    	Qt::WindowFlags flags=0    //指明标准输入对话框的窗口标识
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    注:用千指示标准输入对话框的哪个按钮被触发,若 ok 为 true, 则表示用户单击了 “OK” (确定)按钮;若 ok 为 false, 则表示用户单击了 “Cancel” (取消)按钮。

    二、效果实例

    图一
    在这里插入图片描述

    三、原码详解

    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include 
    #include 
    #include 
    #include 
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = nullptr);
        ~Dialog();
    
    private:
        Ui::Dialog *ui;
        QPushButton *inputBtn;
        inputdlg *inputDlg;
        QGridLayout *mainLayout;
    private slots:
    void showInputDlg();
    
    
    };
    
    #endif // DIALOG_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    inputdlg.h

    #ifndef INPUTDLG_H
    #define INPUTDLG_H
    #include 
    #include 
    #include 
    #include 
    
    class inputdlg : public QDialog
    {
        Q_OBJECT
    public:
        inputdlg(QWidget* parent = nullptr);
    private slots:
        void ChangeName();
        void ChangeSex();
        void ChangeAge();
        void ChangeScore();
    private:
        QLabel *nameLabel1;
        QLabel *sexLabel1;
        QLabel *ageLabel1;
        QLabel *scoreLabel1;
        QLabel *nameLabel2;
        QLabel *sexLabel2;
        QLabel *ageLabel2;
        QLabel *scoreLabel2;
        QPushButton *nameBtn;
        QPushButton *sexBtn;
        QPushButton *ageBtn;
        QPushButton *scoreBtn;
        QGridLayout *mainLayout;
    
    };
    
    #endif // INPUTDLG_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include 
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        inputBtn = new QPushButton;                   //创建控件对象
        inputBtn->setText(tr("标准输入对话框实例"));
        inputBtn->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;"); //设置按钮形状
    
        mainLayout = new QGridLayout(this);
        mainLayout->addWidget(inputBtn,1,1);         //布局设计
        connect (inputBtn, SIGNAL (clicked()), this, SLOT (showInputDlg()));
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::showInputDlg()
    {
        inputDlg = new inputdlg(this);
        inputDlg->show();
        qDebug()<<1;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    inputdlg.cpp

    #include "inputdlg.h"
    #include 
    
    inputdlg::inputdlg(QWidget* parent):QDialog (parent)
    {
        setWindowTitle(tr(" 标准输入对话框实例")) ;
        nameLabel1 =new QLabel;
        nameLabel1->setText(tr(" 姓名:"));
        nameLabel2 =new QLabel;
        nameLabel2->setText(tr(" 周何骏")) ; //姓名的初始值
        nameLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
        nameBtn =new QPushButton;
        nameBtn->setText(tr(" 修改姓名")) ;
        sexLabel1 =new QLabel;
        sexLabel1->setText(tr(" 性别:"));
        sexLabel2 =new QLabel;
        sexLabel2->setText(tr(" 男")) ; //性别的初始值
        sexLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
        sexBtn =new QPushButton;
        sexBtn->setText(tr(" 修改性别"));
        ageLabel1 =new QLabel;
        ageLabel1->setText(tr(" 年龄:"));
        ageLabel2 =new QLabel;
        ageLabel2->setText(tr("21")); //年龄的初始值
        ageLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
        ageBtn =new QPushButton;
        ageBtn->setText(tr(" 修改年龄")) ;
    
        scoreLabel1 =new QLabel;
        scoreLabel1->setText(tr(" 成绩:"));
        scoreLabel2 =new QLabel;
        scoreLabel2->setText(tr ("80")); //成绩的初始值
        scoreLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
        scoreBtn =new QPushButton;
        scoreBtn->setText(tr("修改成绩")) ;
        mainLayout =new QGridLayout(this);
        mainLayout->addWidget(nameLabel1,0,0);
        mainLayout->addWidget(nameLabel2,0,1);
        mainLayout->addWidget(nameBtn,0,2);
        mainLayout->addWidget(sexLabel1,1,0);
        mainLayout->addWidget(sexLabel2,1,1);
        mainLayout->addWidget(sexBtn,1,2);
        mainLayout->addWidget(ageLabel1,2,0);
        mainLayout->addWidget(ageLabel2,2,1);
        mainLayout->addWidget(ageBtn,2,2);
        mainLayout->addWidget(scoreLabel1,3,0);
        mainLayout->addWidget(scoreLabel2,3,1);
        mainLayout->addWidget(scoreBtn,3,2);
        mainLayout->setMargin(15);
        mainLayout->setSpacing(10);
        connect (nameBtn, SIGNAL (clicked()), this, SLOT (ChangeName()));
        connect (sexBtn, SIGNAL (clicked()), this, SLOT (ChangeSex ()));
        connect (ageBtn, SIGNAL (clicked()), this, SLOT (ChangeAge ()));
        connect(scoreBtn,SIGNAL(clicked()) ,this,SLOT(ChangeScore()));
    }
    
    
    void inputdlg::ChangeName()    //修改姓名
    {
        bool ok;
        QString text=QInputDialog::getText(this,tr("标准字符串输入对话框"), tr("请输入姓名: "), QLineEdit::Normal, nameLabel2->text (), &ok);
        if (ok && !text.isEmpty())
        nameLabel2->setText(text);
    }
    
    void inputdlg::ChangeSex()   //修改性别
    {
        QStringList Sexitems;
        Sexitems << tr("男") << tr(" 女") ;
        bool ok;
        QString Sexitem = QInputDialog::getItem(this, tr("标准条目选择对话框"),
        tr(" 请选择性别: "), Sexitems, 0, false, &ok);
        if (ok && !Sexitem.isEmpty())
        sexLabel2->setText(Sexitem);
    }
    void inputdlg::ChangeAge()  //修改年龄
    {
        bool ok;
        int age= QInputDialog::getInt(this, tr("标准 int 类型输入对话框n"),tr(" 请输入年龄: "), ageLabel2->text().toInt(&ok), 0, 100, 1, &ok);
        if (ok)
        ageLabel2->setText(QString(tr("%1")) . arg (age)) ;
    
    }
    void inputdlg::ChangeScore() //修改成绩
    {
        bool ok;
        double score= QInputDialog::getDouble(this, tr("标准 double 类型输入对话框"),tr(" 请输入成绩: "),scoreLabel2->text().toDouble(&ok), 0, 100, 1, &ok);
        if (ok)
        scoreLabel2->setText(QString(tr("%1")) .arg(score));
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91

    main.cpp

    #include "dialog.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        inputdlg w1;
        w.show();
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    总结

    标准输入对话框也是在应用程序中经常用到的

  • 相关阅读:
    软件项目管理 4.1.软件需求管理过程
    app逆向(9)|APP关于抓包的常见问题
    HOG特征学习笔记
    C/C++数据结构之Hash与BloomFilter详解
    MobileNetV2详解与多动物分类实战
    使用并查集生成一个迷宫
    本地FTP服务器快速搭建(windows)
    浅谈Solidity智能合约DAPP项目系统开发技术逻辑(Solidity语言框架)
    Python 笔记06(Mysql数据库)
    JS备忘录
  • 原文地址:https://blog.csdn.net/weixin_44759598/article/details/126561495