Deploying TLS cert with QT qrc - c++

I'm trying to include a SSL cert in the app deployment so we can connect to our server securely. I am able to add the files into our asset folder and our resources.qrc file and it works with debug deployment. However when I try to make a release build, it breaks and is unable to find the files.
I've ripped through the QT docs and SO, but everything I've tried has not worked.
Here is the excerpt from the .pro file:
`qmlFolder.source = qml
DEPLOYMENTFOLDERS += qmlFolder # comment for publishing
assetsFolder.source = assets
DEPLOYMENTFOLDERS += assetsFolder
# Add more folders to ship with the application here
QT += xml svg quick qml widgets
# resources.qrc
RESOURCES += resources.qrc # uncomment for publishing`
Here is my resources.qrc file:
<RCC>
<qresource prefix="/">
<file>qml</file>
<file>GandiStandardSSLCA2.crt</file>
<file>SSLcomDVCA_2.crt</file>
</qresource> </RCC>
Here is where I set the path: certPath = "://SSLcomDVCA_2.crt"; // Dev Cert
I got the path from right clicking the file. Again it works in a debug build, but not for release, so I believe it's not being packaged properly. Any hints or ideas how to proceed resolve this issue?

I've found the "issue" and the answer to my problem. Although I think the problem would be a QT bug or my understanding of string definition is wrong. I set the resource file called certs.qrc when I put the two certificates for dev and prod. I created the file definition and used the path I got by right clicking on the resource file and copying it's path. See below for the full definition of the SSL, hopefully this will serve as an example for QT SSL as well because I could barely find any of those either.
Here is my certs file without the 'RCC' header because it seems to break block quotes:
<qresource prefix="/certs">
<file>GandiStandardSSLCA2.crt</file>
<file>SSLcomDVCA_2.crt</file>
</qresource>
Below is the definition for that certificate file:
// SSL socket set up
QFile certFile(":/certs/GandiStandardSSLCA2.crt");
if(certFile.open(QIODevice::ReadOnly))
{
qDebug() << "File opened";
}
else {
qDebug() << "Boooo";
}
QSslCertificate cert(&certFile, QSsl::Pem);
forgotPwSocket = new QSslSocket(this);
forgotPwSocket->addCaCertificate(cert);
All this is currently functional. make sure you run qmake in order to get the resource files compiled.
So now, my explanation as to why I was having a problem. I was using a different deploy method before for the certs where I was deploying the entire assets folder. That works for Android by not iOS so I decided to go with qrc instead. While I was trying to implement the /assets folder deployment I was using an ifdef to define the string of the path depending on if it was iOS or Android. I removed the ifdef, and all my problems went away, it was now able to find the files at the path. The string is a member of my network object and I would define it at run time using the ifdef. During all my debugging, it was the correct path as shown in the inspector. Realizing that using the .qrc resource eliminated the need to define the path based on Android/iOS I hardcoded the path value as stated above. Once I did that, it started working. The path name is the same as it was in the debugger with the ifdef in place so I'm not sure why/where this was breaking. If anyone has any further insight it would be fantastic to hear. Hopefully this will help anyone having a similar issue or getting SSL working in QT in general!

Related

QT5: Unable to set stylesheet properties using qss file

