Cannot load image for QLabel background - qt

my qrc file defined as follows,
<RCC>
<qresource prefix="/images">
<file>Resources/images/background.png</file>
....
I want to use the file background.png as my label's background.
I did like this,
label->setStyleSheet( "background-image: url(:/images/background.png);" );
but it cannot set the image as background.
Is it anyway to know why label cannot load the image? Cannot find the image?
thanks

You said you want the /images prefix for the Resources/images/background.png file, so in the resource system the file is available as
:/images/Resources/images/background.png
If you want also to simplify the file's path, use an alias:
<file alias="background.png">Resources/images/background.png</file>
This will make it available under
:/images/background.png
For the future, learn how to debug such simple problems yourself: just putting
QDirIterator i(":/", QDirIterator::Subdirectories);
while (i.hasNext())
qDebug() << i.next();
in your main function will tell you how your resource hierarchy looks like, and so if you're using a wrong resource path.

Related

How to set a thumbnail to my custom file for a preview in Windows Explorer?

In my Qt application, I can save project files of my own type. I would like those files to have a nice preview in Windows explorer, just like picture and video files do by default. Is there a way to do that? I am using Qt, but maybe there is another way.
In other words, if my code for saving a file is as follows, I would like to know what to do in line 5 to make it work:
void saveFile(const QString& fileName, const QImage& thumbnail) {
QFile file(fileName);
file.open(QFile::WriteOnly);
writeInFile(file); // Custom function that saves the project
//file.setPreview(thumbnail); <- What I wish I could simply do
file.close();
}
Setting thumbnail for your own file type is not a Qt thing. It's job of Windows shell, and the behavior is controlled using registery.
Check this if you want to assign a custom icon file.
If you want to generate different preview for each file, just like image file, then check this.
Note they are Windows specific.

geting icon by name is not working

I can't get icon lookup by name to work correctly. I copied the code from Gallery examples and repeated the same configuration in my project, but it doesn't work.
This is what I did:
1.Copy icons/gallery into the directory of my own project, this is the list of files:
icons/default
icons/default/20x20#3
icons/default/20x20#3/back.png
icons/default/20x20#3/menu.png
icons/default/20x20#3/drawer.png
icons/default/20x20#4
icons/default/20x20#4/back.png
icons/default/20x20#4/menu.png
icons/default/20x20#4/drawer.png
icons/default/20x20
icons/default/20x20/back.png
icons/default/20x20/menu.png
icons/default/20x20/drawer.png
icons/default/index.theme
icons/default/20x20#2
icons/default/20x20#2/back.png
icons/default/20x20#2/menu.png
icons/default/20x20#2/drawer.png
2.Added index.theme file into theme directory:
[Icon Theme]
Name=default
Comment=Qt Quick Controls 2 Gallery Example Icon Theme
Directories=20x20,20x20#2,20x20#3,20x20#4
[20x20]
Size=20
Type=Fixed
[20x20#2]
Size=20
Scale=2
Type=Fixed
[20x20#3]
Size=20
Scale=3
Type=Fixed
[20x20#4]
Size=20
Scale=4
Type=Fixed
3.Added corresponding lines to main.cpp, to enable icons, this is the code:
QGuiApplication::setApplicationName("MyApp");
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QIcon::setThemeName("default");
QQuickStyle::setStyle("Material");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
In a QML file I use the icon this way:
ToolButton {
icon.name: "menu"
}
If I lookup by URL, it works fine:
ToolButton {
icon.source: "qrc:/icons/default/20x20/menu.png"
}
So, what could be the reason of why lookup by name doesn't work and how to debug this ?
EDIT:
After Mitch's answer I found that icon's weren't working because the ":/icons" entry in the icon's path wasn't the first one.
So, this code DOESN'T work:
QIcon::setThemeName("default");
QStringList list;
list<<":/icons";
list<<"/usr/share/icons";
QIcon::setThemeSearchPaths(list);
However, this cdoe DOES work:
QIcon::setThemeName("default");
QStringList list;
list<<":/icons";
list<<"/usr/share/icons";
QIcon::setThemeSearchPaths(list);
To make it work you only need to make the path where your icons are located, first entry in list.
But maybe it is a bug, I am using Qt 5.11
If the list of things you did is exhaustive, then you missed a step:
Traditionally, only Linux and UNIX support icon themes on the platform level, but it is possible to bundle a compliant icon theme in an application to use themed icons on any platform.
The default icon theme search paths depend on the platform. On Linux and UNIX, the search path will use the XDG_DATA_DIRS environment variable if available. All platforms have the resource directory :/icons as a fallback. Custom icon theme search paths can be set with QIcon::setThemeSearchPaths().
The following example bundles an icon theme called mytheme into the application's resources using Qt's resource system.
<RCC>
<qresource prefix="/">
<file>icons/mytheme/index.theme</file>
<file>icons/mytheme/32x32/myicon.png</file>
<file>icons/mytheme/32x32#2/myicon.png</file>
</qresource>
</RCC>
After creating that file, you also need to add it to your .pro.

Qt does not show images

Problem show in the link below:
http://postimg.org/image/sp9mm85qb/
I try to add this image in code
QIcon iconmain("qrc:/img/main.png");
ui->tabWidget->tabBar()->setTabIcon(1,iconmain);
Tried to add image from resourse file from disk. But it still invisible.
I assume the image is in the resource file with the prefix img, so you would access it without the qrc text prefix: -
QIcon iconmain(":/img/main.png");
Solved by changing msvs compil to mingv

PyQt - Error while setting QLineEdit background from image

I searched all over the internet for a solution for this, but found nothing.
Basicly I have a QLineEdit name 'le' (inside a class) whose background I want to set as an image. Using the code bellow, i get the error "Could not parse stylesheet of widget 0x1867580" and the image is not dispalyed.
self.le = QtGui.QLineEdit(self)
self.le.move(300,300)
self.le.setFrame(False)
self.le.setFixedWidth(100)
self.le.setStyleSheet("background: url:(Try.png)")
The Try.png image is a 100x20 px image, so it was supposed to fit that particular QLineEdit.
Does anyone have a clue about what might be going on?
Many thanks in advance,
Miguel
Try
self.ls.setStyleSheet("background-image: url(Try.png);")
but Try.png must be in folder from which Qt app started. Unless it is a resource, then
self.ls.setStyleSheet("background-image: url(:/images/try);")
where images is name of resource group
<qresource prefix="/images">
<file alias="try">Try.png</file>
</qresource>
then the image will be compiled into the app.

fix file path according to iis

i have a website #local and lots of images in site, but after I deploy site to server images paths are broken(exception given in css file), and I need to fix this as soon as possible.
sample path:
imageurl = #"/Images/sample.gif";
how can i fix this?
thank you.
you can set a appsetting in web.config file with server url like
and get this path on code and add image name with it.
Images (like background-url) in CSS are always referenced relative to the css file.
you need to set in css file like...
background-image: url( '../../Images/image.gif' );
.. this will bring out one folder from current folder hierarchy

Resources