我想检索扩展名为“arial.ttf”的字体文件路径。
QFont::rawname()方法总是返回“未知”。
还有其他方法来获取字体的名称和扩展名吗?
下面是我们使用的代码:
bool ok = true;
QFont font = QFontDialog::getFont(&ok, this);
if(ok)
{
QString fontpath = "/Library/Fonts/" + font.family()+".ttf";//Using font file path in the application
}发布于 2013-09-30 23:34:07
QFont是对字体的请求,而不是对匹配的实际字体的描述;后者是QFontInfo。看看我的其他答案。不幸的是,QFontInfo没有给您rawName()。如果根本没有保证它在所有平台上都能工作,那么有一种迂回的方法可以得到它。
QFont myFont;
QFontInfo info(myFont);
QFont realFont(info.family());
QString rawName = realFont.rawName(); 如果您正在寻找诸如字体之类的东西的标准位置,那么在Qt 5中,您可以在FontsLocation中使用FontsLocation。在Qt 4中,您将使用QDesktopServices::storageLocation。
发布于 2020-11-07 15:58:18
我需要做同样的事情,尽管我在Python中工作。我使用PyQt接口来选择字体,但是使用PIL绘制文本,并且PIL需要字体的文件路径(或者至少是文件名)来使用它。到目前为止,我只有一个总体的部分答案-希望我有什么你可以适应。
您可以做的是首先通过QStandardPaths获得字体所在位置的路径,然后使用QFontDatabase,在字体路径上遍历文件并通过addApplicationFont将其加载到数据库中。如果它加载,您将得到一个索引;然后将该索引提供给数据库的applicationFontFamilies函数。这只会给出字体族名,但是您可以使用它将名称映射到文件路径。
您无法通过此方法区分确切的字体(C:/Windows/ fonts /HTOWERT.TTF和C:/Windows/ fonts /HTOWERTI.TTF都返回相同的姓氏,但第二个是斜体),因此这不是一对一的映射,我甚至不确定它是否适用于非真类型字体(.ttf),但至少是一个开始。
下面是Python中的样子:
from PySide2.QtCore import QStandardPaths
from PySide2.QtGui import QFontDatabase
from PySide2.QtWidgets import QApplication
import sys, os
def getFontPaths():
font_paths = QStandardPaths.standardLocations(QStandardPaths.FontsLocation)
accounted = []
unloadable = []
family_to_path = {}
db = QFontDatabase()
for fpath in font_paths: # go through all font paths
for filename in os.listdir(fpath): # go through all files at each path
path = os.path.join(fpath, filename)
idx = db.addApplicationFont(path) # add font path
if idx < 0: unloadable.append(path) # font wasn't loaded if idx is -1
else:
names = db.applicationFontFamilies(idx) # load back font family name
for n in names:
if n in family_to_path:
accounted.append((n, path))
else:
family_to_path[n] = path
# this isn't a 1:1 mapping, for example
# 'C:/Windows/Fonts/HTOWERT.TTF' (regular) and
# 'C:/Windows/Fonts/HTOWERTI.TTF' (italic) are different
# but applicationFontFamilies will return 'High Tower Text' for both
return unloadable, family_to_path, accounted>>> app = QApplication(sys.argv)
>>> unloadable, family_to_path, accounted = getFontPaths()
>>> family_to_path['Comic Sans MS']
'C:/Windows/Fonts\\comic.ttf'我觉得有点奇怪,没有真正的方法来映射字体到他们的位置。我的意思是,在某种程度上,Qt需要知道字体在哪里才能使用它,对吗?
发布于 2022-11-23 03:32:08
可以根据QFont对象的字体系列检索所需的字体文件。
为了做到这一点,可以通过从pip安装matplotlib库来使用它:
pip install matplotlib然后,您可以从库中使用font_manager模块,并访问它的find_font函数,该函数将font family作为字符串访问:
Python:
from PyQt6 import QtGui
#QtGui.QFont() should be replaced with your QFont Object
QtGui.QFont().family()C++ (等价物)
QFont::family()接下来,您将从QFont对象中获取字体系列,并将其放入font_manager.find_font()方法中:
from matplotlib import font_manager
x = QtGui.QFont().family() #this is now a string
font_manager.find_font(x)
#this method now converted the font family string into the file path of the font总之,代码如下所示:
from PyQt6 import QtGui
from matplotlib import font_manager
#QtGui.QFont() should be replaced with your QFont Object
QtGui.QFont().family()
x = QtGui.QFont().family() #this is now a string
font_manager.find_font(x)
#this method now converted the font family string into the file path of the font下面是PyQt6中的一个实际例子:
from PyQt6 import QtGui, QtWidgets, QtCore
import os
#import matplotlib.font_manager as font_manager
from matplotlib import font_manager
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.fontComboBox = QtWidgets.QFontComboBox(self.centralwidget)
self.fontComboBox.setGeometry(QtCore.QRect(210, 220, 338, 32))
self.fontComboBox.setObjectName("fontComboBox")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 30))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
def your():
print(font_manager.findfont(ui.fontComboBox.currentFont().family()))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
your()
sys.exit(app.exec())我知道您可能不知道python,因为您正在使用C++,但是您可以研究如何在python中使用open()函数来编写包含文件路径的文本文件(或者创建一个jason文件,由您决定)。
我希望我在某种程度上帮助了你,请随便问我关于这件事的任何问题。即使这看上去很基本,也不要让这让你气馁。不管别人怎么说!
https://stackoverflow.com/questions/19098440
复制相似问题