1

我想将 selectedPart 的文本设置为粗体。我想根据角色的格式来做到这一点。使用我的代码,我可以检测到该格式,但设置粗体不起作用。如果我取消注释那 2 行注释,并注释下面的所有行(当然接受最后 5 行),我可以将代码设置为粗体。

这段代码完全可以工作,你可以测试一下,也许你有一个想法。您通过鼠标选择文本,然后使用 CTRL+B 调用 set-bold-method。

(我对此的想法:问题似乎取决于我移动 QTextCursor 以获取有关选择另一端的字符格式的信息这一事实。也许有一种更简单的方法来获取该信息,但我只是想通了这个。但我不明白为什么 textCursor 的移动不好,因为它似乎仍然有一个有效的选择,我假设 font=self.currentFont() 仍然可以工作。)

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtCore, QtGui
import sys,os

class test(QTextBrowser):
    def __init__(self, parent=None):
        super(QTextBrowser, self).__init__(parent=None)
        self.setHtml("""
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII<br>IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII<br>
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII<br>IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII<br>
IIIIIIIIIIIII<b>IIIIIIIIIIIIIIIIIIIIIIII<br>IIIIIIIIIIIIIIIIIII</b>IIIIIIIIIIIIIIIIIIIIII<br>
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII<br>IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII<br>
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII<br>IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII<br>
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
""")
        self.setReadOnly(False)
        self.resize(400,400)
        self.keylist=[]
        self.installEventFilter(self)        
    def setBold(self):       
            font  = self.currentFont()
            cur=self.textCursor()
##            font.setBold(True)
##            self.setCurrentFont(font)

            if cur.hasSelection():
                print("Before: there is selected text.")
            else:
                print("Before: there is NO selected text.")

            print("The anchor position is "+str(self.textCursor().anchor()))
            print("The cursor position is "+str(self.textCursor().position()))

            anchorPos=cur.anchor()
            currentPos=cur.position()

            if anchorPos>currentPos:
                direction="rtl" #rigth to left selection
            else:
                direction="ltr" #left to right selection

            print("The selection direction is "+str(direction))
            oldPosition=cur.position()

            if direction=="ltr":
                selectedTextEnd = cur.charFormat()
                cur.setPosition(cur.anchor(), QtGui.QTextCursor.KeepAnchor)
                self.setTextCursor(cur)
                selectedTextBeginning = cur.charFormat()

            else:
                selectedTextBeginning = cur.charFormat()
                cur.setPosition(cur.anchor(), QtGui.QTextCursor.KeepAnchor)
                self.setTextCursor(cur)
                selectedTextEnd = cur.charFormat()

            cur.setPosition(oldPosition, QtGui.QTextCursor.KeepAnchor)

            if cur.hasSelection():
                print("After: there is selected text.")
            else:
                print("After: there is NO selected text.")

            print("fontWeight begin = "+str(selectedTextBeginning.fontWeight()  )  )
            print("fontWeight end = "+str(selectedTextEnd.fontWeight()  )  )

            if selectedTextBeginning.fontWeight()==50 and selectedTextEnd.fontWeight()==75:
                print("Check 1")
                font.setBold(False)
                self.setCurrentFont(font)
            if selectedTextBeginning.fontWeight()==75 and selectedTextEnd.fontWeight()==75:
                print("Check 2")
                font.setBold(False)
                self.setCurrentFont(font)
            if selectedTextBeginning.fontWeight()==50 and selectedTextEnd.fontWeight()==50:
                print("Check 3")
                font.setBold(True)
                self.setCurrentFont(font)
            if selectedTextBeginning.fontWeight()==75 and selectedTextEnd.fontWeight()==50:
                print("Check 4")
                font.setBold(True)
                self.setCurrentFont(font)

            cur.setPosition(oldPosition, QtGui.QTextCursor.MoveAnchor)

    def eventFilter(self, target, event):            
        if event.type()==QEvent.KeyPress: 
            if "Ctrl" in self.keylist:
                self.keylist.append(str(event.key()))                
            if event.key()==Qt.Key_Control:
                self.keylist.append("Ctrl")
        return False

    def processmultikeys(self,keyspressed):
        if keyspressed[0]=="Ctrl" and keyspressed[1]=="66": #ctrl and B
            self.setBold()

    def keyReleaseEvent(self,event):
        if len(self.keylist)==2:
            self.processmultikeys(self.keylist)
            del self.keylist[-1]
        else:
            self.keylist=[]

if __name__ == '__main__':
    app=QApplication(sys.argv)
    a = test()
    a.show()
    app.exec_()
4

0 回答 0