C++ 如何在OpenSuse上访问Qt中的Awesomefonts

C++ 如何在OpenSuse上访问Qt中的Awesomefonts,c++,qt,qt5,font-awesome,qicon,C++,Qt,Qt5,Font Awesome,Qicon,我希望在Qt应用程序中使用fontawesome()中的一些图标,我已将fontawesome-webfont.ttf文件提取到usr/share/fonts中。我尝试在线搜索,但找不到任何此类示例。这是我编写的用于从资源中提取图像的示例代码(不是必需的)还可以访问Qfont库本身中存在的一些Qfont(即下面的示例中的courier new) #包括“MainWindow.h” #包括“ui_main window.h” #包括 #包括 #包括 主窗口::主窗口(QWidget*父窗口): Q

我希望在Qt应用程序中使用fontawesome()中的一些图标,我已将fontawesome-webfont.ttf文件提取到usr/share/fonts中。我尝试在线搜索,但找不到任何此类示例。这是我编写的用于从资源中提取图像的示例代码(不是必需的)还可以访问Qfont库本身中存在的一些Qfont(即下面的示例中的courier new)

#包括“MainWindow.h”
#包括“ui_main window.h”
#包括
#包括
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
centralWidget=新的QWidget(此);
gridLayout=新的QGridLayout(centralWidget);
mylabel=新的QLabel();
mylabel2=新的QLabel();
font=新QFont(“快递”);
addresspic=新的QPixmap(:/new/prefix1/address.png”);
*addresspic=addresspic->缩放(50,50,Qt::KeepAspectRatio,Qt::FastTransformation);
mylabel->setPixmap(*addresspic);
mylabel2->setTextFormat(Qt::RichText);
mylabel2->setGeometry(QRect(QPoint(100100),QSize(150150));
mylabel2->setText(“地址图标”);
gridLayout->addWidget(mylabel2);
gridLayout->addWidget(mylabel);
字体->设置斜体(真);
字体->设置像素大小(20);
mylabel2->setFont(*font);
//网格布局->设置垂直间距(1);
//网格布局->设置水平间距(1);
此->设置centralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
删除用户界面;
}
再次感谢

编辑:错误的屏幕截图

编辑2:尝试G.M.的方法会导致以下错误:知道为什么吗?

尝试使用…显式加载字体

int fid = QFontDatabase::addApplicationFont("/usr/share/fonts/fontawesome-webfont.ttf");
然后,您可以使用…查询字体系列名称

在我的案例中,上述情况导致了单一的姓氏“FontAwesome”。在这种情况下,你应该能够使用的字体与

QFont f("FontAwesome");
注意:上述方法似乎可以发挥作用,但为字体指定点大小并没有达到预期效果。不确定原因,因为ttf文件似乎包含请求的图示符大小

编辑1 上面构造的
QFont
可以像任何其他unicode字体一样使用。因此,可以使用

QLabel label;
label.setFont(QFont("FontAwesome"));
label.setText("\uf0fe");    /* f0fe is the code point for fa-plus-square */
请注意,@eyllanesc提供的答案可能是更好的方法。我只是为了完整性而添加此编辑。

从下载并将
qfonticon.h
qfonticon.cpp
文件添加到项目中,然后使用以下代码创建图标:

QFontIcon::addFont("/path/your/fonts/{your font}.ttf");
QIcon icon = QFontIcon::icon(0xf2b9);

{your widget}->setIcon(icon);
例如:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPushButton>
#include "qfonticon.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWidget *centralWidget;
    QGridLayout *gridLayout;

    centralWidget = new QWidget(this);
    gridLayout = new QGridLayout( centralWidget );

    QFontIcon::addFont("/usr/share/fonts/fontawesome-webfont.ttf");


    for(int i = 0; i < 15; i++){
        QIcon icon = QFontIcon::icon(0xf2b9+i);
        QPushButton *b = new QPushButton();
        b->setIcon(icon);
        gridLayout->addWidget(b);
    }
    this->setCentralWidget(centralWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
#包括“qfonticon.h”
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
QWidget*centralWidget;
QGridLayout*gridLayout;
centralWidget=新的QWidget(此);
gridLayout=新的QGridLayout(centralWidget);
QFontIcon::addFont(“/usr/share/fonts/fontWebFont.ttf”);
对于(int i=0;i<15;i++){
QIcon=QFontIcon::icon(0xf2b9+i);
QPushButton*b=新的QPushButton();
b->设置图标(图标);
gridLayout->addWidget(b);
}
此->设置centralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
删除用户界面;
}

更多信息


我用Qt5.7和Qtcreator 4.2测试了它

试试:font=newQfont(“FontawesomeWebfont”)@eyllanesc它不工作,qt如何知道从何处访问图标复制文件后是否更新字体缓存?这将是
sudo fc cache-fv
@Matteo Italia尝试这样做well@theindianphil1更改为:虚拟虚空绘制(QPainter*painter,const QRect&rect,QIcon::Mode,QIcon::State)Q_DECL_覆盖;虚拟QPixmap pixmap(const QSize&size,QIcon::Mode,QIcon::State)Q_DECL_覆盖;出现了很多错误:“Q_DECL_OVERRIDE”未命名类型虚拟void paint(QPainter*painter、const QRect&rect、QIcon::Mode、QIcon::State)Q_DECL_OVERRIDE/home/adt/Project/Project1/Project2/MainWindow.cpp:12:错误:无效使用不完整的类型“class Ui::MainWindow”Ui(新Ui::MainWindow)^^^@IndianPhil1 qt4或qt5?使用qt creator 3.2.1的qt 5.3.2仅此一个错误::错误:“Q_DECL_OVERRIDE”未命名类型虚拟无效绘制(QPaint*painter,const QRect&rect,QIcon::Mode,QIcon::State);Q_DECL_覆盖^@印度朋友1我用Qt5.7和Qtcreator 4.2测试了它。如何分别访问fontawesome webfont内的图标。ttf,也可以共享您的代码我尝试了您的建议,但输出是垃圾值。我对问题进行了编辑,以便您可以查看输出。有什么建议吗?您在代码的
QLabel
s中都使用了
QFont(“FontAwesome”)
,但“地址图标”文本的显示方式与标准字体相同。这表明字体加载不正确。检查从
QFontDatabase::addApplicationFont
返回的值——如果它是-1,则存在问题。其返回(“FontAwesome”)本身,也更正了代码,只有一个标签使用FontAwesome。仍在产生垃圾值。更新屏幕快照嘿,你在编辑中共享的代码有效。还有一件小事,我如何将这些图标的颜色从标准颜色更改为任何其他颜色。
QFontIcon::addFont("/path/your/fonts/{your font}.ttf");
QIcon icon = QFontIcon::icon(0xf2b9);

{your widget}->setIcon(icon);
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPushButton>
#include "qfonticon.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWidget *centralWidget;
    QGridLayout *gridLayout;

    centralWidget = new QWidget(this);
    gridLayout = new QGridLayout( centralWidget );

    QFontIcon::addFont("/usr/share/fonts/fontawesome-webfont.ttf");


    for(int i = 0; i < 15; i++){
        QIcon icon = QFontIcon::icon(0xf2b9+i);
        QPushButton *b = new QPushButton();
        b->setIcon(icon);
        gridLayout->addWidget(b);
    }
    this->setCentralWidget(centralWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}