How to attach MS word document in Qt GUI application? - c++

I wrote a Qt widget application. In the file menu I want to insert an MS word document as a user manual. Is there any way to do it? I checked Qt help and various blogs but none of them gave me a clear solution.

If it is only manual then it is not necessary to embed MS Word inside your app. Maybe try to open needed document with Word installed in computer. Try this code:
QDesktopServices::openUrl(QUrl("file:///G:/tst.docx"));
Just set needed path. As doc said:
If the URL is a reference to a local file (i.e., the URL scheme is
"file") then it will be opened with a suitable application instead of
a Web browser.

If you want to embed it in your application executable, just insert your .docx file as a resource file. To open the docx file from resources, you should first copy it to some location for example in the application directory path :
QFile HelpFile("qrc:/myFile.docx");;
HelpFile.copy(qApp->applicationDirPath().append("/myFile.docx"));
Next you can open it by :
QDesktopServices::openUrl(QUrl::fromLocalFile(qApp->applicationDirPath().append("/myFile.docx")));

Related

How do I launch an exe that's in the same subfolder as the main one? (C++)

I'm working on a program download manager with options to open the app you've downloaded.
You can download an app and it would be here:
.../programfiles/pub/appmanager/apps/APPHERE.exe
And the main program would be in
.../programfiles/pub/appmanager/MAINFILE.EXE
I need to find a way to start the app. As seen below, i've tried many ways of doing it (system, ect).
(also system() only opens apps in the same directory so i could do something there).
I can do a separate application, but if someone knows how to implement it into a .NET gui, that would be helpful
I tried:
ShellExecute, System, Create Process
(help me on this one im confused)
EDIT:
I need it so that it makes the full directory.
I try something like :
system(app/pxws.exe)
and it wont work
and i tried to merge strings with getmaindirectory and it says not found
To get a new path that is relative to the EXE of the current process, you can do the following:
Retrieve the calling process's full EXE path via GetModuleFileName() or .NET equivilent.
Strip off the filename portion (MAINFILE.EXE) using System.IO.Path.GetDirectoryName().
Append the desired relative path segment (apps/APPHERE.exe) using System.IO.Path.Combine().
Then you can use the new path with whatever API you need.

Check, if a PDF-Reader is installed - QDesktopServices::openUrl()

i want to check, if a pdf-reader is installed. The idea was to use QDesktopServices::openUrl("path/test.pdf") and if its return "false" i know that no pdf-reader is installed. The problem is, that if a pdf-reader is installed, it opens the pdf. Can I "disable" that?
/edit: My solution:
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.pdf\\OpenWithProgids", QSettings::NativeFormat);
if (settings.allKeys().size() == 0) {...}
Can I "disable" that?
Simple answer - no.
As QDesktopServices::openUrl asks the system to open the file with the associated program, you can't disable it via Qt.
While not recommended, you could disable this on a per-platform basis, but if you're going down that route, I suggest using each platform's own features to check if there is an application associated with the pdf data file.
On Windows, it's in stored in the registry, while OS X uses LaunchServices.
However, just because a file association doesn't exist, it doesn't mean that a suitable application isn't installed, for opening a pdf.
If you want to be able to display a pdf, you're probably better off handling that directly in your program. You can read about some options for that here.

ShellExecute in MFC does not open Adobe XI

note: I already looked at the following question but it provided no insight: PDF file does not get open from ShellExecute function in Visual Studio, C++
I have the same problem as the poster in the above question.
Trying to open a PDF file from an MFC Application using ShellExecute. Adobe XI on Win 8 64 bit system.
hReturn = ::ShellExecuteA(NULL,"open",sPath,NULL,NULL,SW_SHOWMAXIMIZED);
sPath is a CString although I've tried alternatives with the same result. I've also tried SW_SHOW, SW_SHOWNORMAL. ShellExecute returns 42 when I cast the HINSTANCE to an int. I understand that means it opens successfully.
In fact, if I watch the task manager when executing the command, Adobe Reader opens in response in Task Manger but the Adobe window never opens.
When I exit my application, Adobe is still open.
If I try to open the target PDF file, the O/S says it can't be opened because it is already open in another application. When I "End Task" on the Adobe Reader in task manager, it frees up the file.
If I then click on the file, it opens fine in Adobe Reader.
So, I'm inferring from this that the ShellExecute is launching Adobe, that it is getting the right file, and that the file is, indeed, readable.
I've also confirmed that ShellExecute is working by directly referencing a txt file. It opens fine in Notepad.
hReturn = ::ShellExecuteA(NULL,"open","d:\\develop\\readme.txt", NULL, NULL, SW_SHOWMAXIMIZED);
Any ideas?
Thanks
try disable "Protected mode"
Adobe Reader - Protected Mode
This seems to be a known problem with opening a PDF file on Win8 via ShellExecuteEx. I had the same issue with an installer that would try to open a PDF at the end of the install process. On Win7 it worked fine. On Win8, it displayed the symptoms you describe. I was able to track it down to a problem with elevated privileges on our install program. I'm not sure why that would adversely affect things, but, it did. As an alternative, we looked at opening the file as an HTML document instead. We are still looking for a better solution.

Can i Write to Embedded Resource Text File?

im stucking with Writing to Text File in the Resource, i can read from
the specified file, but cant write, its says Stream is not writeable.
there is any way to fix it ? i do it for my program builder, thanks in advice!
You can not write the the application bundle, pick another location, perhaps the Documents directory on iOS or the Application Support directory in OSX.
On iOS the resource bundle is also signed, another reason writing to it is not allowed.

How can I read the source URL of a file downloaded with FireFox from an external application?

I have an C++ app I built which is registered as the default handler for a file with a specific extension. So when I download one of these files with Firefox from a website, it downloads it to a temp directory and then shell executes my app while passing the full path to the downloaded file on the command line.
What is the best way to figure out from the external app what the original download url of the file was, given only it's path on disk? Can I use XPCOM API calls to inspect the FireFox download manager database?
I've figured out that this data get's stored in the "%APPData%\Mozilla\Firefox\($profile)\downloads.sqlite" file which is a SqlLite db file, but I really rather not try to open this file directly as FireFox has an open write handle to the file while running.
After perusing the Mozilla developer center for a while, I ran accross the nsIDownloadManager service, which seems to be just the thing. But I can't seem to get access to it from XPCOM in a separate process?
Here's the code I am using:
nsresult rv;
//init XPCOM
nsCOMPtr<nsIServiceManager> servMgr;
rv = NS_InitXPCOM2(getter_AddRefs(servMgr), nsnull, nsnull);
NS_ENSURE_SUCCESS(rv, rv);
//Get a download manager instance
nsCOMPtr<nsIDownloadManager> downloadMgr;
rv = servMgr->GetServiceByContractID(NS_DOWNLOADMANAGER_CONTRACTID,
nsIDownloadManager::GetIID(), getter_AddRefs(downloadMgr));
NS_ENSURE_SUCCESS(rv, rv);
When I run this, the GetServiceByContractID() call returns 0x8007000e, which is defined in nsError.h as NS_ERROR_OUT_OF_MEMORY. (which I find very weird).
Any ideas here? Am I barking up the right tree?
No, you can't access Firefox's XPCOM objects from an external process, and you also shouldn't open the sqlite database while Firefox has it open. I don't know that there's any straightforward way to do what you want without writing a Firefox extension that has access to the Firefox internals.
I'm a little hazy on the details right now, but, assuming that your download is served with a custom MIME type, it's possible to register a handler for that type; your handler can then cancel the download and pass the URL to your application.