Qt标准对话框之输入对话框(QInputDialog)

public slots:
    void inputText();
    void inputInt();
    void inputDouble();
private:
    QLineEdit *line1;
    QLineEdit *line2;
    QLineEdit *line3;

构造函数

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(600,600);
    QPushButton *btn1 = new QPushButton(tr("输入用户名"),this);
    QPushButton *btn2 = new QPushButton(tr("输入年龄"),this);
    QPushButton *btn3 = new QPushButton(tr("输入金额"),this);
    line1 = new QLineEdit(this);
    line2 = new QLineEdit(this);
    line3 = new QLineEdit(this);
    line1->move(100,150);
    line2->move(100,200);
    line3->move(100,250);
    btn1->move(220,150);
    btn2->move(220,200);
    btn3->move(220,250);
    connect(btn1,&QPushButton::clicked,this,&MainWindow::inputText);
    connect(btn2,&QPushButton::clicked,this,&MainWindow::inputInt);
    connect(btn3,&QPushButton::clicked,this,&MainWindow::inputDouble);
}

inputText

void MainWindow::inputText()
{
    bool ok;
    QString text = QInputDialog::getText(this,tr("输入字符串"),tr("请输入用户名:"),QLineEdit::Normal,tr("游客"),&ok);
    if(ok) line1->setText(text);
}

inputInt

void MainWindow::inputInt()
{
    bool ok;
                                                              //初始值 最小值 最大值 滑动值
    int age = QInputDialog::getInt(this,tr("输入年龄"),tr("age"),18,18,100,1,&ok);
    if(ok) line2->setText(QString::number(age,10));
}

inputDouble

void MainWindow::inputDouble()
{
    bool ok;
    double price = QInputDialog::getDouble(this,tr("输入金额"),tr("金额"),0.0,0.0,1000.0,2,&ok);
    if(ok) line3->setText(QString::number(price,'g',6));
}

你可能感兴趣的:(Qt)