//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();
}