Qt 读取注册表

时间:2022-09-05 09:14:57
QSettings *reg = new QSettings("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM",QSettings::NativeFormat);
    QStringList comm = reg->childKeys();//childKeys
    foreach(QString key,comm){
        qDebug() << key;
        qDebug() << reg->value(key).toString();
    }


为什么读的是空

8 个解决方案

#1


"/Device/Serial0" 
"" 
"/Device/VSerial_0" 
"" 
"/Device/VSerial_1" 
"" 
上面是输出

#2


把key中的/替换为“\”,然后再读取, key.replace("/", "\\")

#3


http://m.blog.sina.com.cn/s/blog_5f0d72800100iiw8.html#page=2

#4



QSettings *reg = new QSettings("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM",QSettings::NativeFormat);
    QStringList comm = reg->allKeys();// >childKeys();//childKeys
    foreach(QString key,comm){

        key.replace("/","\\");
        //"\Device\Serial0"
        qDebug() << key;
        QString ans =  reg->value(key).toString();
        qDebug() << ans;
    }

"\Device\Serial0" 
"" 
"\Device\VSerial_0" 
"" 
"\Device\VSerial_1" 
"" 
还读的还是空呀

#5


那就证明你没有写进去,你写注册表制后,去注册表里面看看到底是否写成功了。

#6


我找到原因了,QSetings对Key值含有\的就是读不出来,我随便添加几个不带杠的值,马上就读出来了。 只能用API读了,希望大家不要走弯路搞这个了 Qt 读取注册表

#7


引用 6 楼 gucunlin 的回复:
我找到原因了,QSetings对Key值含有\的就是读不出来,我随便添加几个不带杠的值,马上就读出来了。 只能用API读了,希望大家不要走弯路搞这个了 Qt 读取注册表


QSettings 有这么一句话:

Note that the backslash character is, as mentioned, used by QSettings to separate subkeys. As a result, you cannot read or write windows registry entries that contain slashes or backslashes; you should use a native windows API if you need to do so.

#8


引用 7 楼 dbzhang800 的回复:
Quote: 引用 6 楼 gucunlin 的回复:

我找到原因了,QSetings对Key值含有\的就是读不出来,我随便添加几个不带杠的值,马上就读出来了。 只能用API读了,希望大家不要走弯路搞这个了 Qt 读取注册表


QSettings 有这么一句话:

Note that the backslash character is, as mentioned, used by QSettings to separate subkeys. As a result, you cannot read or write windows registry entries that contain slashes or backslashes; you should use a native windows API if you need to do so.

知道读不出来为什么不改进呢。

#1


"/Device/Serial0" 
"" 
"/Device/VSerial_0" 
"" 
"/Device/VSerial_1" 
"" 
上面是输出

#2


把key中的/替换为“\”,然后再读取, key.replace("/", "\\")

#3


http://m.blog.sina.com.cn/s/blog_5f0d72800100iiw8.html#page=2

#4



QSettings *reg = new QSettings("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM",QSettings::NativeFormat);
    QStringList comm = reg->allKeys();// >childKeys();//childKeys
    foreach(QString key,comm){

        key.replace("/","\\");
        //"\Device\Serial0"
        qDebug() << key;
        QString ans =  reg->value(key).toString();
        qDebug() << ans;
    }

"\Device\Serial0" 
"" 
"\Device\VSerial_0" 
"" 
"\Device\VSerial_1" 
"" 
还读的还是空呀

#5


那就证明你没有写进去,你写注册表制后,去注册表里面看看到底是否写成功了。

#6


我找到原因了,QSetings对Key值含有\的就是读不出来,我随便添加几个不带杠的值,马上就读出来了。 只能用API读了,希望大家不要走弯路搞这个了 Qt 读取注册表

#7


引用 6 楼 gucunlin 的回复:
我找到原因了,QSetings对Key值含有\的就是读不出来,我随便添加几个不带杠的值,马上就读出来了。 只能用API读了,希望大家不要走弯路搞这个了 Qt 读取注册表


QSettings 有这么一句话:

Note that the backslash character is, as mentioned, used by QSettings to separate subkeys. As a result, you cannot read or write windows registry entries that contain slashes or backslashes; you should use a native windows API if you need to do so.

#8


引用 7 楼 dbzhang800 的回复:
Quote: 引用 6 楼 gucunlin 的回复:

我找到原因了,QSetings对Key值含有\的就是读不出来,我随便添加几个不带杠的值,马上就读出来了。 只能用API读了,希望大家不要走弯路搞这个了 Qt 读取注册表


QSettings 有这么一句话:

Note that the backslash character is, as mentioned, used by QSettings to separate subkeys. As a result, you cannot read or write windows registry entries that contain slashes or backslashes; you should use a native windows API if you need to do so.

知道读不出来为什么不改进呢。