I'm currently working on an app and I'm unable to set global stylesheet. Here's the code I've been trying:
QFile file(":/dark.qss");
file.open(QFile::ReadOnly | QFile::Text);
QTextStream stream(&file);
qApp->setStyleSheet(stream.readAll()); '''
and
QFile f(":/dark.qss");
if (!f.exists()) {
printf("Unable to set stylesheet, file not found\n");
}
else {
f.open(QFile::ReadOnly | QFile::Text);
QTextStream ts(&f);
qApp->setStyleSheet(ts.readAll());
}
For the first one I'm getting :
QIODevice::read \dark.qss device not open
and for the second one
Unable to set stylesheet, file not found
The second one used to work fine 2 weeks ago (build with msys2, mingw64, qt5.14.1.3), unfortunately, I had to reinstall mysys2 and since then this is not working anymore.
I tried to build using msys2 with all the packages I used 2 weeks ago (had to downgrade about 80 packages manually ) also straight from qt creator with no luck.
Any idea what might be the problem?
The issue has nothing to do with msys. The most obvious reason is an incorrect project setup. Please make sure dark.qss is added into the resource (qrc) file under "/" prefix and the qrc file is added into RESOURCES in the pro file.
Also, there is a bug in qmake not adding newly added resources into the app sometimes. A full rebuild might help in this situation.
It would be great if you paste both the qrc and pro file here. A full build log could be very helpful as well.
Maybe your .pro file is missing RESOURCES variable with .qrc file name.
You can also try to execute Clean and Run qmake on your project before building.

QT Resource File - Images Not Showing When Running

I just upgraded to QT Creator 5.3 and created a brand new QT Widgets Application project and am using the Microsoft VC++ compiler. All I have is a resource file with "logo.png" added (which opens in QT if I double click it), and a label that I am trying to set the background image for. The problem is that no matter what I do I cannot get the image to show up when the program is running. The only way I can get it to show the image is by not using the resource file and instead map directly to the file (ex. "c:/blah/blah/logo.png")
Also, if I set the background image in the UI designer, the background shows up in the IDE but NOT when I run the program. I have tried probably 20+ variations of the code, including resource file aliases as well as adding the files to the project directly, and nothing seems to work.
I'm not sure if there is a step I am missing, or if perhaps there is something that I have to do to get the project to compile the image.
Resources.qrc
<RCC>
<qresource prefix="/">
<file>logo.png</file>
</qresource>
</RCC>
loginform.cpp
QPixmap pixmap = QPixmap (":/logo.png");
ui->label->setPixmap( QPixmap(pixmap));
Project.pro
OTHER_FILES += \
logo.png
RESOURCES += \
Resources.qrc
After countless hours of trial, error, and mostly frustration, I have been able to resolve this issue by simply running qmake.
To do this I just right clicked on the project and clicked "Run qmake" and suddenly the image errors (as well as a few others) were resolved!

Error trying to load qml from resource

I did an app loading qml from local filesystem, and it was working fine, but now I'm trying to change it to load from qml, but it never find my file.
I've looked into the web and tried some solutions, nothing worked.
My qml file is in a subdirectory of the project qml/MyProject/main.qml
Then I created a resource file and added a prefix "/" (tried with different names too), and then added my qml file to it.
Then I changed the generated qtquick2applicationviewer.cpp setMainQmlFile to: http://pastebin.com/kRJDJACW
Then I tried the following lines to load the qml from resource:
viewer.setMainQmlFile("qrc:/main.qml");
viewer.setMainQmlFile("qrc://main.qml");
viewer.setMainQmlFile("qrc:/other_prefix/main.qml");
viewer.setMainQmlFile("qrc:/qml/MyProject/main.qml");
viewer.setMainQmlFile("qrc://qml/MyProject/main.qml");
My resource file: http://pastebin.com/1sjUqk2T
Qt version: 5.2/ QtCreator 3.0
What am I doing wong?
The code you are showing looks alright, at least the following line:
viewer.setMainQmlFile("qrc:/main.qml");
The error is likely somewhere else. You have not shared the project file of yours, but the reason is probably that you do not have this file correctly in your resource.
You would need to use the following entry in your qmake file:
RESOURCES = main.qrc
Solved, thanks guys. For some reason my resource file was in "other files", not in "resources" area of Qt Creator, so I added a new one and it worked. Thanks!

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

what dll to load if i want to see images when deploying in runtime, also which vc dll do i realy need?

i first time deployed my app outside of the visual studio compiler on clean pc .
and i have problem . first i dont see any images in my application
i use .png , *.gif,.ico file types i trying to add the :
qgif4.dll
qico4.dll
qjpeg4.dll
qjpeg4.lib
qmng4.dll
qsvg4.dll
the way im using my images is with *.qrc file and i was hoping it compile it as resource ( i really hope .. )
in the pro file i just added it like this:
RESOURCES += resources.qrc
and it looks like this:
<RCC>
<qresource prefix="/">
<file>images/image1.png</file>
<file>images/icon-loading.gif</file>
<file>images/filter.ico</file>
<file>images/icon_lp.png</file>
</qresource>
</RCC>
but with no good i still see blank instead of images .
also do i need one of the vs run times dll’s in my application?
msvcm90.dll
msvcp90.dll
msvcr90.dll
Thanks
The image plugins need to be in the right location for the Qt plugin loader to find them. Try putting them in an 'imageformats' directory next to your executable.
Use depedency walker to figure out which are the libraries linked to your application. This way put your application in a random directory, use the tool and you will see all missing references. these are the one you need to include.
Can you show the line of code where you use an image?