修改注册表QSettings

时间:2022-09-05 09:19:45

QSettings可以修改注册表中的信息

下面是自己做的修改IE代理的例子:

setHttpProxy.h

#ifndef SETHTTPPROXY_H
#define SETHTTPPROXY_H

#include <QObject>
#include <QSettings>

class setHttpProxy : public QObject
{
Q_OBJECT
public:
explicit setHttpProxy(QObject *parent = 0);
// 检查是否已设置代理
bool checkHttpProxy();
// 退出代理设置
void close();
// 设置代理
void setIEValue();

signals:

public slots:

public:
bool m_proxyEnable;
// 代理服务器设置,IP:PORT
QString m_proxyServer;
QSettings *reg;

};

#endif // SETHTTPPROXY_H
setHttpProxy.cpp

#include "sethttpproxy.h"

setHttpProxy::setHttpProxy(QObject *parent)
: QObject(parent)
{
QString temp = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
reg = new QSettings(temp, QSettings::NativeFormat);
}

void setHttpProxy::setIEValue()
{
if( m_proxyEnable )
{
reg->setValue("ProxyEnable", 1);
reg->setValue("ProxyServer", m_proxyServer);
}
else
reg->setValue("ProxyEnable", 0);
}

bool setHttpProxy::checkHttpProxy()
{
if( m_proxyEnable )
{
if( 1 == reg->value("ProxyEnable").toInt() &&
m_proxyServer == reg->value("ProxyServer").toString() )
return true;
else
return false;
}
else
{
if( 0 == reg->value("ProxyEnable").toInt() )
return true;
else
return false;
}
}

void setHttpProxy::close()
{
delete reg;
}