輸入對話框(QInputDialog)

在程式中常出現一些簡短的訊息方塊,Qt使用QInputDialog來讓使用者輸入資訊,以供程式後續使用。這邊示範了四種形式的輸入對話框,分別為輸入字串、選項、整數、浮點數,按下按鍵即可跳出相對應的對話框。


dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QDialog>

class InputDlg : public QDialog
{
    Q_OBJECT

public:
    InputDlg(QWidget* parent=0);
private slots:
    void InputName();
    void InputSex();
    void InputAge();
    void InputHeight();
private:
    QLabel *nameLabel1;
    QLabel *sexLabel1;
    QLabel *ageLabel1;
    QLabel *heightLabel1;
    QLabel *nameLabel2;
    QLabel *sexLabel2;
    QLabel *ageLabel2;
    QLabel *heightLabel2;
    QPushButton *nameBtn;
    QPushButton *sexBtn;
    QPushButton *ageBtn;
    QPushButton *heightBtn;
    QGridLayout *mainLayout;
};

#endif

dialog.cpp

#include "dialog.h"
#include <QInputDialog>

InputDlg::InputDlg(QWidget *parent) :
    QDialog(parent)
{
    setWindowTitle(QStringLiteral("輸入對話框"));
    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(""));
    ageLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    ageBtn = new QPushButton;
    ageBtn->setText(tr("輸入年齡"));

    heightLabel1 = new QLabel;
    heightLabel1->setText(tr("身高:"));
    heightLabel2 = new QLabel;
    heightLabel2->setText(tr(""));
    heightLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    heightBtn = new QPushButton;
    heightBtn->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(heightLabel1,3,0);
    mainLayout->addWidget(heightLabel2,3,1);
    mainLayout->addWidget(heightBtn,3,2);

    connect(nameBtn,SIGNAL(clicked()),this,SLOT(InputName()));
    connect(sexBtn,SIGNAL(clicked()),this,SLOT(InputSex()));
    connect(ageBtn,SIGNAL(clicked()),this,SLOT(InputAge()));
    connect(heightBtn,SIGNAL(clicked()),this,SLOT(InputHeight()));
}

void InputDlg::InputName(){
    bool ok;
    QString text = QInputDialog::getText(this,tr("字串輸入對話框"),tr("請輸入姓名:"),QLineEdit::Normal,nameLabel2->text(),&ok);
    if (ok && !text.isEmpty()){
        nameLabel2->setText(text);
    }
} 

void InputDlg::InputSex(){
    QStringList SexItems;
    SexItems << QString("男") << QString("女");
    bool ok;

    //參數0表示初始QStringList的第一項,參數false表示選項的值無法更改
    QString SexItem = QInputDialog::getItem(this, tr("選項輸入對話框"),tr("請選擇性別:"),SexItems,0,false,&ok);
    if (ok && !SexItem.isEmpty()){
        sexLabel2->setText(SexItem);
    }
}

void InputDlg::InputAge(){
    bool ok;

    //輸入範圍從0到150,上下間隔為1
    int age = QInputDialog::getInt(this, QStringLiteral("整數輸入對話框"),QStringLiteral("請輸入年齡:"), ageLabel2->text().toInt(&ok), 0, 150, 1, &ok);
    if (ok && age!=0){
        ageLabel2->setText(QString::number(age));
    }
}

void InputDlg::InputHeight(){
    bool ok;

    //輸入範圍從0到300,小數點後1位數
    double score = QInputDialog::getDouble(this, QStringLiteral("浮點數輸入對話框"),
    QStringLiteral("請輸入成績:"),heightLabel2->text().toDouble(&ok), 0, 300, 1, &ok);
    if(ok){
        heightLabel2->setText(QString::number(score));
    }
}

main.cpp

#include <QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    InputDlg myDlg;
    myDlg.show();
    return a.exec();
}

QInputDialog

QInputDialog

QInputDialog

QInputDialog

QInputDialog