【PyQt实例4】设置文体排序和对齐【转】

#-*- coding:utf-8 -*-
__author__ = 'shanshangzhiren'
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class myListAlign(QMainWindow):
    def __init__(self):
        super(myListAlign,self).__init__()

        self.setWindowTitle(self.tr("设置文体排序和对齐"))
        self.toolBar=self.addToolBar("List")

        self.label=QLabel(self.tr("列表:"))
        self.listBox=QComboBox(self.toolBar)#插入可选的排序方式
        self.listBox.addItem(self.tr("Standard"))
        self.listBox.addItem(self.tr("Bullet List (Disc)"))
        self.listBox.addItem(self.tr("Bullet List (Circle)"))
        self.listBox.addItem(self.tr("Bullet List (Square)"))
        self.listBox.addItem(self.tr("Ordered List (Decimal)"))
        self.listBox.addItem(self.tr("Ordered List (Alpha lower)"))
        self.listBox.addItem(self.tr("Ordered List (Alpha upper"))
        self.toolBar.addWidget(self.label)
        self.toolBar.addWidget(self.listBox)

        self.toolBar.addSeparator()

        self.actGrp=QActionGroup(self)#加入QActionGroup中的,互斥
        self.leftAction=QAction(QIcon("images/left.png"),self.tr("左"),self.actGrp)
        self.leftAction.setCheckable(True)

        self.centerAction=QAction(QIcon("images/center.png"),self.tr("局中"),self.actGrp)
        self.centerAction.setCheckable(True)

        self.justifyAction=QAction(QIcon("images/justify.png"),self.tr("两端对齐"),self.actGrp)
        self.justifyAction.setCheckable(True)

        self.rightAction=QAction(QIcon("images/right.png"),self.tr("右"),self.actGrp)
        self.rightAction.setCheckable(True)

        self.toolBar.addActions(self.actGrp.actions())

        self.editBar=self.addToolBar("Edit")
        self.undoAction=QAction(QIcon("images/undo.png"),self.tr("取消"),self)
        self.editBar.addAction(self.undoAction)
        self.redoAction=QAction(QIcon("images/redo.png"),self.tr("重做"),self)
        self.editBar.addAction(self.redoAction)

        self.text=QTextEdit()
        self.text.setFocus()
        self.setCentralWidget(self.text)

        self.connect(self.listBox,SIGNAL("activated(int)"),self.slotList)
        self.connect(self.leftAction,SIGNAL("triggered()"),self.slotLeftAction)
        self.connect(self.centerAction,SIGNAL("triggered()"),self.slotCenterAction)
        self.connect(self.justifyAction,SIGNAL("triggered()"),self.slotJustifyAction)
        self.connect(self.rightAction,SIGNAL("triggered()"),self.slotRightAction)
        self.connect(self.redoAction,SIGNAL("triggered()"),self.text,SLOT("redo()"))
        self.connect(self.undoAction,SIGNAL("triggered()"),self.text,SLOT("undo()"))
        self.connect(self.text.document(),SIGNAL("redoAvailable(bool)"),self.redoAction,SLOT("setEnabled(bool)"))
        self.connect(self.text.document(),SIGNAL("undoAvailable(bool)"),self.undoAction,SLOT("setEnabled(bool)"))
        self.connect(self.text,SIGNAL("cursorPositionChanged()"),self.slotCursorPositionChanged)#鼠标点中文本时,显示文本的对齐属性

    def slotLeftAction(self):
        self.text.setAlignment(Qt.AlignLeft)
        print("slotLeftAction")

    def slotCenterAction(self):
        self.text.setAlignment(Qt.AlignCenter)
        print("slotAlignCenter")

    def slotJustifyAction(self):
        self.text.setAlignment(Qt.AlignJustify)
        print("slotAlignJustify")

    def slotRightAction(self):
        self.text.setAlignment(Qt.AlignRight)
        print("slotAlignRight")

    def slotList(self,index):
        cursor=self.text.textCursor()
        if index!=0:
            style=QTextListFormat.ListDisc
            if index==1:
                style=QTextListFormat.ListDisc
            if index==2:
                style=QTextListFormat.ListCircle
            if index==3:
                style=QTextListFormat.ListSquare
            if index==4:
                style=QTextListFormat.ListDecimal
            if index==5:
                style=QTextListFormat.ListLowerAlpha
            if index==6:
                style=QTextListFormat.ListUpperAlpha
            cursor.beginEditBlock()
            blockFmt=cursor.blockFormat()
            listFmt=QTextListFormat()

            if cursor.currentList():
                listFmt=cursor.currentList().format()
            else:
                listFmt.setIndent(blockFmt.indent()+1)
                blockFmt.setIndent(0)
                cursor.setBlockFormat(blockFmt)

            listFmt.setStyle(style)
            cursor.createList(listFmt)
            cursor.endEditBlock()

        else:
            bfmt=QTextBlockFormat()
            bfmt.setObjectIndex(-1)
            cursor.mergeBlockFormat(bfmt)

    def slotCursorPositionChanged(self):
        if self.text.alignment()==Qt.AlignLeft:
            self.leftAction.setChecked(True)
        if self.text.alignment()==Qt.AlignCenter:
            self.centerAction.setChecked(True)
        if self.text.alignment()==Qt.AlignJustify:
            self.justifyAction.setChecked(True)
        if self.text.alignment()==Qt.AlignRight:
            self.rightAction.setChecked(True)
        print("slotCursorPositionChanged")

if __name__=="__main__":
    app=QApplication(sys.argv)
    main=myListAlign()
    main.show()
    app.exec_()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值