Adding pdf as a resource in .qrc - c++

I need to add a pdf file to my resource folder, which I can open from the application with the click of a button (kind of like a Help file).
So my confusions are:
How exactly do I add the pdf to my resources?
What path should I use to direct to the pdf?
QDesktopServices::openUrl(QUrl("/Resource Files/Help.pdf"));

To add the resource file into your project, right click on the project in the Projects pane and click Add New... and then Qt>Qt Resource file, enter a name for it and after finishing something is added to your .pro file like :
RESOURCES = myResource.qrc
Once added into the pro file, a separate .qrc file will be added into your Qt project with which you can double click and add your desired resources like icons, translation files etc. Open the resource file and add your pdf file there.
To open the pdf file from resources, you should first copy it to some location for example in the application directory path :
QFile HelpFile("qrc:/myFile.pdf");;
HelpFile.copy(qApp->applicationDirPath().append("/myFile.pdf"));
Next you can open the pdf file by :
QDesktopServices::openUrl(QUrl::fromLocalFile(qApp->applicationDirPath().append("/myFile.pdf")));

Related

How to copy a .ui file in my qwidgets project?

I wish to try this qt example. I created a qwidgets project, copied all headers and sources from the following example to my project.
http://doc.qt.io/archives/qt-5.5/qtmultimedia-audiorecorder-example.html
What is the way to copy the .ui file of that project to my project?
What should be the extention of that file in my project?
The *.ui files are GUI designer files and define the actual GUI.
Copy it as-is. You'll need to invoke the uic (User Interface Compiler) on it to produce the ui_audiorecorder.h header file, which is included in the audiorecorder.cpp file.
Read more on the uic toolchain in the docs: Using a Designer UI File in Your Application.
You copy the .ui file into your project's folder using your favorite file manager or terminal or what have you. Suppose that the file is named foo.ui.
Then you add the file to the project by either:
Adding the following line to the .pro file:
FORMS += foo.ui
Right-clicking the root project node, selecting "Add Existing Files...", and navigating to the foo.ui file in the file dialog.
The two procedures are equivalent, the result is the same: a new FORMS item in the project file.
That's all. As soon as you save the .pro file, Qt Creator will parse it and show the foo.ui under a Forms project tree node.

VS2010 failing to locate source file

My project has following structure:
Someproject
include
workspace
bin
projectspace
source
I am using MFC for dialog based application. I add a new dialog and add a new class.Header and source files are generated in workspace/projectspace folder. I do exclude from project, move the files to respective location (includes/source) and do Add Existing Items in VS. Now when I add a button click event from dialog, it creates a new source file in includes directory. How to avoid this?
You should not do exclude. Instead, copy the files to the desired location, then remove from the project. Then Add existing from the new location.

How do you add a file to an existing project in Codelite?

I've got my workspace, project and a single file currently. I want to add an additional file to my src folder but can't figure out how.
I tried adding a new file to my project folder using ctrl+N and it shows up with my first file when I go to Edit -> Open file. However, it won't show up in my src folder while I've got my workspace/project open; that part is still only showing the original file I created when I made the project.
Right click on the src folder and select Add an existing file

Cant open html resource file using ifstream::open - Visual Studio 2013

I am working on a project which requires me to open an HTML file and use its contents. I added it to Resource files but when I try to open it lie this:
std::ifstream templateFile;
templateFile.open("filename.html", std::ifstream::in);
The operation fails. I checked it by using templateFile.fail().
The above operation works when I provide the full path. The file lies in the project folder along with other files. I tried setting build action to content but still it doesnt work. Please Help.
Output directory, where your executable is compiled and put into differs from the source directory, where you create all your .cpp/.hpp files (I assume there is filename.html file). Local path filename.html is supposed to be local for your executable file, not the source file.
Read more about changing the output directory here: https://msdn.microsoft.com/en-us/library/ms165410.aspx
Under Configuration Properties / Debugging, see what your Working Directory is using the macros dialog box. Move your file into this folder.
Click the button shown in the figure. There, click either Edit or Browse. Browse will take you to the working directory. Edit will expose the link to open the macros box

Qt: Path to file in QString

I have problem with storing a path to file in Windows in a QString. I'm using Qt with C++.
QString resourcePath = ":/images/frog.bmp";
if( ! QFile::exists(resourcePath) )
{
qDebug("*** Error - Resource path not found : %s", resourcePath.data());
}
This code results with this:
*** Error - Resource path not found : :
So I can see that resourcePath.data()) contains just ":". I assumed that the problem is with slashes, so I tried changing "/" with "\" but the result is the same.
But if I write:
QString resourcePath = "C:\\Users\\Boris\\Desktop\\Frogger3\\images\\frog.bmp";
everything works just fine. What am I missing? Is there a reason why colon cant be the first sign in QString? How should I write path to the file in the same folder as the code?
Thanks in advance!
The style of resource path you are using is implying that the file frog.bmp is in a resource file. So either you need to resolve the path of the bmp file at run-time, or you need to add a resource file to your project.
If you use the UI designer the concept of resource files is handled automatically, but if you want to access resources through code there are a few things you need to do.
First create a resource file. In visual studios (with the visual studios add-in) there is a wizard to do this. Essentially it is just an xml file with the extension .qrc looking something like this:
<RCC>
<qresource prefix="/images">
<file>frogger.bmp</file>
</qresource>
</RCC>
Now this file has to be processed during the build. If you have used .ui files, it is similar. There is a tool called "rcc.exe" that takes the qrc file as an input and generates a .cpp file that needs to be compiled and linked with your project.
If you are using visual studios and have the Qt Visual Studios Plugin, this should all be handled for you when you add the qrc file to the project.
If you are using QMake then your pri file should contain a "RESOURCES" section where you need to list your qrc file something like:
RESOURCES += yourqrcfile.qrc
Now, once that is done. You can access your resources in code. Your call to QFile::exists should resolve the file name.
In the case where you put your resources in a static or shared library, you will need to add the following line to your class to ensure that the resource file is loaded.
Q_INIT_RESOURCE(yourqrcfile); // do not include the extension, just the name of the file.
Here are a few links that explain things in more detail:
Creating a resource file in Qt Creator
Explaining how resource files work