QT开发中文网

Qt中的字体对话框实例

下面是一个简单的Qt字体对话框实例:

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QFontDialog, QLabel

class FontDialogExample(QWidget):
    def __init__(self):
        super().__init__()
        
        # 创建一个垂直布局
        layout = QVBoxLayout(self)
        
        # 创建一个标签,用于显示当前选中的字体信息
        self.label = QLabel("Hello World!", self)
        layout.addWidget(self.label)
        
        # 创建一个按钮,点击后弹出字体对话框
        button = QPushButton("Select Font", self)
        button.clicked.connect(self.showFontDialog)
        layout.addWidget(button)
        
    def showFontDialog(self):
        # 弹出字体对话框,并获取用户选择的字体
        font, ok = QFontDialog.getFont()
        
        # 如果用户选择了字体,则更新标签的字体信息
        if ok:
            self.label.setFont(font)

if __name__ == '__main__':
    app = QApplication([])
    window = FontDialogExample()
    window.show()
    app.exec_()

运行程序后,点击“Select Font”按钮即可弹出字体对话框,在对话框中选择好字体后点击“OK”按钮即可将所选字体应用到标签上。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=3427

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?