c++visual-studioqticons

QT Framewrok and Visual Studio - icon conflict


Introduction

I need to set a icon, both for .exe file, in top-left corner of my app window, and on the task bar (Windows 10). However it works only partially - I'm able to set .exe icon, but that's it. In the other two places I have default icon. I'm working with QT 5.9.7 inside Visual Studio 2017.

What I tested so far

I have following line in my .rc file:

IDI_ICON1               ICON                    "favicon.ico"

Which lets me to have .exe icon. But it seems to not affect the rest of framework. I was trying to set in-app icon this way (main.cpp):

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowIcon(QIcon(":/favicon.ico"));
    // a.setWindowIcon also doesn't work.
    return a.exec();
}

or even inside mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/favicon.ico"));
}

However it seems to be ignored - it has no effect, also it doesn't give me any error. I also tried to include imageformats/qico.dll folllowing THIS instruction - still no luck.

The Question

So - how to set this two other icons correctly - on Windows task bar, and in the corner of my application window?

EDIT: Adding qrc file also doesn't fix the problem.


Solution

  • I have found the solution, so I will post it here, in the case someone else will encounter the same problem:

    First of all - the thing has nothing to do with .rc or .qrc file, the icon can be even loaded directly from app directory. The problem is in the supported formats - for some reason, under Windows 10 there is an issue with .ico format in QT framework. Theoretically addition of imageformats/qico.dll should fix the issue, but it doesn't. The solution is to use other format (in my case .png), and place it directly where the .exe file is, not in any subdirectory. Then you can call it like this:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        a.setWindowIcon(QIcon("favicon.png"));
        return a.exec();
    } 
    

    Alternatively, if you want to have icon compiled in binary form, you can try to add it to resources, however the format still mustn't be .ico to make it works, as this is the origin of the issue. The size of the icon is adjusted correctly, so it look good both in the corner of app window, and on the task bar - therefore .png is fairly enough in this case.