码迷,mamicode.com
首页 > 其他好文 > 详细

pyqt学习之设置字体,字号等格式属性(网友提供)

时间:2014-10-13 13:20:09      阅读:2658      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   ar   for   sp   on   cti   log   

# -*- 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_()

 

pyqt学习之设置字体,字号等格式属性(网友提供)

标签:style   color   io   ar   for   sp   on   cti   log   

原文地址:http://www.cnblogs.com/mhxy13867806343/p/4021863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!