Error trying to load qml from resource - c++

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!

Related

A Qt spreadsheet app shows stored files like unknown ones

Here is the program and below is the screenshot of the issue.
I using Qt Installer Framework (QtIFW2.0.1) created an installer for that Qt app (the spreadsheet). Then installed it on my PC. Now when I create a New File and then save it on the Desktop (Windows 7 x64), the app handles the file but its shape is like unknown files!
Here are some samples:
I searched on the web and did these:
I edited the package.xml file and changed all #component.addOperation to #component.addElevatedOperation in the installscript file.
There were three operations all for creating shortcuts.
Now I seemingly should add this line of code into the code:
addElevatedOperation("GlobalConfig", "SystemScope", "Company", "App", "key", "value")
My question is, is it sufficient to solve the issue? And if so, where in the code, what .h or .cpp file to use it into, please?
If any other file is needed I will pleasantly upload it.

Deploying TLS cert with QT qrc

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!

Trouble getting a Qt Reference Document program to work(Minehunt)... its just blank when run... no errors though?

http://docs.huihoo.com/qt/4.7/demos-declarative-minehunt.html
When I build and run... I just get a blank white rectangle... game is MineHunt.
The source code is in the URL. I created files and copied and pasted into Qt Creator 4.7.
Running in Linux.
No need to downvote. Deploying qml apps does not seem to be easy for beginners. jdl, I don't know if I can help you. Currently I am only on Linux. But what I did:
I copied the whole folder minehunt in my destination folder (/tmp/mine). When I ran qmake I got:
WARNING: Include file
/tmp/mine/helper/qmlapplicationviewer/qmlapplicationviewer.pri not
found
So I also copied the examples/declarative/helper folder into /tmp/mine.
Looked like this:
/tmp
/mine
/minehunt
/helper
Then I changed into minhunt, did 'qmake' and 'make'. A few seconds later I had a working minehunt binary in /tmp/mine/minehunt/.
I'll try tomorrow the Windows build.
Edit: Ok, tried now under Windows 7. Qt4.
QtCreator opened.
Project ...qt\demos\declarative\minehunt\minehunt.pro
loaded. Normally in release mode compiled. No problem. A file minehunt.exe was created in ....qt\demos\declarative\minehunt\release. Started: White screen. Exactly as you described it. In ...qt\demos\declarative\minehunt I found a folder 'qml'. I moved this folder and the minehunt.exe into another folder (myMineFolder). Simulating a simple deployment this way. Clicked on minehunt.exe -> worked like a charm.
So my folder layout:
myMineFolder
qml
minehunt.exe
But of course, I have set my PATH variable correctly to my Qt installation.

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

Qt qrc resource file - can't load icon

I have a Qt5 desktop project and I added a "resource.qrc" file with the Qt Creator editor which created the following line into the project's .pro file:
RESOURCES = resource.qrc
I put a blank prefix and a png file (14x14) and I tried to use it like this:
QPixmap pixmap = QPixmap ("://my_image.png");
ui->combobox->addItem(QIcon(pixmap), "itemname");
The problem is: the icon won't show up!
The following works:
QPixmap pixmap(14,14);
pixmap.fill(QColor("red"));
ui->combobox->addItem(QIcon(pixmap), "itemname");
so the problem must be in the resource embedding process.. I noticed that the generated "exe" hasn't a resource section inside it... I don't have static linked external libraries, so I don't think I need the Q_INIT_RESOURCE(resource) macro (it gives me undefined external)
Update:
I'm posting here my qrc file:
<RCC>
<qresource prefix="/">
<file>my_image.png</file>
</qresource>
</RCC>
it's pretty simple and I don't understand why at runtime icons are not showing up
I had this same issue recently, where I malformed the resource string. If you are using a current version of Qt Creator, you can open your .qrc file for edit and then right-click the resource (in this case image) that you are trying to address, then click "Copy Resource Path to Clipboard". And voila, you have the correct resource string every time.
Qt Creator is awesome.
Hope this helps!
#Nikos C. gives you useful advice, but I think your main problem was that you didn't use the correct link to the resource.
In your code, you have:
QPixmap pixmap = QPixmap ("://my_image.png");
but, according to the documentation, it should be
QPixmap pixmap = QPixmap (":/my_image.png");
or you can give aliases to your resources, and use those instead.
Issue is solved is use rcc.exe
C:\root\QT>c:\root\QT\4.7.4\bin\rcc.exe Headless.qrc -o qtresources.cpp
During compilation you should have images in the path.
Create the qtresources.cpp file include this file in makefile or project. You should able to see the image.