Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values



  • Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values,but other type keys can read well, eg. REG_SZ.
    code as below:

    void MainWindow::on_pushButton_clicked()
    {
    ui->listWidget->clear();
    QString str = ui->lineEdit->text();
    QSettings reg(str, QSettings::Registry64Format);
    QStringList keyList=reg.childKeys();
    foreach(QString key, keyList)
    {
    QString keyvalue=reg.value(key).toString();
    ui->listWidget->addItem(key + " = " + keyvalue);
    }
    }
    output as below:
    0_1499413859266_regedit.png
    0_1499413886062_myProgram.png


  • Moderators

    @Frankkon said in Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values:

    reg.value(key).toString()

    Did you try to print out reg.value(key)

    qDebug() << reg.value(key);
    

    You're converting it to string, but actually it is an integer number. You should convert to string like this:

    QString::number(reg.value(key).toInt());
    


  • @jsulm Thank you very much. I do as you said, but it still not work well.
    my debug code as below:
    qDebug() << key << " = " << reg.value(key);
    qDebug() << key << " = " << reg.value(key).toInt();
    qDebug() << key << " = " << QString::number(reg.value(key).toInt());
    the debug output as below:
    0_1499501461174_debug.PNG

    but regedit tool shows as below:
    0_1499501507610_regedit.PNG

    can you help me? thank you.



  • Hi, unfortunately when registry keys contain slashes or backslashes QSettings does not work :-(
    But you can still use old the old Win32 API, like this:

    Add
    LIBS += -ladvapi32
    to your .pro file.

    Add #include "windows.h" to your .cpp file.

    And try something like this:

    QSettings reg("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\SharedDLLs\\",QSettings::NativeFormat);
    
    for (QString key : reg.childKeys())
    {
        key = key.replace("/","\\");    // replace back to backslashes to keep Windows happy
        DWORD dw = 0,sdw = sizeof(dw);  // dword value comes to dw
        ::RegGetValue(HKEY_LOCAL_MACHINE,L"Software\\Microsoft\\Windows\\CurrentVersion\\SharedDLLs",(LPCWSTR) key.constData(),RRF_RT_DWORD,NULL,&dw,&sdw);
    
        qDebug() << key << dw;
    }
    


  • @hskoglund Thank you! Yes, the problom is “\” , I create a key like "aaa/bbb/ccc", QSettings can work well on it. Thanks a lot.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.