QT中的QInputDialog的小例子

简介:

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

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

目录
相关文章
|
开发者
冷门但好看的 VSCode 主题推荐
笔者在使用VSCode进行开发的过程中喜欢没事就逛一逛插件商店里的颜色主题,也看过国内外许多论坛上面的颜色主题推荐,不知不觉已经下载了超过一百个的颜色主题。这篇文章总结了我用过的最舒服的一些颜色主题。
7704 0
冷门但好看的 VSCode 主题推荐
|
网络协议 Docker 容器
Docker容器内不能联网的6种解决方案
Docker容器内不能联网的6种解决方案   注:下面的方法是在容器内能ping通公网IP的解决方案,如果连公网IP都ping不通,那主机可能也上不了网(尝试ping 8.
12019 2
|
7月前
|
安全 Android开发 iOS开发
escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
escrcpy 是一款基于 Scrcpy 的开源项目,使用 Electron 构建,提供图形化界面来显示和控制 Android 设备。它支持 USB 和 Wi-Fi 连接,帧率可达 30-120fps,延迟低至 35-70ms,启动迅速且画质清晰。escrcpy 拥有丰富的功能,包括自动化任务、多设备管理、反向网络共享、批量操作等,无需注册账号或广告干扰。适用于游戏直播、办公协作和教育演示等多种场景,是一款轻量级、高性能的 Android 控制工具。
543 1
|
设计模式 前端开发 C语言
【设计模式】 观察者模式介绍及C代码实现
观察者模式(Observer Pattern)是一种常用的设计模式,它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,当主题对象发生变化时,它的所有观察者都会收到通知并更新自己的状态。观察者模式又称为发布-订阅模式。Subject(主题):被观察的对象,它将所有观察者对象的引用保存在一个集合中,并提供了添加和删除观察者对象的方法。Observer(观察者):观察者接口,定义了更新自己的状态的方法,以便主题在状态发生变化时通知观察者。ConcreteSubject(具体主题)
620 0
【设计模式】 观察者模式介绍及C代码实现
|
Linux 网络安全 数据安全/隐私保护
CentOS 7安装配置vsftp并搭建FTP(一)
CentOS 7安装配置vsftp并搭建FTP(一)
24781 0
CentOS 7安装配置vsftp并搭建FTP(一)
|
Java 数据库 开发者
Spring注解大揭秘:@Component、@Service、@Repository详解
Spring注解大揭秘:@Component、@Service、@Repository详解
769 0
|
存储 缓存 Unix
C语言第四章(进程间的通信,管道通信,pipe()函数)
C语言第四章(进程间的通信,管道通信,pipe()函数)
642 0
|
JSON NoSQL C++
VScode调试C/C++项目调试多个C++程序makefile
VScode调试C/C++项目调试多个C++程序makefile
748 0
|
Linux C++
vscode中配置cmake及debug使用
vscode中配置cmake及debug使用
1991 0
|
Devops Shell 数据中心
Docker容器迁移到其他服务器的5种方法
迁移在许多情况下都是不可避免的。硬件升级、数据中心变化、过时的操作系统,所有这些都可能成为迁移的触发点。
16742 3