Qt Resource Not Working - c++

I've seen every SE question and have even followed some video tutorials. I have no idea why it isn't working.
I have two QLabels, lblBuyer and lblSeller as well as two pictures. Here is the relevant code:
QPixmap pixmap_buyer(":/Resources/Images/Buyer.png"), pixmap_seller(":/Resources/Images/Seller.png");
size_t lblBuyerWidth = ui->lblBuyer->width(), lblBuyerHeight = ui->lblBuyer->height();
size_t lblSellerWidth = ui->lblSeller->width(), lblSellerHeight = ui->lblSeller->height();
ui->lblBuyer->setPixmap(
pixmap_buyer.scaled(lblBuyerWidth, lblBuyerHeight, Qt::KeepAspectRatio)
);
ui->lblSeller->setPixmap(
pixmap_seller.scaled(lblSellerWidth, lblSellerHeight, Qt::KeepAspectRatio)
);
This is what the resources look like:
And the resources.qrc if you want it:
<RCC>
<qresource prefix="/Resources">
<file>Images/Buyer.png</file>
<file>Images/Seller.png</file>
</qresource>
</RCC>
And the file structure (images are directly in the Images directory):
I selected "Copy as Resource Path" so I was sure not to mess anything up. Also, when I click "Remove Missing Files," neither gets removed... However, when I run it:
QPixmap::scaled: Pixmap is a null pixmap
QPixmap::scaled: Pixmap is a null pixmap

I have seen this a couple of times, and the cure varies:
The first thing to try is clean/rebuild your project.
The second thing to try is to manually clean/rebuild your project (as in delete your build dir by hand in a file manager).
If the problem still persists, you can try putting a resource init macro into main.
In other words, put a line like: Q_INIT_RESOURCE(my_resource); in your main() function just after instantiating your app for each resource in your project, like this:
int main(int argc, char *argv[]){
QApplication app(argc, argv);
Q_INIT_RESOURCE(my_resource);
// ... whatever here ...
return app.exec();}
}
Of course you put the name of your actual resource file in place of my_resource.
This is usually necessary only when I am doing some strange stuff like using my own static Qt or using a wired project structure.

Related

How to set Virtual Keyboard style from .qrc in QtWidgets application with CMake

I have tried for a couple of days to setup Virtual Keyboards in a Linux application. My application contains a .qrc file containing keyboard layout and styles. I tried following tons of forums to no avail, but I believe the closest I came is through following https://forum.qt.io/topic/93279/how-to-custom-qml-virtual-keyboard. Now all links on Google are purple. Many of the guides and forums expect a QML application written in QMake, and I have not succeeded in translating these to my problem using CMake.
I have also asked this question here but haven't gotten answers in days.
My .qrc file is the following (with the default-style taken from here):
<RCC>
<qresource prefix="/">
<file>virtual_keyboard/QtQuick/VirtualKeyboard/layouts/en_GB/main.qml</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/layouts/en_GB/numbers.qml</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/textmode-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/shift-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/shift-c5d6b6.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/shift-80c342.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/settings-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/selectionhandle-bottom.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/search-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/hidekeyboard-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/handwriting-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/globe-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/enter-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/check-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/images/backspace-fff.svg</file>
<file>virtual_keyboard/QtQuick/VirtualKeyboard/Styles/style/style.qml</file>
</qresource>
</RCC>
I know I need the following lines to use my custom layout and specify my style, here just named 'style'. The custom layout is loaded properly, but the style is not:
qputenv("QT_VIRTUALKEYBOARD_LAYOUT_PATH", "qrc:/virtual_keyboard/QtQuick/VirtualKeyboard/layouts");
qputenv("QT_VIRTUALKEYBOARD_STYLE", "style");
QApplication a(argc, argv);
...
return a.exec();
What I get from running my application in the following, which suggests that I am not importing from the right directory for whatever reason:
WARNING: Cannot find style "style" - fallback: "default"
What I've tried
As can be seen in the .qrc-file, I use the same path as they expect, e.g. .../QtQuick/VirtualKeyboard/Styles/_MYSTYLENAME_/..., where _MYSTYLENAME_ = style.
I tried putting qputenv("QML_IMPORT_PATH", "qrc:/virtual_keyboard") and qputenv("QML2_IMPORT_PATH", "qrc:/virtual_keyboard") before the lines above to enforce using styles from the .qrc-file, with no luck.
I've tried adding the following lines (below QApplication a(argc, argv) or else it wouldn't run):
QQmlEngine qml_engine;
qml_engine.addImportPath("qrc:/virtual_keyboard");
Thanks in advance for any help!

Is it possible to add background image on QWidget without using QtCreator?

Having trouble adding background image on the widget, even though I referenced the recent codes online.
This is my current code for main.cpp:
#include <QApplication>
#include <QWidget>
int main (int argc, char **argv){
QApplication app (argc,argv);
QWidget *w=new QWidget();
w->setStyleSheet("background-image: url(:/cover.jpg);");
w->setWindowTitle("Test");
w->show();
return app.exec();
}
After executing the code, how come the widget remains blank? Thanks in advance!
QtCreator is an IDE designed by Qt. It's just an interface.
I checked your implementation and I don't see anything wrong. It also work well on my pc. Can you check your image url or try with another image ?
Btw, if you're on linux, try removing : character after url ;
w->setStyleSheet("background-image: url(/cover.jpg);");
EDİT:
If jpg is in the same directory with your application, it should be ;
w->setStyleSheet("background-image: url(./cover.jpg);");
You can give a full path to avoid this kind of errors.
"Is it possible to add background image on QWidget without using QtCreator?"
Yes, of course it is.
QtCreator is just an IDE. You don't need to use it at all to write code using the Qt library.
Just as you can use it to write code that does not use Qt at all.

QFileSystemWatcher: Modified files in a directory

I am trying to keep track of files in a directory. Not only do I want to get notion of new files being created or existing files being deleted, but I also would like to watch for modifications done to existing files (i.e., when the file content is modified).
QFileSystemWatcher allows for watching directories and files. Given a large number of files in the directory it is not feasible to add every file to the watcher (QFileSystemWatcher::addFile()). In return, however, it would be totally sufficient to hinge the modification of a file on its modification time alone.
Now I've set up a little test like this:
#include <QtCore>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFileSystemWatcher *w = new QFileSystemWatcher;
w->addPath("/tmp/xxx");
QObject::connect(w, &QFileSystemWatcher::directoryChanged,
[]{ qDebug() << "change"; }
);
QFile f("/tmp/xxx/the.file");
f.open(QFile::WriteOnly);
f.write("blabla");
f.setFileTime(QDateTime::fromSecsSinceEpoch(10000), QFile::FileModificationTime);
f.close();
QDialog d;
d.show();
app.exec();
}
Yet whatever I try within this program (writing to the file, setting the file times) no change is ever detected.
But what strikes me is that if I use touch on the file the change is detected. So what on earth does touch do differently?
Obviously I am running Linux.

How to move a Qt stylesheet to an external file but keep it compiled in resources?

My Qt application has a large stylesheet with lots of margins, pixels and other stuff directly based on and related to drawing and graphics. I would be happy to give all that stuff to the designer, but the stylesheet is kept within the .ui file which is not convenient for the designer; she'd prefer to see a separate file and edit it using her usual tools.
What I want is moving the stylesheet to an external .qss file, adding that file to the program resources and linking it to the .ui file, so the stylesheet would be compiled and used by the widget automatically, and the application wouldn't have to keep the stylesheet file and load it at runtime.
How to achieve that?
Copy all your styles into a file and rename it to something like stylesheet.qss
Then include it in your qrc file as a new resource item. You can simply do this by editing your qrc file, if you already have one. Refer documentation on how to add a new resource.
Then modify your code like this to read the content of the qss file at run time and apply styles to your application.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile file(":/stylesheet.qss");
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
a.setStyleSheet(file.readAll());
file.close();
}
MainWindow w;
w.show();
return a.exec();
}
Note: when you do a change in the stylesheet.qss, you have to compile the qrc file for changes to take effect.
I set the stylesheet when the application is run from an external file. Also, for the designer we added a button to 'reload stylesheet'. This way the designer can modify the file and try the changes immediately.
For example:
QFile styleFile("stylesheet.qss");
styleFile.open(QFile::ReadOnly);
QByteArray bytes = styleFile.readAll();
QApplication *app = (QApplication*)QApplication::instance();
app->setStyleSheet(newStyleSheet);

First time using Qt: How to display an image?

Noob: How to display an image
I am very new to this, just starting actually. I need to figure out how to display an image on screen.
First I tried:
Source code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QPixmap qp = QPixmap("../images/tank2.bmp");
if(qp.isNull())
{
printf("Yes its null\n");
}
else
{
QGraphicsPixmapItem item(QPixmap("../images/tank2.bmp"));
scene.addItem(&item);
}
view.show();
return a.exec();
}
from:
Qt jpg image display
it compiles and runs but doesn't show an image. returns 0, etc.
Then I just sorta messed around from there. I'm also curious about something: In Qt editor they show a file structure that doesn't exist on the disk. They have files "Headers", "Sources", and Resources whereas on the systems its just a folder "projectname" with all the files in one folder. Is this just for visual clarity?
The version of QtCreator I'm using is 2.4.1 running Qt 4.7.4 64 bit.
My eventual goal is to make a widget where a picture is a clickable icon where you select that pic and can place it on a larger screen like a tile.
Another Question: Why does Qt have things like "QString" and "QChar"? Is there something wrong with the normal c++ libraries?
If you just want to display a simple image then make a Qlabel the central widget and call setPixmap() passing it your image path
"Another Question: Why does Qt have things like "QString" and "QChar"?
Is there something wrong with the normal c++ libraries?"
Yes there is lots wrong with the normal libraries - at least for std::string.
When Qt was started there wasn't very good cross platform STL support and the standard libraries were very bad at Unicode and support for translations. QString doesthis very well - although I think a combination of modern STL and boost can probably do everything QString can do.
Almost all of the Qt types are automatically reference counted so you can pretty much ignore memory management for them AND pass them around freely. There are also some tricks Qt can do because the extra MOC compile pass means it has Java-like introspection that standard C++ doesn't.
But in general you are free to use standard C++ types (Qt: Qt classes vs. standard C++)
Tested like this, it works. Don't forget to create qrc file.
#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QPixmap qp = QPixmap(":/images/123.bmp");
if(qp.isNull())
{
printf("Yes its null\n");
}
else
{
printf("HAHA");
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap(":/images/123.bmp"));
scene.addItem(item);
}
view.show();
return a.exec();
}
Here are qrc file:
<RCC>
<qresource prefix="/">
<file>images/123.bmp</file>
</qresource>
</RCC>
And .pro file
QT += core gui
TARGET = testimage
TEMPLATE = app
SOURCES += main.cpp
RESOURCES += 123.qrc
I think your problem is here:
{
QGraphicsPixmapItem item(QPixmap("../images/tank2.bmp"));
scene.addItem(&item);
}
item goes out of scope before you'll actually use it.
I'm also pretty sure you meant that to use the QPixmap that you loaded earlier at the top-level scope.
Generally speaking, you want to limit your questions on SO to a single question... but to address your last question: QChar and QString allow the Qt libs to make several assumptions about strings. The most obvious of which is that QStrings have a standardized encoding.