1. 程式人生 > >【PyQt例項3】設定字型,字號等格式屬性

【PyQt例項3】設定字型,字號等格式屬性

  本例項實現了在工具欄上設定字型,字號大小,加粗,斜體,下劃線以及字型顏色等格式屬性的功能,程式碼如下:

# -*- 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("設定字型,字號等格式屬性"))
        self.text = QTextEdit()
        self.setCentralWidget(self.text);
        
        #字型
        self.toolBar=self.addToolBar("Font")
        self.label1 = QLabel(self.tr("字型:"))
        self.fontBox = QFontComboBox()
        self.fontBox.setFontFilters(QFontComboBox.ScalableFonts);
        self.toolBar.addWidget(self.label1)
        self.toolBar.addWidget(self.fontBox)
        #字號
        self.label2 = QLabel(self.tr("數字:"))
        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();
        
        #加粗 、斜體、下劃線、顏色
        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)
        
        #訊號和槽函式
        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_()