It is currently Wed Nov 20, 2019 11:43 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Qt - How to set a window icon?
PostPosted: Wed Oct 30, 2019 5:04 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Dec 17, 2009 12:00 pm
Posts: 185
Location: Llangadog, Wales, UK
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:
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.

_________________
www.deeproot.co.uk


Top
 Profile  
Reply with quote  
 Post subject: Re: Qt - How to set a window icon?
PostPosted: Wed Oct 30, 2019 9:18 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 500
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:
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Qt - How to set a window icon?
PostPosted: Thu Oct 31, 2019 1:23 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Dec 17, 2009 12:00 pm
Posts: 185
Location: Llangadog, Wales, UK
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)

_________________
www.deeproot.co.uk


Top
 Profile  
Reply with quote  
 Post subject: Re: Qt - How to set a window icon?
PostPosted: Thu Oct 31, 2019 1:33 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 500
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Qt - How to set a window icon?
PostPosted: Fri Nov 01, 2019 1:26 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Dec 17, 2009 12:00 pm
Posts: 185
Location: Llangadog, Wales, UK
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!

_________________
www.deeproot.co.uk


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye