Qt QSettings读取注册表和ini

1.读取注册表,以读取已经安装的软件为例。

已经安装的软件信息的注册表位置:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

void Widget::initSoftWareList()
{
    softWareCount=0;
    softWareList=new QListWidget;
    softWareList->setFrameShape(QFrame::NoFrame);
    QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",QSettings::NativeFormat);
    QStringList groupsList=reg.childGroups();
    foreach(QString group,groupsList)
    {
        reg.beginGroup(group);
        QString softWareName=reg.value("DisplayName").toString();
        if(!softWareName.isEmpty())
        {
            softWareList->addItem(new QListWidgetItem(softWareName));
            softWareCount++;
        }
        reg.endGroup();
    }
}

主要是要分清childGroups和childKeys。

Qt QSettings读取注册表和ini_第1张图片

Qt QSettings读取注册表和ini_第2张图片

 

再看看读取开机自启动信息:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Qt QSettings读取注册表和ini_第3张图片

void Widget::initRunOnStart()
{
    runOnStartCount=0;
    runOnStartTree=new QTreeWidget;
    runOnStartTree->setColumnCount(2);
    runOnStartTree->setHeaderLabels(QStringList()<<tr("名称")<<tr("位置"));
    runOnStartTree->setFrameShape(QFrame::NoFrame);
    QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);
    QStringList keyList=reg.childKeys();
    foreach(QString key,keyList)
    {
        QString location=reg.value(key).toString();
        if(!location.isEmpty())
        {
            runOnStartTree->addTopLevelItem(new QTreeWidgetItem(QStringList()<<key<<location));
            runOnStartCount++;
        }
    }
}

 

2.读取ini文件

Qt QSettings读取注册表和ini_第4张图片

void Widget::initIniText()
{
    QString text;
    QSettings iniTest("C:\\Users\\Administrator\\Desktop\\test.ini",QSettings::IniFormat);
    QStringList groupList=iniTest.childGroups();
    foreach(QString group,groupList)
    {
        iniTest.beginGroup(group);
        QStringList keyList=iniTest.childKeys();
        foreach(QString key,keyList)
        {
            text+=key+" "+tr(iniTest.value(key).toString().toStdString().c_str());
        }
        iniTest.endGroup();
    }
    iniText=new QTextEdit(text);
    iniText->setFrameShape(QFrame::NoFrame);
    iniText->setReadOnly(true);
}

 

你可能感兴趣的:(c,windows,Microsoft,ini,qt)