亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的 charFormat 樣式只適用于選擇,而且只適用于特定方向的選擇?

為什么我的 charFormat 樣式只適用于選擇,而且只適用于特定方向的選擇?

紅糖糍粑 2023-05-23 16:03:04
我一直在嘗試更明確地分配文本編輯器的字符格式,以便我可以了解我可以根據當前的技能范圍自定義哪些內容。雖然我的格式化方法的基本復制粘貼版本工作得很好,但下面的版本一直在工作,然后以令人沮喪的方式無法工作,需要幫助找出可能導致它的原因。該編輯器最初旨在成為通過文檔標簽設計樣式的所見即所得編輯器。Qt 對 Html 的混亂使用并沒有讓事情變得如此簡單。我的基本流程是提取當前格式的副本,檢查其當前狀態,反轉它,然后將格式重新應用到從中提取的位置或選擇。# textEdit is a QTextEdit with a loaded document.# This function is one of several related pairs called by a switchboard.# It's intent is to invert the italic state of the current position/selection.def toggle_italic_text(textEdit):    # Get the cursor, and the format's state at its current selection/position.    cursor = textEdit.textCursor()    charFormat = cursor.charFormat()    currentState = charFormat.fontItalic()    # Invert the state within the format.    print(currentState)    charFormat.setFontItalic(not currentState)    print(charFormat.fontItalic())    # Reapply the format to the cursor's current selection/position.    cursor.mergeCharFormat(charFormat)當我第一次實施它時,這很有效?,F在,它只適用于選擇,即便如此,它似乎也會根據我選擇的方向來識別錯誤的狀態。在試驗之后,似乎如果我向右進行選擇,它就會正確反轉。如果我在左側進行選擇,則不會。當嘗試將其分配到沒有選擇的位置時,打印狀態從 False 變為 True,這是需要的,但效果在我鍵入時不適用。如果我就地重復運行它,它會繼續從 False 變為 True,這意味著更改正在丟失。該函數被一致調用并完全運行。charFormat 副本的存儲狀態確實發生了變化。為什么這種模式停止工作了?我使用的 charFormats 錯了嗎?為什么選擇的方向會改變結果?就我這邊發生的變化而言,在需要通過 QFonts、QCharFormats、QPalette 和 CSS 樣式表(以及 doc.defaultStylesheet)同時針對小部件和 html 標簽來應用樣式之后,我在樣式設計工作中迷失了方向。我非常希望通過一種方法來控制我的樣式,但無法弄清楚層次結構或找到一種應用足夠廣泛的方法。最后,除了分配給窗口的樣式表之外,我刪除了所有內容。如果代碼本身沒有問題,我真的希望能得到一些提示,說明什么可能會破壞事情。我花了一段時間才習慣這樣的想法,即光標和格式是要更改和重新應用的副本,而文檔及其塊才是真正的結構。
查看完整描述

2 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

必須考慮的重要事項QTextCursor.charFormat()是:

返回光標position?(?)之前字符的格式。

因此,這不僅不能很好地處理包含多種字符格式的選擇,而且您還必須考慮光標位置,它可能會在選擇中發生變化:它可能在開頭(因此它會返回格式選擇之前的字符)或結尾(返回選擇中最后一個字符的格式)。

如果要根據當前光標位置反轉狀態(如果在開頭,則使用第一個字符,如果在結尾,則使用最后一個),則可以使用以下內容:


查看完整回答
反對 回復 2023-05-23
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

    def toggle_italic_text(self):

        cursor = self.textEdit.textCursor()

        if not cursor.hasSelection():

            charFormat = cursor.charFormat()

            charFormat.setFontItalic(not charFormat.fontItalic())

            cursor.setCharFormat(charFormat)

            # in this case, the cursor has to be applied to the textEdit to ensure

            # that the following typed characters use the new format

            self.textEdit.setTextCursor(cursor)

            return


        start = cursor.selectionStart()

        end = cursor.selectionEnd()

        newCursor = QtGui.QTextCursor(self.textEdit.document())

        newCursor.setPosition(start)


        if cursor.position() == start:

            cursor.setPosition(start + 1)

        charFormat = cursor.charFormat()

        charFormat.setFontItalic(not charFormat.fontItalic())

        newCursor.setPosition(end, cursor.KeepAnchor)

        newCursor.mergeCharFormat(charFormat)

如果要反轉選擇中的所有狀態,則需要循環遍歷所有字符。

雖然您可以只更改每個字符的 char 格式,但這對于非常大的選擇來說并不是一件好事,因此解決方案是僅在 char 格式實際從以前的狀態發生變化時應用斜體,并且在選擇結束。


    def toggle_italic_text(self):

        # ...

        start = cursor.selectionStart()

        end = cursor.selectionEnd()

        newCursor = QtGui.QTextCursor(self.textEdit.document())

        newCursor.setPosition(start)


        cursor.setPosition(start)

        prevState = cursor.charFormat().fontItalic()

        while cursor.position() < end:

            cursor.movePosition(cursor.Right)

            charFormat = cursor.charFormat()

            if charFormat.fontItalic() != prevState or cursor.position() >= end:

                newPos = cursor.position()

                if cursor.position() < end:

                    newPos -= 1

                newCursor.setPosition(newPos, cursor.KeepAnchor)

                charFormat.setFontItalic(not prevState)

                newCursor.mergeCharFormat(charFormat)

                prevState = not prevState

                newCursor.setPosition(cursor.position() - 1)


查看完整回答
反對 回復 2023-05-23
  • 2 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號