Page 1 of 1
Qt - How to set a window icon?
Posted: Wed Oct 30, 2019 5:04 pm
by deeproot
Might be a fairly simple thing but I've got stuck on this! Just want to set my own icon on the window (and hopefully the system task-bar). I would imagine most people using Qt will want to do this.
For GTK I do the following:
Code: Select all
CatchImage(#Image_Icon, ?ProgIcon)
gtk_window_set_default_icon(ImageID(#Image_Icon))
What would be the equivalent for Qt, either for all windows in a program or maybe for a specific window (my first small Qt program only has one window).
Messing around with QtScript - I can see the window object has a windowIcon member, but not found a way to set it. (total beginner with QtScript!!)
Note - I'm only concerned here with an embedded window icon, not a launcher icon for the program.
Re: Qt - How to set a window icon?
Posted: Wed Oct 30, 2019 9:18 pm
by wombats
I could not find a way to do this with QtScript, but it is possible with a shared library. If you create a shared library project with Qt Creator, you can add a function something like (there may be a more elegant way to find the window in question):
C++Code: Select all
extern "C" SETWINDOWICON_EXPORT void SetWindowIcon(const char * _windowTitle, const char * _fileName)
{
QApplication *b = static_cast<QApplication *>(qApp);
QString windowTitle = QString::fromUtf8(_windowTitle);
const QWidgetList topLevelWidgets = b->topLevelWidgets();
for (QWidget *widget : topLevelWidgets) {
if (widget->windowTitle() == windowTitle) {
widget->setWindowIcon(QIcon(_fileName));
break;
}
}
}}
PureBasic
Code: Select all
OpenLibrary(0, "libSetWindowIcon.so.1.0.0")
Prototype SetWindowIcon(window.p-utf8, file.p-utf8)
Global SetWindowIcon.SetWindowIcon = GetFunction(0, "SetWindowIcon")
OpenWindow(0, 0, 0, 320, 240, "Window", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
SetWindowIcon(GetWindowTitle(0), "icon.png")
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Re: Qt - How to set a window icon?
Posted: Thu Oct 31, 2019 1:23 pm
by deeproot
Thanks wombats - this could be a good solution for some projects at least.
Unfortunately it is probably not ideal for the small program I'm working on, because my idea is to have no external dependencies other than standard run-time Qt libraries. So an additional shared library isn't perfect. The aim is to be able to distribute to customers in the simplest way possible and compatible with most popular distros. However, it's early days and I'm still looking at possible options! (also still learning about Qt)
Re: Qt - How to set a window icon?
Posted: Thu Oct 31, 2019 1:33 pm
by wombats
I'm afraid that's the only solution I could find. I personally don't see the issue with including another shared library since most applications are distributed via archive or installer.
I haven't tested it, but does the "Use Icon" option in the Compiler Options work on Qt? Or do you need to change it at runtime?
Re: Qt - How to set a window icon?
Posted: Fri Nov 01, 2019 1:26 pm
by deeproot
The Compiler Options "Use Icon" option is greyed out on Linux, regardless of the subsystem. I seem to remember some previous discussion about this and I think it is quite understandable considering all the desktop variations. My biggest project was ported over from Windows and Mac and although greyed-out my icon filename is still shown. The file exists but has no effect.
For GTK the api method works nicely and I think that's a perfectly acceptable method.
For Qt I may well use your shared library solution. I certainly couldn't find any other method myself - spent quite a while on it! I was trying to keep distributed files as minimal as possible but probably would not be a big deal. I've been using a simple tar archive for the Beta testers, but proper release is long way off yet!