确定了设置图标的格式,但是设置任务栏图标去出现了问题,死活显示不出来于是做了一个小的例程用来测试
例程
vim untitled1123.pro
QT += core gui webenginewidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
vim mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebEngineWidgets>
#include <QIcon>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebEngineView *view = new QWebEngineView;
QUrl url = QUrl::fromUserInput("http://www.baidu.com");
view->load(url);
ui->gridLayout->addWidget(view,0,0);
}
MainWindow::~MainWindow()
{
delete ui;
}
vim main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.setWindowIcon(QIcon("./task.ico"));
return a.exec();
}
通过例程,发现可以设置啊,用的当前路径,那就奇怪了。
经过一番尝试,发现需要通过资源文件才能够正确显示任务栏图标,奇了怪了。。。
添加资源文件如下,在pro文件中:
RESOURCES += \
source.qrc
添加了qrc资源文件,就不需要添加ICO了
unix {
# ICON = platform/Kylin/Resources/task.ico
}
c文件中:
#ifdef Q_OS_MAC
//设置程序图标
SingleApplication::setWindowIcon(QIcon(":/image/platform/MacOS/Resources/Icon.icns"));
#else
SingleApplication::setWindowIcon(QIcon(":/image/platform/Kylin/Resources/task.ico"));
#endif
qrc文件中:
<RCC>
<qresource prefix="/image">
<file>platform/MacOS/Resources/Icon.icns</file>
<file>platform/Kylin/Resources/task.ico</file>
</qresource>
</RCC>
自此,才可以正确的显示任务栏图标。