QT 5.4, Unable to access Resource from code - c++

I try to include style images of my app into a q-resource file.
When I include the file directly in the code, it work, but when i try to use QResource, it fail (do not load the file).
I have the resource file in the main directory:
AppFolder
|- main.cpp
|- darkstyle.qrc
|- darkstyle
|- WindowTitleBar.png
The following example print:
failed1
failed2
#include <QApplication>
#include <QResource>
#include <Qfile>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
bool ok= QResource::registerResource("darkstyle.qrc");
if (!ok) qDebug()<<"failed1";
QFile file(":/darkstyle/WindowTitleBar.png");
//QFile file("../AppFolder/darkstyle/WindowTitleBar.png"); //that work
if(!file.open(QFile::ReadOnly | QFile::Text)) qDebug()<<"failed2";
else file.close();
//return a.exec();
return 0;
}
Note: Qt creator by default create binaries (.exe) in a top folder: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug/debug/AppFolder.exe
The execution root path seem to be: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug
I tried most of possible combinations with execution paths.
Note2: Some examples use a .rcc file format, I have none of these, but that could be a clue.
Summary:
How to access a QResource file from inside a QT app?
EDIT 1: Content of qrc file:
<RCC>
<qresource prefix="/">
<file>darkstyle/WindowTitleBar.png</file>
<file>darkstyle/WindowTitleButton.png</file>
<file>darkstyle/WindowTitleButton1.png</file>
<file>darkstyle/WindowTitleButton2.png</file>
<file>darkstyle/WindowTitleButton3.png</file>
</qresource>
</RCC>

QResource::registerResource("darkstyle.qrc") registers the resource description. If you want to use resources dynamically like this you need to register the compiled resources themselves. Run rcc -binary darkstyle.qrc -o darkstyle.rcc and use QResource::registerResource("darkstyle.rcc")
Alternatively, compile the resources into your binary directly. Do do so, use RESOURCES += darkstyle.qrc in your .qrc, and leave out the QResource::registerResource.

The problem is related to an incompatibility of the given version of QT with MSVS2013.
The problem is solved by downloading another version of QT or visual studio.

Related

Reading file from working directory QtWebEngine

Hello I am trying to set the QWebEngine URL to an index.html file that is placed in the working directory.
I am trying to use the file by setting the URL to ./index.html but it cant seem to find the file.
Here is where my files are placed
content (Work directory)
main.cpp
content.pro
index.html
How can i open index.html through the QWebEngine without using the full system path?
here is my code
#include <QApplication>
#include <QWebEngineView>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWebEngineView view;
view.setUrl(QUrl(QStringLiteral("file:///./index.html")));
view.resize(1024, 750);
view.show();
return app.exec();
}
Try moving the html file to your project build directory (you're currently keeping it inside the source directory). Then you can build your URL this way:
QUrl url = QUrl::fromLocalFile(QDir::currentPath() + "/index.html");
and set it to the view:
QWebEngineView view;
view.setUrl(url);
view.resize(1024, 750);
view.show();
From http://doc.qt.io/qt-5/qurl.html
qDebug() << QUrl("main.qml").isRelative(); // true: no scheme
qDebug() << QUrl("qml/main.qml").isRelative(); // true: no scheme
qDebug() << QUrl("file:main.qml").isRelative(); // false: has "file" scheme
qDebug() << QUrl("file:qml/main.qml").isRelative(); // false: has "file" scheme
Try: view.setUrl(QUrl(QStringLiteral("index.html")));
As p-a-o-l-o pointed out in his answer, you're likely building out-of-source, so your index.html file has to be in the folder where content.exe is created, not in the source folder.
To make this less complicated, and safer, Qt supports embedding files in the .exe via Qt Resource files (.qrc). These can easily be created in Qt Creator, and once added to the project, the embedded files are accessed via a qrc:/// prefix.
So in your sample code, after adding a .qrc file to your project and adding index.html to it, you would adjust your code like this:
view.setUrl(QUrl(QStringLiteral("qrc:///index.html")));
This has the advantage of working regardless of build type or location, and it's a lot simpler than trying to add a file copy step to your project file (or to manually copy the file each time)

Image is not shown in QGraphicsScene

Description
I wrote following Qt code. And Make executable file for Windows (*.exe). But Image (JPG) is not shown in QGraphicsScene. And I had already checked whether the path to image is correct. In following code, MyQGraphicsScene is derived class inherits QGraphicsScene class.
I compiled same code in macOS. Then, Executable file (ELF) for macOS ran correctly. Image file was shown in its component. I feel starange that source code is same. But the result is different by environment.
Development environment
Windows 10
Qt version : 5.7.0
C++ Compiler : Microsoft Visual C++ Ver.14.0 (MSVC2015)
Source code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsRectItem>
#include <QMessageBox>
#include <QIcon>
#include "TrainingData.h"
#include "trainingDataMaker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QGraphicsItem *curGItem;
QListWidgetItem *listItem;
ui->setupUi(this);
scene = new MyQGraphicsScene(QRectF(0, 0, 400, 400));
ui->graphicsView->setScene(scene);
listItem = new QListWidgetItem();
listItem->setIcon(QIcon("data/000001.jpg"));
ui->imgListWidget->addItem(listItem);
listItem = new QListWidgetItem();
listItem->setIcon(QIcon("data/000276.jpg"));
ui->imgListWidget->addItem(listItem);
ui->imgListWidget->setIconSize(QSize(300,100));
QPixmap pixmap;
QImage img("data/000001.jpg");
QTransform transForm;
pixmap = QPixmap::fromImage(img);
QGraphicsPixmapItem *imgPixmap = new QGraphicsPixmapItem(pixmap);
transForm = QTransform();
transForm.scale(0.1, 0.1);
imgPixmap->setTransform(transForm);
scene->addItem(imgPixmap);
//connect(scene, SIGNAL(changed(const QList<QRectF> &)), imgPixmap, SLOT(chgSize(const QList<QRectF> &)));
}
I compiled same code in macOS. Then, Executable file (ELF) for macOS
ran correctly. Image file was shown in its component. I feel starange
that source code is same. But the result is different by environment.
Since it works under MacOS/X but not under Windows, one likely explanation is that it's a distribution/environmental problem. In particular, perhaps your program can't find Qt's imageformat plugin (e.g. qjpeg4.dll or qjpeg5.dll) under Windows, and that is why the image is not being displayed.
To test that theory, you could copy the qjpeg*.dll plugin file into the same folder as your .exe file, and then re-run your program and see if that makes it behave better. Alternatively, if you don't want to mess with image format plugins, you could convert your .jpg file to another file format that Qt includes native (i.e. non-plugin-based) support for, such as BMP or PNG, and use that instead.
As result, Cause of this issue is my misunderstanding of current directory in case that the application is launched from IDE. Current directory is (a) in following diagram.
Directory structure before
[Output directory is specified in build configuration] - (a)
|- debug - (b)
| |- test.exe // executable file of this application
| |- data
| |-000001.jpg
|- release
Therefore, I should have located "data" directory includes image files in directory (a) as following diagram. I'm feeling strange this specification of IDE. Of course, Current directory is same diretory of exe file in case that application launch directly(Doublu-clicking exe file) .
Directory structure after
[Output directory is specified in build configuration] - (a)
|- debug - (b)
| |- test.exe // executable file of this application
|- release
|- data
|-000001.jpg

Cannot open file in resources directory

I've tested every natural iteration that I can imagine right now but still can not successfully open a file with a QFile object. I've also tested placing the file in the ./resources directory and in the directory with the source file.
I've cleaned my project in between each test and restarted Qt Creator as well. I've also deleted my *.pro.user project configuration file and tested with new versions of that.
#include <QFile>
#include <QDebug>
...
//QFile input_file("./resources/testfile.txt");
//QFile input_file("qrc:/10_graph.txt");
//QFile input_file("testfile.txt");
QFile input_file(":/testfile.txt");
if (!input_file.exists()) {
qDebug() << "File does NOT exist";
exit(11);
}
Qt Creator >> About
Qt Creator 3.1.1 (opensource)
Based on Qt 5.2.1 (Clang 5.0 (Apple), 64 bit)
You should add a resource file to your project and add the testfile.txt to the resources. After that you can access the file from resources by :
QFile input_file(":/testfile.txt");
If you add a prefix to the resources it can be accessed by :
QFile file(":/somePrefix/new.txt");
If you put the file in a folder alongside the source directory and add it to the resources, you can access it by :
QFile file(":/folderName/new.txt");

Background-image in Qt stylesheet doesn't work

This should set the background to cats.jpg, but it doesn't do anything:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setStyleSheet("background-image: url(images/ricepaper.png);");;
w.show();
return a.exec();
}
I thought that it could be a problem with the image location since changing the widget's background-color works fine, but the images folder is in the build-debug directory which I believe is the correct place. I've tried changing the image path, syntax, and styleSheet class and nothing has worked, any suggestions?
From Qt Style Sheets Reference:
It should work. Probably the image file is not to be found where the program expects. If you're using Qt Creator, you should be aware that it builds the target binary in a separate directory (usually with a name like build-yourprojectname-qtversion-Debug or so). That's called shadow build.
You'll need to copy over your image(s) to the proper location relative to that shadow build directory, otherwise the program will not be able to find the file.
Your best bet is to embed the image in the binary as a resource.
I just tried this and seems to be working for me:
In main.cpp:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *w = new QWidget;
w->setStyleSheet("background-image: url(:/resources/pixmaps/close.png);");
w->show();
return app.exec();
}
Note the :/ part in url(:/resources/pixmaps/close.png). That's needed for embedded resources.
In resources.qrc:
<RCC>
<qresource prefix="/">
<file>resources/pixmaps/close.png</file>
... other resource files go here
</qresource>
At the end of testproject.pro:
RESOURCES += \
resources.qrc
Of course you'll need to put the images in the proper location in your project directory so that the resource compiler can find it.
In my example,
resources/
├── pixmaps
│   ├── application.png
│   ├── cancel.png
│   ├── close.png <--Here
...

Does Meego Nokia N950 support C++ QML binding?

I want to communicate between C++ and QML on Meego. It happens using C++ QML binding as described in this link.
When I run an app on the Symbian platform it works, and data from C++ is available to manipulate in some QML file.
But on Meego it always says myItem.qml file not found.
See the following code snippet:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDeclarativeView view;
MyClass myClass;
view.rootContext()->setContextProperty("myObject", &myClass);
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
return app.exec();
}
Does Meego does not support QML C++ binding?
I also tried to give the exact path of the QML file but never found that file, even myItem is in project structure.
Is there some thing associated with path settings in .pro file etc
Please help on this as I am stuck here with this and cannot move forward in my app using Qt Quick.
Yes, but your problem isn't related to C++ and QML bindings. Your application (specifically the view) can't find the .qml file to load.
By default, I think on meego the default path to look in is ../qml. So by default you should be packaging into /opt/YOURNAME with the binary in /opt/YOURNAME/bin and the qml files in /opt/YOURNAME/qml.
However, you can also put the qml files into a resource and use the :/ paths to access a resource instead.
IE, in your resource file:
<RCC>
<qresource prefix="/">
<file>qml/foo.qml</file>
</qresource>
</RCC>
And in C++:
viewer.setSource(QUrl("qrc:/qml/foo.qml"));