FontAwesome在使用QT和QML的Mac App中不起作用

时间:2018-12-14 11:34:20

标签: qt qml

我已经在我的QT项目中添加了真棒字体:

这一个https://fontawesome.com/how-to-use/on-the-desktop/setup/getting-started

问题在于它在Windows系统上运行良好,但在MAC中却无法运行。

下面,我将显示一个示例应用程序,其中包含所有代码以说明问题:

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QFile res2(":/resources/fonts/Font Awesome 5 Free-Solid-900.otf");
    QFile res3(":/resources/fonts/Font Awesome 5 Free-Regular-400.otf");
    QFile res4(":/resources/fonts/Font Awesome 5 Brands-Regular-400.otf");

    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

main.qml

import QtQuick 2.7
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import QtQuick.Window 2.3

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    FontLoader { id: fontAwesomeSolid; source: "fonts/Font Awesome 5 Free-Solid-900.otf" }
    FontLoader { id: fontAwesomeRegular; source: "fonts/Font Awesome 5 Free-Regular-400.otf" }
    FontLoader { id: fontAwesomeBrands; source: "fonts/Font Awesome 5 Brands-Regular-400.otf" }

    Button {
        id:buttonAbout
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottomMargin: 0
        width:15
        height: 15
        text: "\uf129"
        font.family: fontAwesomeRegular.name
        contentItem: Text {
            anchors.fill:buttonAbout
            anchors.right:buttonAbout.right
            anchors.bottomMargin: 3
            text: buttonAbout.text
            font: buttonAbout.font
            color: "black"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
        background:
            Rectangle{
                width:15
                height: 15
                anchors.top:parent.top
                anchors.right: parent.right
                radius:10
                border.width: 1
                border.color:"gray"
            }
        onClicked: popup.open()
    }

    Popup {
        id: popup
        x: 100
        y: 100
        width: 200
        height: 300
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.close()
    }

}

我创建了一个新的.qrc文件,在其中添加了fontawesome的文件夹fonts Font Awesome 5 Free-Solid-900.otf Font Awesome 5 Free-Regular-400.otf Font Awesome 5 Brands-Regular-400.otf

在Windows中演示发生什么情况的屏幕: enter image description here

和在MAC中: enter image description here

这是通过QT 5.12

完成的

编辑: 我提供此应用的代码 https://drive.google.com/file/d/1ZSZBzg0fS9IUNpQ540M4cGlRQATV4NlU/view?usp=sharing

这样,使用MAC的任何人都可以测试此代码吗?

0 个答案:

没有答案