How to display icon in QMessageBox? - qt

I have an about box that I'm trying to display an icon inside.
Here is my code:
QMessageBox about_box(this);
about_box.setText("...");
about_box.setIconPixmap(QPixmap("qrc:/images/logo.png"));
about_box.setParent(this);
about_box.exec();
Here is my resource file:
<RCC>
<qresource prefix="/images">
<file>logo.png</file>
</qresource>
</RCC>

You don't need the qrc prefix:
about_box.setIconPixmap(QPixmap(":/images/logo.png"));

You will need this function
EDIT: I didn't see that the OP had already used this.
Are you sure you're running qmake (and thus rcc) when compiling?

Related

How to add prefix in QtCreator

Currently I've Custom Components in / prefix. I want to store all Custom Components in another prefix. Here's how I tried to create a prefix.
but nothing appears under qml.qrc
Open your qml.qrc file in a text editor and you should see something like this:
<RCC>
<qresource prefix="/">
<file>Components/PathButton.qml</file>
...
Change that to this:
<RCC>
<qresource prefix="/MyPrefix/">
<file>Components/PathButton.qml</file>
...
Now you will have to use import "MyPrefix/Components in your qml file.

Qml works when run but will not load photos in Qt designer

When working on a small qml project I found this behavior and I am not sure if I am doing anything wrong or if this is just how it is.
I created a simple test file Test.qml along with the main.qml and main.cpp
import QtQuick 2.0
Item {
width: 212
height: 212
Image {
id: image
x: 0
y: 22
width: 212
height: 168
source: "images/PaperSpeed.png"
fillMode: Image.PreserveAspectFit
}
}
The image linked in source above
shows the actual image in designer
but fails to show the image when running
I go ahead and change the source to the qrc URL:
source: "qrc:/img/images/PaperSpeed.png"
but now it fails to show up in designer
and instead shows up when running
What I find even weirder The image does show up in designer when on the Test.qml file but does not show up in designer when on the main.qml file for both source cases
my .pro file:
QT += quick
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp
RESOURCES += qml.qrc
QML_IMPORT_PATH =
QML_DESIGNER_IMPORT_PATH =
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
and the qrc file:
<RCC>
<qresource prefix="/">
<file>Thermostat.qml</file>
<file>main.qml</file>
<file>Test.qml</file>
</qresource>
<qresource prefix="/img">
<file>images/PaperSpeed.png</file>
</qresource>
</RCC>
This did seem to happen out of no where when I tried to import a new module like QtQuickContols eventually got rid of it but the problem persisted.
I have tried messing around with the QML emulation layer and moving the files around within the qrc. Anyone have any ideas as to what I can try or what may be going on?
Sorry in advance for any formatting issue!
I had same issue. It solves if i add an image to window.
Check the exaple below. I have same result if i remove test image from window.
For anyone still looking at this, what worked for me is separating the UI elements into a .ui.qml and the backend logic into a .qml file. This does a better job at loading UI elements and pictures when the .ui.qml file is opened in designer.

Qt Image from resource file

I'm trying to insert an image to my program via resource file, which is like:
<RCC>
<qresource prefix="/">
<file>green.png</file>
<file>other files</file>
</qresource>
</RCC>
and when I'm trying to load it using QImage or QPixmap, like:
QImage *green = new QImage(":/green.png");
if(green->isNull()) qDebug("null");
I always see that null message, indicating that I'm doing something wrong. One solution may be using absolute path like
"C:\\Users\\user\\Documents\\project\\green.png",
which works of course, but I'd prefer implement it using resource file. Could you please give me some advice?
All this will work if your png files are located in the same folder as .pro, .qrc, and .cpp files of your project.
Usually it is convenient to put all images to special subfolder Resources, for example. Then in .qrc the line will look like:
<file>Resources/green.png</file>
And in .cpp file:
QImage *green = new QImage(":/Resources/green.png");
First of all, you need to create a .qrc file and add image folder to it
(image folder must be contained inside of the project folder)
Right-click on the project file
Add New
Qt
Qt Resource File press Choose and do other steps
after opening .qrc file you must press Add > Add Prefix > change prefix name if you want
again Add > Add File > and choose your images
then go to mainwindow.cpp (in my project ) file and call the images as below code
in my case the icon folder is Playericons
ui->play->setIcon(QIcon(":/Playericons/icons8-pause-30.png"));
Did you remember to run qmake after adding the resource file?

QML + .qrc: Keeping images under another prefix

My problem has been similar to that discussed in another stackoverflow discussion and I could get my code working that way. However, I'm not completely satisfied with that solution. Initially, I had my .qml-file under one prefix ("/") and my images under another ("/images"). Without abandoning this separation, I don't get the program running.
Is there any (simple) way to use different prefixes in a QML project with .qrc resource file?
If you want to group the files by prefix based on the actual folder name without lengthening the reference name then why not use the alias keyword?
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
<qresource prefix="/images">
<file alias="foo.png">images/foo.png</file>
</qresource>
</RCC>
This will remove the extra indirection that was causing confusion i.e. you can access it with :/images/foo.png instead of :/images/images/foo.png
An explanation and further examples are available in the Qt docs (search for "alias"): http://doc.qt.io/qt-5/resources.html
Instead of asking, I should have gone drinking a coffee or do some sports. It's an embarrassing beginner's problem. Still, there might be others like me...
My QtQuick application consisted of essentially of a C++ source file main.cpp, a resource file qml.qrc and an image foo.png.
Source file (the shown code is generated automatically by QtCreator):
//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
Resource file with additional prefix for images:
//qml.qrc
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
<qresource prefix="/images">
<file>images/foo.png</file>
</qresource>
</RCC>
A qml file, where I want to import an image:
//main.qml
import QtQuick 2.2
import QtQuick.Window 2.0
Window {
visible: true
Image {
source: ???
}
}
My problem was that I didn't know what to write instead of ??? in the .qml file. To import the graphic you need to write "/images/images/foo.png", but my mind revolted against the idea of writing /images twice.
Thanks.
Instead of ???, you must put the path "qrc:/images/images/foo.png"
Example:
//main.qml
import QtQuick 2.2
import QtQuick.Window 2.0
Window {
visible: true
Image {
source: "qrc:/images/images/foo.png"
}
}
In your case, instead of writing "images" twice as your source path, you could have defined the file in your resource file as follows:
<file alias="foo.png">images/foo.png</file>
Then you are allowed to use "images" only once.
Window {
visible: true
Image {
source: "qrc:/images/foo.png"
}
}
See the documentation!

Customise QPushButton with an image

I am using Qt5 and having some problems in setting the back ground image of a QPushButton. I am using Qt Creator and Design to drag and drop the QPushButton. After that I created a source folder and placed an image in that directory and I googled and tried doing
ui->pushButton_play->setStyleSheet(QString::fromUtf8("background-image: url(:/source/play.png);"));
but no success...am I creating the folder in a wrong place?..I created in D:\Qt5_PROJECT_NAME\source or will I make the url(:\\source\\play.png). Nothing is working...please help...
Did you add the images file into .qrc file? A URL start with : has indicated that It will be searched in .qrc file. If .qrc file didn't used, URL should not start with :. Maybe you should access the image file as
ui->pushButton_play->setStyleSheet(QString::fromUtf8("background-image: url(source/play.png);"));
This is The Qt Resource System documentation.

Resources