首页 > 解决方案 > 在子类 QFontDialog 中获取选定的字体

问题描述

我正在尝试继承 QFontDialog 并希望检索所选字体的特征。如果我使用 getFont() 首先出现一个 QFontDialog 窗口,...我肯定做错了什么。

这是我的示例代码:

from PyQt5.QtWidgets import (QFontDialog, QPushButton, 
                            QMainWindow, QApplication, 
                            QTabWidget, QWidget, QVBoxLayout)

import sys

class FontSelection(QFontDialog) :
    def __init__(self, parent=None):
        super(FontSelection, self).__init__(parent)
        
        self.setOption(self.DontUseNativeDialog, True)
        
        self.bouton = self.findChildren(QPushButton)
        self.intitule_bouton = self.bouton[0].text().lower()
        self.ouvertureBouton = [x for x in self.bouton if self.intitule_bouton in str(x.text()).lower()][0]
        self.ouvertureBouton.clicked.disconnect()
        self.ouvertureBouton.clicked.connect(self.font_recup)
        
        
    def font_recup(self) :
        self.font_capture()
        
        
    def font_capture(self) :
        if self.intitule_bouton in ['ok', '&ok'] : 
            font, self.intitule_bouton = self.getFont()
            print(font)
            
            
class MainQFontDialogTry(QMainWindow):
    def __init__(self):
        super(MainQFontDialogTry, self).__init__()
    
        self.setWindowTitle('QFontDialog subclassed try')
    
        self.setGeometry(0, 0, 1000, 760)
        self.setMinimumSize(1000, 760)
    
        self.tab_widget = QTabWidget()
    
        self.win_widget_1 = FontSelection(self)

        widget = QWidget()

        layout = QVBoxLayout(widget)
    
        self.tab_widget.addTab(self.win_widget_1, "QFontDialog Tab")
    
        layout.addWidget(self.tab_widget)

        self.setCentralWidget(widget)
    
        self.qfont = FontSelection()
        self.qfont.font_recup()
    
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MainQFontDialogTry()
    ex.show()
    sys.exit(app.exec_())

标签: python-3.xpyqt5

解决方案


推荐阅读