You can retrieve the font file that you want based on the font family of the QFont
object
in order to do this you can use the matplotlib
library by installing it from pip:
pip install matplotlib
then you can use the font_manager
module from the library and access its find_font
function that takes in the font family
as a string accessed by:
Python:
from PyQt6 import QtGui
#QtGui.QFont() should be replaced with your QFont Object
QtGui.QFont().family()
C++ (equivelant)
QFont::family()
Next you would get the font family from the QFont
object and put it in the font_manager.find_font()
method:
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
all in all the code looks like this:
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
Here is a real life example of this in 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())
I know that you might not know python
as you are using C++
but you can research how to use the open()
function in python
to write a text file containing the file path (or make a jason file, its up to you).
I hope I helped you in some way, feel free to ask me any question about this. Even if it may seem basic, don't let that discourage you. No matter what others say!
_stripView->setFont(fontpath)
? Does it throw an error message? – Roulers