Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

c++ - how to set an icon on a Main window and action with 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...