QT实现记录上一次用户名和密码功能

说明:在登录时,为了避免每次打开软件都要重新输入账号和密码,可以用QSettings来实现记录功能。

本例子中记录的是ip和port号。

#ifndef PERFORMANCETESTING_H
#define PERFORMANCETESTING_H

#include 
#include "ui_performancetesting.h"

class PerformanceTesting : public QMainWindow
{
	Q_OBJECT

public:
	PerformanceTesting(QWidget *parent = 0);
	~PerformanceTesting();

	private slots:
	void PushButton();

	void WriteInit(QString key, QString value);
	void ReadInit(QString key, QString &value);

private:
	Ui::PerformanceTestingClass ui;
};

#endif // PERFORMANCETESTING_H



#include "performancetesting.h"
#include "handlethreadandtest.h"
#include 
#include 
#include 
PerformanceTesting::PerformanceTesting(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	QString readIP;
	QString readPort;
	ReadInit("IP", readIP);
	ReadInit("PORT", readPort);
	ui.serverIP->setText(readIP);
	ui.serverPort->setText(readPort);
}

PerformanceTesting::~PerformanceTesting()
{
}

void PerformanceTesting::PushButton()
{
	if (ui.serverIP->text() == "" || ui.serverPort->text() == "")
	{
		QMessageBox::information(this, "Warning", "IP and port number can not be empty!", QMessageBox::Ok);
		return;
	}
	WriteInit("IP", ui.serverIP->text());
	WriteInit("PORT", ui.serverPort->text());

	QString ip = ui.serverIP->text();
	int port = ui.serverPort->text().toInt();
	int meetingRoomNum = ui.meetingRoomNum->text().toInt();
	int peopleNum = ui.peopleNum->text().toInt();

	HandleThreadAndTest *threadAndTest = new HandleThreadAndTest();
	threadAndTest->SetIP(ip);
	threadAndTest->SetPort(port);
	threadAndTest->SetMeetingRoomNum(meetingRoomNum);
	threadAndTest->SetPeopleNum(peopleNum);

	this->close();
	threadAndTest->show();
}

void PerformanceTesting::WriteInit(QString key, QString value)
{
	QString path = "ip.ini";

	//创建配置文件操作对象
	QSettings *config = new QSettings(path, QSettings::IniFormat);

	//将信息写入配置文件
	config->beginGroup("config");
	config->setValue(key, value);
	config->endGroup();
	delete config;
}

void PerformanceTesting::ReadInit(QString key, QString &value)
{
	value = QString("");
	QString path = "ip.ini";

	//创建配置文件操作对象
	QSettings *config = new QSettings(path, QSettings::IniFormat);

	//读取配置信息
	value = config->value(QString("config/") + key).toString();
	delete config;
}


QT实现记录上一次用户名和密码功能_第1张图片


第二次运行时,就会填入第一次输入的ip和port。

QT实现记录上一次用户名和密码功能_第2张图片


//参考:http://blog.sina.com.cn/s/blog_a6fb6cc90101bz3c.html
bool Util::writeInit(QString path, QString user_key, QString user_value)
{
	if (path.isEmpty() || user_key.isEmpty())
	{
		return false;
	}
	else
	{
		//创建配置文件操作对象
		QSettings *config = new QSettings(path, QSettings::IniFormat);

		//将信息写入配置文件
		config->beginGroup("config");
		config->setValue(user_key, user_value);
		config->endGroup();

		return true;
	}
}

bool Util::readInit(QString path, QString user_key, QString &user_value)
{
	user_value = QString("");
	if (path.isEmpty() || user_key.isEmpty())
	{
		return false;
	}
	else
	{
		//创建配置文件操作对象
		QSettings *config = new QSettings(path, QSettings::IniFormat);

		//读取用户配置信息
		user_value = config->value(QString("config/") + user_key).toString();

		return true;
	}
}

//获取界面用户输入的配置内容
QString address = address_line_edit->text();  //地址
QString port = port_line_edit->text();  //端口

向user.ini配置文件中写入地址、端口
bool is_address = Util::writeInit(QString("./user.ini"), ADDRESS, address);
bool is_port = Util::writeInit(QString("./user.ini"), PORT, port);

从user.ini配置文件中读取地址、端口
QString address_value;
QString port_value;
bool is_address = Util::readInit(QString("./user.ini"), ADDRESS, address_value);
bool is_port = Util::readInit(QString("./user.ini"), PORT, port_value);


你可能感兴趣的:(QT)