PDA

View Full Version : How to read registry entries



Abc
21st August 2008, 07:24
I am calling an application built in C# from my Qt application. In the C# application I have some registry entires made. I want to read these entries in my Qt program and display certain messages depending on the value obtained. Please let me know how to do this.

jpn
21st August 2008, 07:28
See QSettings. Notice especially "Accessing the Windows Registry Directly" in the detailed description.

Abc
21st August 2008, 08:13
I read about QSettings but I am still not able to do it.
Can you please give a little more explanation about doing the same??

santhoshv84
21st August 2008, 08:22
Hi,

Use QSettings like this..

QSettings settings;
//setValue is to set the value to the registry.
settings.setValue("somekey", ui.someButton->isChecked() );

//value is to get the stored value from the registry..
settings.value("somekey", true).toBool();

All the best..

patrik08
21st August 2008, 08:47
search getGSDefaultExeName on file
http://fop-miniscribus.googlecode.com/svn/trunk/QTextPanel/qtextpanelmime.cpp
is part from http://www.qtcentre.org/forum/f-qt-programming-2/t-paint-qtextdocument-splitted-by-pages-15-and-edit-txt-14918.html/?highlight=qtextdocument
a small qtextedit display on paper format like word openoffice..


here a small example to search on regedit on this case it search GhostScript by version....




/* find gpl GhostScript path or exe */
QString getGSDefaultExeName()
{
QString gsName;
QString gVersion;
#if defined Q_WS_WIN
QFileInfo Pinfo;
// Try to locate GhostScript thanks to the qsetting
gsName = "gswin32c.exe";
QSettings softs("HKEY_LOCAL_MACHINE\\Software",QSettings::NativeFormat);
QStringList allsoftware = softs.childGroups();
QStringList gsonly = allsoftware.filter(QRegExp("Ghostscript"));
//////////qDebug() << "### gsonly " << gsonly;
for (int i = 0; i < gsonly.size(); ++i)
{
const QString RealName = gsonly.at(i); /* realpath */
if (RealName.contains("Ghostscript"))
{
//////////qDebug() << "### soft " << RealName;
for (int e=1;e<99;e++)
{
/* check version 8 ++ 99 down */
gVersion = QString("8.%1").arg(100 - e);
if (softs.value(RealName+"/"+gVersion+"/GS_DLL").toString().size() > 6)
{
Pinfo.setFile(softs.value(RealName+"/"+gVersion+"/GS_DLL").toString());
return gsName.prepend(Pinfo.absolutePath()+"/");
}
/* check version 7 ++ 99 down */
gVersion = QString("7.%1").arg(100 - e);
if (softs.value(RealName+"/"+gVersion+"/GS_DLL").toString().size() > 6)
{
Pinfo.setFile(softs.value(RealName+"/"+gVersion+"/GS_DLL").toString());
return gsName.prepend(Pinfo.absolutePath()+"/");
}
}
}
}

/* win not having GPL Ghostscript ! */
gsName = "";
return gsName;
#endif

Abc
21st August 2008, 09:22
I understood about QSettings but still not getting what I intend.

I have a key named 'Status' which can take two values 'success' or 'failed' according to the process and is available in the path 'Software/Burn'. I want to display some message depending on the value obtained. I have done it like...


QSettings settings("HKEY_CURRENT_USER/Software/Burn", QSettings::NativeFormat);
QString value = settings.value("HKEY_CURRENT_USER/Software/Burn/Status").toString();
if(value == "failed") {
// display some message
} else if(value == "success") {
// display some message
}

Please let me know what's wrong in this, why doesn't it work???

jpn
21st August 2008, 09:40
Please follow the example in the docs. Your attempt is radically different. Notice what docs say about slashes and about registry entries appearing under the specified path.

Abc
21st August 2008, 11:31
Still does not work for me...

Did u mean that I should do something like...



QSettings settings("HKEY_CURRENT_USER\\Software\\Burn", QSettings::NativeFormat);
QString value = settings.value("Status").toString();
if(value == "failed") {
// display some message
} else if(value == "success") {
// display some other message
}

Please tell me what's going wrong now.

jpn
21st August 2008, 11:47
Could you export and paste contents of HKEY_CURRENT_USER\\Software\\Burn?

Abc
21st August 2008, 12:01
The contents are as follows...

(Default) REG_SZ (value not set)
Status REG_SZ failed
CommPort REG_DWORD 0x00000003(3)
CommSpeed REG_DWORD 0x00000000(0)
FileName REG_SZ C:/Documnets & Settings/.... (path to the file)

jpn
21st August 2008, 12:15
Is that really an export? As far as I remember it looks a bit different. I wanted to assure that the path is actually correct but I guess you've already done that. :) Anyway, what does


qDebug() << settings.childKeys();
qDebug() << settings.childGroups();

output?

Abc
21st August 2008, 13:23
These two commands don't return any value...
Now, how to proceed.

jpn
21st August 2008, 14:24
So are you 100% sure that the registry path is correct? Does this assert?


QSettings settings("HKEY_CURRENT_USER", QSettings::NativeFormat);
Q_ASSERT_X(settings.childGroups().contains("Software"), "Software", "Software doesn't exist");
settings.beginGroup("Software");

Q_ASSERT_X(settings.childGroups().contains("Burn"), "Burn", "Burn doesn't exist");
settings.beginGroup("Burn");

Q_ASSERT_X(!settings.allKeys().isEmpty(), "Keys", "Software/Burn is empty");
qDebug() << settings.allKeys();

Abc
21st August 2008, 15:20
If I include this snippet, I don't get any output.
Infact, qDebug does not work for me I guess.

Just don't know how to get this working...

jpn
21st August 2008, 17:11
You could have said that earlier when I asked for the output of childKeys() and childGroups(). :) In case you're using an IDE of some kind, check its output console. In case you're not, enable console by adding

CONFIG += debug console
to the .pro file, re-run qmake, make clean and recompile the application. Now you should see some output in the console window.

Abc
22nd August 2008, 08:04
If I include this in the .pro file, the application does not build. :confused:

I am writing Qt code in notepad++ and using Qt 4.4.0 Command Prompt to build my application.

I did this... added the given line to .pro file, did a make clean and then qmake followed by a make.
The error message reads like...
C:\MinGW\bin\..\lib\gcc\ming32\3.4.5\..\..\..\..\m ingw32\bin\ld.exe: cannot find -lQtGuid4
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\myApplication.exe] Error 1
mingw32-make[1]: Leaving directory '<path to the directory>'
mingw32-make: *** [debug] Error 2

jacek
22nd August 2008, 09:53
You don't have Qt libraries built in debug mode. There should be a "Build Qt in debug mode" or similar link in start menu.