#-*-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 attributes "))
Self. Text = qtextedit ()
Self. setcentralwidget (self. Text );
# Font
Self. toolbar = self. addtoolbar ("font ")
Self. label1 = qlabel (self. tr ("Font :"))
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, italic, underline, and color
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.exe C _()
Pyqt learning: Setting font, font size, and other format attributes (provided by netizens)