This example realizes setting font on the toolbar , Font size , Bold , Italics , The function of format attributes such as underline and font color , The code is as follows :

# -*- coding: utf-8 -*- from PyQt4.QtGui import * from PyQt4.QtCore import *
import sys QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8")) class
MainWindow(QMainWindow): def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
self.setWindowTitle(self.tr(" Set font , Font size and other format properties ")) self.text = QTextEdit()
self.setCentralWidget(self.text); # typeface self.toolBar=self.addToolBar("Font")
self.label1 = QLabel(self.tr(" typeface :")) self.fontBox = QFontComboBox()
self.fontBox.setFontFilters(QFontComboBox.ScalableFonts);
self.toolBar.addWidget(self.label1) self.toolBar.addWidget(self.fontBox) # Font size
self.label2 = QLabel(self.tr(" number :")) self.sizeBox = QComboBox() #print
dir(self.sizeBox) self.toolBar.addWidget(self.label2)
self.toolBar.addWidget(self.sizeBox) self.db = QFontDatabase() for i in
self.db.standardSizes(): self.sizeBox.addItem(QString.number(i))
self.toolBar.addSeparator(); # Bold , Italics , Underline , colour self.boldBtn = QToolButton()
self.boldBtn.setIcon(QIcon("image/bold.png")) self.boldBtn.setCheckable(True)
self.toolBar.addWidget(self.boldBtn) self.italicBtn = QToolButton()
self.italicBtn.setIcon(QIcon("image/italic.png"))
self.italicBtn.setCheckable(True) self.toolBar.addWidget(self.italicBtn)
self.underBtn = QToolButton()
self.underBtn.setIcon(QIcon("image/underline.png"))
self.underBtn.setCheckable(True) self.toolBar.addWidget(self.underBtn)
self.toolBar.addSeparator() self.colorBtn = QToolButton()
self.colorBtn.setIcon(QIcon("image/color.png"))
self.toolBar.addWidget(self.colorBtn) # Signal and slot functions
self.fontBox.activated.connect(self.slotFont)
self.sizeBox.activated.connect(self.slotSize)
self.boldBtn.clicked.connect(self.slotBold)
self.italicBtn.clicked.connect(self.slotItalic)
self.underBtn.clicked.connect(self.slotUnder)
self.colorBtn.clicked.connect(self.slotColor)
self.text.currentCharFormatChanged.connect(self.slotCurrentFormatChanged)
self.fmt = QTextCharFormat() def slotFont(self): self.f =
self.fontBox.currentFont().family() self.fmt.setFontFamily(self.f) self.cursor
= self.text.textCursor() if(self.cursor.hasSelection() is False):
self.cursor.select(QTextCursor.WordUnderCursor)
self.cursor.mergeCharFormat(self.fmt)
self.text.mergeCurrentCharFormat(self.fmt) def slotSize(self): self.num =
self.sizeBox.currentText() self.fmt.setFontPointSize(self.num.toFloat()[0])
self.text.mergeCurrentCharFormat(self.fmt) def slotBold(self): if
self.boldBtn.isChecked(): self.fmt.setFontPointSize(QFont.Bold) else:
self.fmt.setFontPointSize(QFont.Normal)
self.text.mergeCurrentCharFormat(self.fmt) def slotItalic(self): if
self.italicBtn.isChecked(): if self.fmt.fontItalic() is False:
self.fmt.setFontItalic(True) else: self.fmt.setFontItalic(False)
self.text.mergeCurrentCharFormat(self.fmt) def slotUnder(self):
self.fmt.setFontUnderline(self.underBtn.isChecked())
self.text.mergeCurrentCharFormat(self.fmt) def slotColor(self): self.color =
QColorDialog.getColor(Qt.red) if(self.color.isValid()):
self.fmt.setForeground(self.color) self.text.mergeCurrentCharFormat(self.fmt)
def slotCurrentFormatChanged(self):
self.fontBox.setCurrentIndex(self.fontBox.findText(self.fmt.fontFamily()))
self.sizeBox.setCurrentIndex(self.sizeBox.findText(QString.number(self.fmt.fontPointSize())))
self.boldBtn.setChecked(self.fmt.font().bold())
self.italicBtn.setChecked(self.fmt.fontItalic())
self.underBtn.setChecked(self.fmt.fontUnderline()) app=QApplication(sys.argv)
main=MainWindow() main.show() app.exec_()

Technology
©2019-2020 Toolsou All rights reserved,
( Essence )2020 year 8 month 15 day redis database StackExchange.Redis in Set type (C# edition )vue vue-element-admin Summary of the project vue-countTo Complete operation vue Value transfer between parent-child and non parent-child components The project followed for a year , The customer finally said no ( Essence )2020 year 6 month 26 day C# Class library read json Profile help class unity Get the car speed and forward or backward python Short circuit logic in Golang Array bisection , Array split , Array grouping ( Essence )2020 year 7 month 15 day Wechat applet template Use of