Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set an icon on a Main window and action with QT

Tags:

c++

qt

Honestly I do not understand resource files and how to get so that my things can get done, because it was partially explained to me and I'm quite confused where to put icon and how to make it visible on my programs.

setWindowIcon(QIcon(":/images/icon.png")); 

It doesn't show up or even show a error.

like image 683
Parias Lunkamba Mukeba Avatar asked Mar 26 '15 17:03

Parias Lunkamba Mukeba


People also ask

How do I make Qt icons default?

To use these icons within Qt Designer you would select the drop-down and choose "Set Icon From Theme..." You then enter the name of the icon you want to use, e.g. document-new (the full list of valid names).

Does Qt have built in icons?

Qt ships with a small set of standard icons you can use in any of your applications for common actions. The icons are all accessible through the current active application style using .

How do I create a window in Qt?

Let's open up Designer by double-clicking the mainwindow. ui to put menu bar and action. Type in "File" at the top menu, and "New Window..." under the "File" menu. We need to press Enter to make a real change in the menu.


2 Answers

Create a resources file named resources.qrc:

<!DOCTYPE RCC>
<RCC version="1.0">
  <qresource>
    <file>path/to/icon.png</file>
  </qresource>
</RCC>

Make sure that path/to/icon.png is an actual path, relative to the directory that contains resources.qrc.

In your .pro file, include the resource:

TARGET = your_app
TEMPLATE = app
QT += widgets 
RESOURCES += path/to/resources.qrc

Again, make sure that path/to/resources.qrc exists, relative to the directory that contains the project file.

After compiling, your resource will be embedded into your executable. It can be accessed like:

setWindowIcon(QIcon(":/path/to/icon.png"));

If the icon is not appearing, try this stackoverflow question or this one.

Another approach would be to use the Application Icon. This will set the application icon for your application on the desktop and start menus, and also on the top left corner of QMainWindows and QDialogs

like image 54
Walt Dizzy Records Avatar answered Oct 25 '22 23:10

Walt Dizzy Records


Wouldn't it be the simplest to use QtCreator's Designer window? In the lower right corner you can find 3 tabs - click on the most righthand one, then click on the pen:

qt designer resources

This will open Resources editor:

Qt creator Resources Editor

Define new resources file (below the left pane), than add a "namespace" (rigth pane) and add your files. You can set aliases for them, so that when you decide to replace your icon with somethin else - you only have to switch the path, as long as alias is kept the same. You can then reference your resources via their alias to set your icon where needed, eg: setWindowIcon(QIcon(":/HurBudClientGUI/plug"));

Also - take your time to read this: http://doc.qt.io/qt-5/resources.html and that: http://doc.qt.io/qt-5/designer-resources.html

Good luck!

like image 40
murison Avatar answered Oct 25 '22 22:10

murison