//inputdlg.h
#ifndef INPUTDLG_H
#define INPUTDLG_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QDialog>
#include <QInputDialog>
class InputDlg : public QDialog
{
Q_OBJECT
public:
InputDlg(QWidget *parent = 0);
~InputDlg();
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
//inputdlg.cpp
#include "inputdlg.h"
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);
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()));
}
InputDlg::~InputDlg()
{
}
//字符串输入对话框
void InputDlg::ChangeName()
{
#if 0
QString getText(父窗口,对话框标题,标签提示,输入模式,默认文字,true:确定按钮触发 false:取消按钮触发);
#endif
bool ok;
//字符串输入
QString str = QInputDialog::getText(this,tr("标准字符串输入对话框"),tr("请输入姓名"),
QLineEdit::Normal,nameLabel2->text(),&ok);
if(ok && !str.isEmpty())
nameLabel2->setText(str);
}
//条目选择对话框
void InputDlg::ChangeSex()
{
#if 0
QString getItem(父窗口,对话框标题,标签提示,对象,默认显示条目序号,控件中显示的文字是否可编辑,
true:确定按钮触发 false:取消按钮触发);
#endif
QStringList sexItem;
sexItem << tr("男") << tr("女");
bool ok;
QString str = QInputDialog::getItem(this,tr("标准条目选择对话框"),tr("请选择性别"),sexItem,0,false,&ok);
if(ok && !str.isEmpty())
sexLabel2->setText(str);
}
void InputDlg::ChangeAge()
{
#if 0
QString getInt(父窗口,对话框标题,标签提示,默认显示值,指定范围,,指定步进, true:确定按钮触发 false:取消按钮触发);
#endif
bool ok;
int age = QInputDialog::getInt(this,tr("标准int类型输入对话框"),tr("请输入年龄"),ageLabel2->text().toInt(&ok)
,0,100,1,&ok);
if(ok)
ageLabel2->setText(QString(tr("%1")).arg(age));
}
void InputDlg::ChangeScore()
{
bool ok;
double dTemp = QInputDialog::getDouble(this,tr("标准int类型输入对话框"),tr("请输入年龄"),ageLabel2->text().toInt(&ok)
,0,100,1,&ok);
if(ok)
scoreLabel2->setText(QString(tr("%1")).arg(dTemp));
}
//dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "inputdlg.h"
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QPushButton *inputBtn;
InputDlg *inputDlg;
private slots:
void showInput();
};
#endif // DIALOG_H
//dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
inputBtn = new QPushButton;
inputBtn->setText(tr("标准输入对话框"));
QGridLayout *main = new QGridLayout(this);
main->addWidget(inputBtn,3,0);
connect(inputBtn,SIGNAL(clicked()),this,SLOT(showInput()));
}
Dialog::~Dialog()
{
}
void Dialog::showInput()
{
inputDlg = new InputDlg(this);
inputDlg->show();
}