简体   繁体   中英

Qt4 application icon not appearing on Windows taskbar

My Qt4 application, when launched, does not appear as an icon on the taskbar of Windows 7 and Windows XP. How can I make it appear?

When I minimize my app, it turns into a small window-less title bar on the bottom left.

And by the way, I'm doing the following in my CMakeLists.txt, to prevent a console window from tagging along with my app:

if(WIN32)
  add_executable( codequery WIN32 ${CODEQUERY_SRCS} ${CODEQUERY_MOC_SRCS} ${CODEQUERY_RC_SRCS} ${CODEQUERY_UI_HDRS} ${QM} )
else()
  add_executable( codequery ${CODEQUERY_SRCS} ${CODEQUERY_MOC_SRCS} ${CODEQUERY_RC_SRCS} ${CODEQUERY_UI_HDRS} ${QM} )
endif()

Here's my main function:

#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow *wndw = new QMainWindow;
mainwindow mw(wndw, &app);

mw.show();
return app.exec();
}

Here's part of mainwindow's declaration:

namespace Ui {
     class MainWindow;
 }

class mainwindow : public QMainWindow
{
  Q_OBJECT

public:
Ui::MainWindow *ui;
mainwindow(QMainWindow *parent = NULL, QApplication *app = NULL);
virtual ~mainwindow();

And here's part of its constructor:

#include "mainwindow.h"
#include "ui_mainWindow.h"

mainwindow::mainwindow(QMainWindow *parent, QApplication *app)
:QMainWindow(parent)
,m_app(app)
,ui(new Ui::MainWindow)
 {
     ui->setupUi(this);
 }

The GUI part was designed using Qt Designer, and it's generated as ui_mainWindow.h.

Part of the setupUi function:

void setupUi(QMainWindow *MainWindow)
{
    if (MainWindow->objectName().isEmpty())
        MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    MainWindow->resize(629, 600);
    QIcon icon;
    icon.addFile(QString::fromUtf8(":/mainwindow/images/logo.png"), QSize(), QIcon::Normal, QIcon::Off);
    MainWindow->setWindowIcon(icon);

I found it! WS_EX_APPWINDOW must be added.

#ifdef _WIN32
#include <windows.h>
#endif

#include "mainwindow.h"


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMainWindow *wndw = new QMainWindow;
    mainwindow mw(wndw, &app);

    mw.show();

#ifdef _WIN32
    ShowWindow(mw.winId(), SW_HIDE);
    SetWindowLongPtr(mw.winId(), GWL_EXSTYLE, 
                GetWindowLongPtr(mw.winId(), GWL_EXSTYLE) | WS_EX_APPWINDOW);
    ShowWindow(mw.winId(), SW_SHOW);
#endif

    return app.exec();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM