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.
Related
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.
I develop a program on MacOS using QT5.1.1 and I started to use the translation tools in order to translate my program to French (for the moment). I use the code below to install the .qm file :
QApplication a(argc, argv);
QTranslator translator;
translator.load("/path_to_qm_file");
a.installTranslator(&translator);
Using the English file I've got the About and Preferences sub-menu which automatically goes in the Joker menu like that :
And when I load the French file About and Preferences go to the File menu :
How to make Qt understand that I want the first behavior to be the only one it should use ?
This is due to automatic deduction of QAction menu roles. The deduction works for English text, but not for French, especially that you're using the wrong translation of Properties (not the one from Apple's HIG). You need to explicitly set the menu role of your Preferences action to QAction::PreferencesRole - using QAction::setMenuRole. That will solve the problem.
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.
im developing QT application, and im using few icons
this is my resource.qrc file
<RCC>
<qresource prefix="/new/prefix1">
<file>army-officer-icon.png</file>
<file>uac.png</file>
</qresource>
</RCC>
then i ofcourse include it in my .pro file
RESOURCES += \
resource.qrc
and here is the code which takes care of icons:
//this code is part of the mainwindow.cpp
QSystemTrayIcon *trayIcon;
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QIcon(":/new/prefix1/army-officer-icon.png"));
trayIcon->show();
//this code is part of the ui_mainwindow.h (generated by QT)
QLabel *label_5;
label_5 = new QLabel(centralWidget);
label_5->setObjectName(QString::fromUtf8("label_5"));
label_5->setGeometry(QRect(40, 302, 46, 21));
label_5->setPixmap(QPixmap(QString::fromUtf8(":/new/prefix1/uac.png")));
label_5->show();
So, this looks reasonable, right ?
===windows 7, developing station, QT installed
also the result effect is as i expected
i can see both icons :: tray icon & uac shield = awesome
but when i move to the other workstation, somethings strange happens
===windows XP, user work station, QT NOT installed
as u can see, the trayicon still has its own icon, but the "uac shield icon" dissapears... its very disturbing, and i really dont get it since both icons are *.png formats, i browsed a lot, and maybe it has connection with qt image plugins (althought i think that i should care about it only when my icon are *.jpeg, *.gif format), but wasnt able to make this solution works...
so any ideas are welcomed.
Thanx in advance.
Okay, ive found the solution just after ive posted this (actually , i was quite close, but i copied something with wrong path :/)
locate C:\QtSDK\Desktop\Qt\4.8.0\mingw\plugins
copy plugins/imageformats to your application folder
open main.cpp and add this line of codes
a.addLibraryPath(QCoreApplication::applicationDirPath ()+"/plugins");
compile, and have a look at your beautiful icons :)
In my last question (Qt/C++: Icons not showing up when program is run) I asked how to get an icon to show up on a toolbar and was told I needed a Qt Resource, which I added and that fixed my problem, the icon did show up on the toolbar.
Now I'm trying to set the title icon of a window, using the same resource file, and it shows up fine in the Qt preview viewer but blank in the actual program. I am using a MainWindow which has an MDIArea and the children are MainWindows as well; neither the parent MDI nor child MDI windows icons will show properly. On the parent, I see the regular "Windows Application icon" and on the child, the icon is completely blank.
How can I solve this?
You will have to go through a standard resource file for windows. (That is, a .rc)
The process (as described in the documentation) is:
Store the ICO file in your application's source code directory, for
example, with the name myappico.ico. Then, create a text file called,
say, myapp.rc in which you put a single line of text:
IDI_ICON1 ICON DISCARDABLE "myappico.ico"
Finally, assuming you are using qmake to generate your makefiles, add this line to your myapp.pro
file: RC_FILE = myapp.rc
Regenerate your makefile and your application. The .exe file will now be represented with your icon in Explorer.
In the Visual Studio case you're simply able to add a resource to your project.