[QT]QT教程之实例分析[二]各种标准输入框[QInputDialog]时间:2021-04-19 05:30:17重点知识已近在代码里注释... 请仔细看代码 本文原创 转载请保留此链接 http://blog.csdn.net/siren0203 头文件 input .h #ifndef INPUT_H#define INPUT_H#include <QObject>#include <QDialog>class QPushButton;class QLabel;class InputDlg:public QDialog{ Q_OBJECTpublic: InputDlg();private: 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 slotAge(); void slotSex(); void slotStature();};#endif // INPUT_H 实现代码 main.cpp #include <QApplication>#include <QDialog>#include <QTextCodec>#include <QLabel>#include <QFrame>#include <QPushButton>#include <QGridLayout>#include <QInputDialog>#include <QStringList>#include "input.h"InputDlg::InputDlg(){//设置窗口标题 setWindowTitle("input dialog"); //创建标签对象 label1=new QLabel(tr("Name")); label2=new QLabel(tr("Age")); label3=new QLabel(tr("Sex")); label4=new QLabel(tr("Stature")); nameLabel=new QLabel; nameLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); ageLabel=new QLabel; ageLabel->setFrameStyle((QFrame::Panel | QFrame::Sunken)); sexLabel=new QLabel; sexLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); statureLabel=new QLabel; statureLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);//创建按钮对象 nameButton=new QPushButton(tr("Name")); ageButton=new QPushButton(tr("Age")); sexButton=new QPushButton(tr("Sex")); statureButton=new QPushButton(tr("Stature"));//布局管理 QGridLayout *layout=new QGridLayout(this); layout->addWidget(nameButton ,0,3); layout->addWidget(nameLabel,0,2); layout->addWidget(label1,0,1); layout->addWidget(ageButton,1,3); layout->addWidget(ageLabel,1,2); layout->addWidget(label2,1,1); layout->addWidget(sexButton,2,3); layout->addWidget(sexLabel,2,2); layout->addWidget(label3,2,1); layout->addWidget(statureButton,3,3); layout->addWidget(statureLabel,3,2); layout->addWidget(label4,3,1); setLayout(layout);//链接信号和槽 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::slotAge(){ bool ok; //弹出整型对话输入框//// /getInt ( QWidget * parent, 父窗口// const QString & title, 窗口标题// const QString & label, 窗口提示// int value = 0, 默认值// int min = -2147483647, 最小值// int max = 2147483647, 最大值// int step = 1, 步进值// bool * ok = 0, 哪个按钮被触发// Qt::WindowFlags flags = 0 ) int age=QInputDialog::getInteger(this,tr("user age"),tr("please enter age"), ageLabel->text().toInt(),0,140,1,&ok); if(ok){ ageLabel->setText(QString(tr("%1").arg(age))); }}void InputDlg::slotName(){ bool ok; //弹出标准字符串输入对话框 //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 ) 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("man")<<tr("girl"); bool ok; //弹出 标准条目选择对话框// getItem ( QWidget * parent, 条目选择对话框的父窗口// const QString & title, 条目选择对话框的标题// const QString & label, 条目选择对话框的提示信息// const QStringList & items,指定标准对话框中 QComboBox的可选条目// int current = 0, 条目对话框 默认的条目的序号// bool editable = true, QComboBox是否可编辑// bool * ok = 0, 指示哪个按钮被触发// Qt::WindowFlags flags = 0 指定窗体标示// ) QString sex=QInputDialog::getItem(this,tr("user sex"),tr("please input new age"),list,0,false,&ok); if(ok){ sexLabel->setText(sex); }}void InputDlg::slotStature(){ bool ok; //弹出标准浮点型输入框// getDouble ( QWidget * parent, 浮点型输入框的父窗口// const QString & title, 窗口标题// const QString & label, 窗口提示// double value = 0, 默认值// double min = -2147483647,最小值// double max = 2147483647, 最大值// int decimals = 1, 步进值// bool * ok = 0, 指示哪个按钮被触发// Qt::WindowFlags flags = 0 指定窗体标示// ) double stature=QInputDialog::getDouble(this,tr("user stature"), tr("please enter your stature"), 175.00,0,300.00,1,&ok); if(ok){ statureLabel->setText(QString(tr("%1").arg(stature))); }}int main(int argc,char ** argv){ QApplication app(argc,argv); InputDlg dlg; dlg.show(); return app.exec();} 贴图...