我将此字体添加到资源:BYekan.ttf
我想在我的应用程序中使用该字体。我已经试过了:

    QFont font(":/images/font/BYekan.ttf");
    nLabel->setFont(font);
    nLabel->setText(tr("This is for test"));
    layout->addWidget(nLabel);

但是,我想它不起作用。如何使用它?

编辑:
阅读this question之后,我再次尝试了:
int fontID(-1);
bool fontWarningShown(false);
QFile res(":/images/font/Yekan.ttf");
if (res.open(QIODevice::ReadOnly) == false) {
    if (fontWarningShown == false) {
        QMessageBox::warning(0, "Application", (QString)"Impossible d'ouvrir la police " + QChar(0x00AB) + " DejaVu Serif " + QChar(0x00BB) + ".");
        fontWarningShown = true;
    }
}else {
    fontID = QFontDatabase::addApplicationFontFromData(res.readAll());
    if (fontID == -1 && fontWarningShown == false) {
        QMessageBox::warning(0, "Application", (QString)"Impossible d'ouvrir la police " + QChar(0x00AB) + " DejaVu Serif " + QChar(0x00BB) + ".");
        fontWarningShown = true;

    }
    else
        nLabel->setFont(QFont(":/images/font/Yekan.ttf", 10));
}

我比较了这种字体和其他字体,但是Qt上没有任何区别。为什么?

最佳答案

int id = QFontDatabase::addApplicationFont(":/fonts/monospace.ttf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
QFont monospace(family);

07-28 01:31
查看更多