[Solved] Reading windows registry values using QSettings
I'm using QML and C++ for a small application with a custom interface. This is the first time I've used QML and so far I'm really pleased with it (learned a lot, too). Unfortunately, I stumbled upon a problem that I haven't been able to solve even after an entire day of searching so I decided it would be best to try here.
One of the things my application is supposed to do is to open an Excel (.xls) file in MS Excel (or equivalent program on other platforms). My approach was to write a small class in C++ which would do this using QProcess. Since this is platform specific code, I'll have to rewrite the class for each platform, but since it's pretty small, it doesn't seem like a big problem. I tested opening Excel in a new QProcess by passing the path to Excel.exe and it works fine and it also works for Adobe Reader (another thing I'm supposed to do). Obviously though, this is not a good solution because it assumes where Excel is installed and I cannot guarantee that.
The solution I wanted to use was to find the location of Excel.exe in Windows registry. Despite my best attempts, it just doesn't work. I've tried so many different combinations but I just can't get the path. To make things really simple, I'm providing a small piece of code showing what I'm trying to get.
int main(int argc, char *argv)
QCoreApplication app(argc, argv);
QSettings excelSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Office\\14.0\\Excel\\InstallRoot", QSettings::NativeFormat); QString excelLocation = excelSettings.value("Path").toString(); return app.exec();
If you open RegEdit you can try to find this, though you may need to change 14 (2010) to whichever version of Office you have. "Path" contains the path to where Office is located and that's exactly what I need but the above code just doesn't work. I've also tried removing parts of the above registry path and using QSettings::allKeys(), childKeys() and childGroups() to see what I'd get and I either got only some values or none at all. This would imply that QSettings is doing something wrong, but I'm more inclined to think that I just don't understand what those functions are supposed to do.
Here's the funny thing: since I don't know which version of Office the user will have, I also used the registry to find out, and this works fine using "HKEY_CLASSES_ROOT\Excel.Application\CurVer" and "Default" for value. I can also get the path of Adobe Acrobat 9 Pro (currently using that instead of the Reader) using "HKEY_CLASSES_ROOT\acrobat\DefaultIcon" and "Default" for value.
I have some backup solutions, but I'd rather not resort to them if this can be solved the "right" way. So please, if someone can tell me what I'm doing wrong, I'll be very thankful.
NOTE: I don't need to write anything to the registry, just read from it.
OS: Win7 Pro x64
Qt Framework: 5.0.2 with MinGW compiler
P.S. Sorry for the long post, but I figured it would be easier if I explained what I need to do and why.
Maybe you are experiencing 32/64 bit problems: If your program is compiled 64bit you may have problems to access to 32bit registry and vice versa.
I haven't tried to use QProcess on this but on Windows it would be common practice to execute the Excel file and let Windows do the job.
thanks for your reply. I just tried using Qt.openUrlExternally() directly in QML and it looks like it's working. I'll have to test this on other platforms, but I'm marking this as solved. Thank you for your idea, it's much cleaner than what I had in mind. Hopefully, I'll have a prototype of the application soon to show to my boss, but I got much further than I expected. I can't express how delighted I am with Qt and QML, it looks like my hunch was right and they are exactly the right tool for the job.
P.S. I'm not sure, but I think the program was compiled 32bit. The task manager marks it as such, but I don't know if that's where I should be looking. The MinGW compiler is 32bit if that helps.
EDIT: I do have a further question: is it possible to give a relative path to a local file when using Qt.openUrlExternally()? From what little I've googled, it seems it isn't, but maybe I'm doing something wrong again. If it isn't, is there a way to get the path to the folder where the application is located?
You mean something like
@QString QDir::currentPath ()@
Hmm, I was hoping it was possible to do this directly in QML, but if not, I'll just use that.