代码改变世界

QT中的QInputDialog的小例子

2012-04-10 09:27  Rollen Holt  阅读(7279)  评论(0编辑  收藏  举报

其实这断代码没什么优秀之处,贴出来主要为为了方便自己和他人,因为以后如果用到这一块的话,这些代码可能能够直接拿过来用。

InpugDialog.h头文件:

#ifndef INPUGDIALOG_H
#define INPUGDIALOG_H

#include <QtGui>
#include "ui_inpugdialog.h"

class InpugDialog : public QDialog
{
	Q_OBJECT

public:
	InpugDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
	~InpugDialog();

	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:
	Ui::InpugDialogClass ui;

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

#endif // INPUGDIALOG_H

InpugDialog.cpp文件

#include "inpugdialog.h"

InpugDialog::InpugDialog(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	ui.setupUi(this);
	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"));
	sexLabel=new QLabel(tr("F"));
	ageLabel=new QLabel(tr("12"));
	statureLabel=new QLabel(tr("123"));

	//创建各种修改按钮
	nameButton=new QPushButton(tr("Modify"));
	sexButton=new QPushButton(tr("Modify"));
	ageButton=new QPushButton(tr("Modify"));
	statureButton=new QPushButton(tr("Modify"));

	//布局管理
	QGridLayout *layout=new QGridLayout(this);

	layout->addWidget(label1,0,0);
	layout->addWidget(nameLabel,0,1);
	layout->addWidget(nameButton,0,2);

	layout->addWidget(label2,1,0);
	layout->addWidget(sexLabel,1,1);
	layout->addWidget(sexButton,1,2);

	layout->addWidget(label3,2,0);
	layout->addWidget(ageLabel,2,1);
	layout->addWidget(ageButton,2,2);

	layout->addWidget(label4,3,0);
	layout->addWidget(statureLabel,3,1);
	layout->addWidget(statureButton,3,2);

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

}

InpugDialog::~InpugDialog()
{

}

//各种槽函数

void InpugDialog::slotAge(){
	bool ok;
	int age=QInputDialog::getInteger(this,tr("User Age"),tr("Please input your age"),ageLabel->text().toInt(),0,100,1,&ok);

	if (ok){	
		ageLabel->setText(QString(tr("%1")).arg(age));
	
	}
}

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

void InpugDialog::slotName(){
	bool ok;
	QString name=QInputDialog::getText(this,tr("User Name"),tr("Input your name"),QLineEdit::Normal,nameLabel->text(),&ok);
	if(ok&& !name.isEmpty()){
		nameLabel->setText(name);
	}
}

void InpugDialog::slotStature(){

	bool ok;
	double d=QInputDialog::getDouble(this,tr("User Stature"),tr("please input your stature"),170.00,0,200,2,&ok);
	if(ok){
		statureLabel->setText(QString(tr("%1")).arg(d));
	}
}

main.cpp

#include "inpugdialog.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	InpugDialog *w=new InpugDialog();
	w->show();
	return a.exec();
}

下面粘贴一下效果图:

image

image

image

image

image