使用标准输入对话框

使用标准输入对话框

这次演示一下使用标准输入框QInputDialog,QInputDialog类目前提供了4中数据类型的输入:字符串,Int数据,double类数据,下拉列表框。

1、创建inputdialog.h

#ifndef INPUTDIALOG_H
#define INPUTDIALOG_H

#include <QtGui>

class InputDlg : public QDialog
{
    Q_OBJECT
public:
    InputDlg();

public:
    QPushButton *nameButton;
    QPushButton *sexButton;
    QPushButton *ageButton;
    QPushButton *statureButton;

    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QLabel *label4;
    QLabel *nameLabel;
    QLabel *sexLabel;
    QLabel *ageLabel;
    QLabel *statureLabel;

private slots:
    void slotName();
    void slotSex();
    void slotAge();
    void slotStature();
};

#endif // INPUTDIALOG_H

2、创建inputdialog.cpp

#include "inputdialog.h"

InputDlg::InputDlg()
{
    setWindowTitle(tr("Input Dialog"));

    label1 = new QLabel(tr("Name : "));
    label2 = new QLabel(tr("Sex : "));
    label3 = new QLabel(tr("Age : "));
    label4 = new QLabel(tr("Stature : "));

    nameLabel = new QLabel(tr("LiMing"));
    nameLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    sexLabel = new QLabel(tr("male"));
    sexLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    ageLabel = new QLabel(tr("25"));
    ageLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    statureLabel = new QLabel(tr("175.5"));
    statureLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    nameButton = new QPushButton;
    nameButton->setIcon(QIcon(":/images/btn.png"));
    sexButton = new QPushButton;
    sexButton->setIcon(QIcon(":/images/btn.png"));
    ageButton = new QPushButton;
    ageButton->setIcon(QIcon(":/images/btn.png"));
    statureButton = new QPushButton;
    statureButton->setIcon(QIcon(":/images/btn.png"));

    QGridLayout *layout = new QGridLayout(this);
    int name = 0;
    int sex = 1;
    int age = 2;
    int stature = 3;

    layout->addWidget( label1, name, 0 );
    layout->addWidget( nameLabel, name, 1 );
    layout->addWidget( nameButton, name, 2 );
    layout->addWidget( label2, sex, 0 );
    layout->addWidget( sexLabel, sex, 1 );
    layout->addWidget( sexButton, sex, 2 );
    layout->addWidget( label3, age, 0 );
    layout->addWidget( ageLabel, age, 1 );
    layout->addWidget( ageButton, age, 2 );
    layout->addWidget( label4, stature, 0 );
    layout->addWidget( statureLabel, stature, 1 );
    layout->addWidget( statureButton, stature, 2 );
    layout->setMargin(15);
    layout->setSpacing(10);
    layout->setColumnMinimumWidth(1,120);

    connect(nameButton,SIGNAL(clicked()),this,SLOT(slotName()));
    connect(sexButton,SIGNAL(clicked()),this,SLOT(slotSex()));
    connect(ageButton,SIGNAL(clicked()),this,SLOT(slotAge()));
    connect(statureButton,SIGNAL(clicked()),this,SLOT(slotStature()));
}

void InputDlg::slotName()
{
    bool ok;
    QString name = QInputDialog::getText(this,tr("User Name"),
                                         tr("Please input new name"),
                                         QLineEdit::Normal,
                                         nameLabel->text(),&ok);

    if(ok && !name.isEmpty())
        nameLabel->setText(name);
}

void InputDlg::slotSex()
{
    QStringList list;
    list << tr("male") << tr("female");
    bool ok;
    QString sex = QInputDialog::getItem(this,tr("Sex"),
                                        tr("Please select sex"),
                                        list,0,false,&ok);
    if(ok)
        sexLabel->setText(sex);
}

void InputDlg::slotAge()
{
    bool ok;
    int age = QInputDialog::getInteger(this,tr("User Age"),
                                       tr("Please input age:"),
                                       ageLabel->text().toInt(),
                                       0,150,1,&ok);
    if(ok)
        ageLabel->setText(QString(tr("%1")).arg(age));
}

void InputDlg::slotStature()
{
    bool ok;
    double d = QInputDialog::getDouble(this,tr("Stature"),
                                       tr("Please input stature:"),
                                       175.00,0,230.00,1,&ok);
    if(ok)
        statureLabel->setText(QString(tr("%1")).arg(d));
}

3、创建main.cpp

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

int main( int argc, char **argv )
{
    QApplication app( argc, argv );
    InputDlg *input = new InputDlg();
    input->show();
    return app.exec();
}

4、创建资源文件inputdialog.qrc
下面演示一下怎么创建资源文件:
a、在工程目录下新建一个images目录,把资源图片放到images目录下面。

b、创建资源文件,命名inputdialog。
这里写图片描述

这里写图片描述

c、用Qt Creator打开inputdialog.qrc文件。
这里写图片描述

d、添加前缀,填进去”/”就行。
这里写图片描述

e、添加文件,把images下的图片资源加进去就ok了,保存文件。一定要保证自己的图片名字和代码里的名字和路径保持一致,要不然会找不到资源图片。
这里写图片描述

这里写图片描述

5、运行代码
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

6、代码和图片资源文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值