티스토리 뷰

파이썬

[pyqt5] fontcombobox 사용 예제

헬로르르 2018. 5. 22. 22:01
요약드리자면

self.fontComboBox.currentFontChanged.connect(lambda x : self.plainTextEdit.setFont(x))

한줄이 핵심입니다. fontcombobox의 시그널과 plaintextedit의 슬롯만 연결해주면 됩니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'fontcombobox.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
 
__author__ = "seounghun, chung <4uwingnet@naver.com>"
 
from PyQt5 import QtCore, QtGui, QtWidgets
 
class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(320240)
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.verticalLayout.addWidget(self.plainTextEdit)
        self.fontComboBox = QtWidgets.QFontComboBox(Form)
        
        # ScalableFonts 를 해줘야 QT Warning이 발생하지 않는다. (QT5 bug)
        self.fontComboBox.setFontFilters(QtWidgets.QFontComboBox.ScalableFonts) 
        
        font = QtGui.QFont()
        font.setFamily("Consolas")
        self.fontComboBox.setCurrentFont(font)
        self.fontComboBox.setObjectName("fontComboBox")
        self.verticalLayout.addWidget(self.fontComboBox)
 
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        
        # fontcombobox 의 값이 변했을 때 시그널을, plainTextEdit의 setfont 슬롯과 연결
        self.fontComboBox.currentFontChanged.connect(lambda x : self.plainTextEdit.setFont(x)) 
        
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form""Form"))
        self.plainTextEdit.setPlainText(_translate("Form""Test Sample\n"
"Font change"))
 
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())
 
 
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함