#-*- 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_()
【PyQt实例4】设置文体排序和对齐【转】
最新推荐文章于 2024-03-27 09:10:10 发布