在QML中使用字体awesome:错误的字符

时间:2018-04-20 22:43:30

标签: qt qml font-awesome

我想在我的QML应用中使用字体真棒字体。但它效果不好。

首先我使用这种方法:https://github.com/QMLCommunity/font-awesome-qml 但我只有两个图标工作,看到我的应用程序的捕获。

仅显示笔记本电脑和电池图标。其他人(一个左上角,每个标签上的其他人)都有kanjis或其他符号。

我也尝试过这个解决方案后:How to use Font Awesome in QML 在左上方的图标上,没有成功。

这里是通知栏的代码:

$ virtualenv --python=/usr/local/python2.6.2/bin/python mypythonnewapp
Running virtualenv with interpreter /usr/local/python2.6.2/bin/python
New python executable in /home/vagrant/mypythonnewapp/bin/python
Installing setuptools<37, pip, wheel<0.30...
  Complete output from command /home/vagrant/mypythonnewapp/bin/python - setuptools<37 pip wheel<0.30:
  DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
Collecting setuptools<37
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/commands/install.py", line 324, in run
    requirement_set.prepare_files(finder)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/req/req_set.py", line 554, in _prepare_file
    require_hashes
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/req/req_install.py", line 278, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/index.py", line 792, in get_page
    "Cache-Control": "max-age=600",
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py", line 521, in get
    return self.request('GET', url, **kwargs)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/_vendor/cachecontrol/adapter.py", line 37, in send
    cached_response = self.controller.cached_request(request)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/_vendor/cachecontrol/controller.py", line 111, in cached_request
    resp = self.serializer.loads(request, cache_data)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/_vendor/cachecontrol/serialize.py", line 114, in loads
    return getattr(self, "_loads_v{0}".format(ver))(request, data)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/_vendor/cachecontrol/serialize.py", line 196, in _loads_v2
    return self.prepare_response(request, cached)
  File "/usr/lib/python2.6/site-packages/virtualenv_support/pip-9.0.3-py2.py3-none-any.whl/pip/_vendor/cachecontrol/serialize.py", line 157, in prepare_response
    **cached["response"]
TypeError: __init__() keywords must be strings
----------------------------------------
...Installing setuptools<37, pip, wheel<0.30...done.
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/virtualenv.py", line 2349, in <module>
    main()
  File "/usr/lib/python2.6/site-packages/virtualenv.py", line 712, in main
    symlink=options.symlink)
  File "/usr/lib/python2.6/site-packages/virtualenv.py", line 953, in create_environment
    download=download,
  File "/usr/lib/python2.6/site-packages/virtualenv.py", line 904, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/usr/lib/python2.6/site-packages/virtualenv.py", line 796, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /home/vagrant/mypythonnewapp/bin/python - setuptools<37 pip wheel<0.30 failed with error code 2

PS:我使用第二个链接解决方案作为第一个图标,但结果与第一个解决方案完全相同。

标签栏:

RowLayout {
    anchors.fill: parent
    Label {
        font.pointSize: 10
        font.family: "fa-solid-900"
        text: "\uf071" + " Message"
    }

    Item {
        Layout.fillWidth: true
    }
    Label {
        font.pointSize: 10
        font.family: awesome.family
        text: awesome.loaded ? awesome.icons.fa_laptop : "x"
        Layout.alignment: Qt.AlignRight
    }

    Label {
        font.pointSize: 10
        font.family: awesome.family
        text: awesome.loaded ? awesome.icons.fa_battery_full + " 100 %" : "x"
        Layout.alignment: Qt.AlignRight
    }
}

每次如何显示正确的图标?
提前谢谢!

2 个答案:

答案 0 :(得分:2)

我最近遇到了同样的问题,并在blog post中记录了我的方法。

基本上,我的问题是从版本5开始,Font Awesome在一个字体文件中提供了一些图标,其中&#34;常规&#34;重量和一个文件中的&#34; solid&#34;体重等级。但是,在加载时,两者都将使用字体名称&#34; Font Awesome 5 Free&#34; (假设使用免费版本)。

我原本预计可以通过设置例如在常规和实心图标集之间切换。 QML中字体的粗体属性:

import QtQuick 2.0

Item {
    width: 100
    height: 100

    FontLoader {
        id: faRegular
        source: "./fa-regular-400.ttf"
    }
    FontLoader {
        id: faSolid
        source: "./fa-solid-900.ttf"
    }

    Column {
        width: parent.width

        Text {
            font.family: "Font Awesome 5 Free"
            font.bold: true // use "solid" variant
            text: "\uf073" // fa-calendar-alt
        }
        Text {
            font.family: "Font Awesome 5 Free"
            font.bold: false // use "regular" variant
            text: "\uf073" // fa-calendar-alt
        }
    }
}

不幸的是,这对我来说不起作用,因为Qt合并了两种字体并且只使用了常规或固体变体(这导致某些图标根本不被渲染,特别是如果你使用包含很多的常规变体至少在免费版本的字体中少了图标。

我的解决方案&#34;是编辑实体字体文件(例如使用FontForge)并将字体名称更改为例如&#34; Font Awesome 5 Free Solid&#34;。有了这个,我能够同时加载两种变体的字体,并使用它们的字体名称在这两种变体之间切换:

import QtQuick 2.0

Item {
    width: 100
    height: 100

    FontLoader {
        id: faRegular
        source: "./fa-regular-400.ttf"
    }
    FontLoader {
        id: faSolid
        source: "./fa-solid-900.ttf"
    }

    Column {
        width: parent.width

        Text {
            font.family: faSolid.name // use solid variant
            text: "\uf073" // fa-calendar-alt
        }
        Text {
            font.family: faRegular.name // use regular variant
            text: "\uf073" // fa-calendar-alt
        }
    }
}

答案 1 :(得分:0)

这可能是由于字体合并时某些字符无法解析。您可以尝试从C ++端加载FontAwesome

  1. 使用QFontDatabase

    加载字体
    int fontId = QFontDatabase::addApplicationFont(QStringLiteral("/path/to/font-awesome.ttf");
    
  2. 禁用新字体的合并

    QFont font("fa-solid-900"); // You can also use the fontId to get family name here
    font.setStyleStrategy(QFont::NoFontMerging);
    
  3. 使用QFont Q_PROPERTY或直接在上下文中将字体暴露给QML

    Q_PROPERTY(QFont fontAwesome READ fontAwesome CONSTANT)
    
  4. 这在Qt邮件列表here

    中讨论