【问题标题】:How to get text next to cursor in QTextEdit in PyQt4如何在 PyQt4 中的 QTextEdit 中获取光标旁边的文本
【发布时间】:2018-09-28 15:26:30
【问题描述】:

我正在为 bash 脚本制作一个轻量级 IDE,我想要一个选项,当我按下 Ctrl + / >> 一行时,我的“textCursor”是, 将会 评论了,我已经做到了,但我也想取消评论 使用相同的命令。但我无法在旁边获取文字 '文本光标'。

def commentShortcut(self):
    cursor = self.textCursor()
    Y = cursor.blockNumber()
    self.moveCursor(QtGui.QTextCursor.End)
    cursor = QtGui.QTextCursor(self.document().findBlockByLineNumber(Y))
    self.setTextCursor(cursor)
    self.insertPlainText("#")

这部分代码将我的 textCursor 移动到该行的开头 我用 self.insertPlainText() 插入“#”(注释符号) 功能。

注意:类正在继承 QTextEdit,这就是我的原因 仅与 self 一起使用。

总而言之,我只需要一个方法来检查 textCursor 旁边的字符 如果那个字符是'#',我会删除它,如果不是,那么 我将插入新的 '#' 任何帮助将不胜感激。

【问题讨论】:

    标签: python pyqt pyqt4 qtextedit


    【解决方案1】:

    你必须移动到行首,然后跳过空格,选择第一个字符并比较它,然后在删除过程中你必须删除"#"和右边的空格,对于插入建议插入"# "

    Ps:我无法尝试使用快捷键 Ctrl + /Ctrl + R

    import sys
    from PyQt4 import QtCore, QtGui
    
    class TextEdit(QtGui.QTextEdit):
        def __init__(self, *args, **kwargs):
            QtGui.QWidget.__init__(self, *args, **kwargs)
            shortcut = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+R"), self)
            shortcut.activated.connect(self.commentShortcut)
    
        def commentShortcut(self):
            pos = self.textCursor().position()
            self.moveCursor(QtGui.QTextCursor.StartOfLine)
            line_text = self.textCursor().block().text()
            if self.textCursor().block().text().startswith(" "):
                # skip the white space
                self.moveCursor(QtGui.QTextCursor.NextWord)
            self.moveCursor(QtGui.QTextCursor.NextCharacter,QtGui.QTextCursor.KeepAnchor)
            character = self.textCursor().selectedText()
            if character == "#":
                # delete #
                self.textCursor().deletePreviousChar()
                # delete white space 
                self.moveCursor(QtGui.QTextCursor.NextWord,QtGui.QTextCursor.KeepAnchor)
                self.textCursor().removeSelectedText()
            else:
                self.moveCursor(QtGui.QTextCursor.PreviousCharacter,QtGui.QTextCursor.KeepAnchor)
                self.textCursor().insertText("# ")
            cursor = QtGui.QTextCursor(self.textCursor())
            cursor.setPosition(pos)
            self.setTextCursor(cursor)
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        w = TextEdit()
        w.append('''
    
    #!/bin/bash
    # Simple line count example, using bash
    #
    # Bash tutorial: http://linuxconfig.org/Bash_scripting_Tutorial#8-2-read-file-into-bash-array
    # My scripting link: http://www.macs.hw.ac.uk/~hwloidl/docs/index.html#scripting
    #
    # Usage: ./line_count.sh file
    # -----------------------------------------------------------------------------
    
    # Link filedescriptor 10 with stdin
    exec 10<&0
    # stdin replaced with a file supplied as a first argument
    exec < $1
    # remember the name of the input file
    in=$1
    
    # init
    file="current_line.txt"
    let count=0
    
    # this while loop iterates over all lines of the file
    while read LINE
    do
        # increase line counter 
        ((count++))
        # write current line to a tmp file with name $file (not needed for counting)
        echo $LINE > $file
        # this checks the return code of echo (not needed for writing; just for demo)
        if [ $? -ne 0 ] 
         then echo "Error in writing to file ${file}; check its permissions!"
        fi
    done
    
    echo "Number of lines: $count"
    echo "The last line of the file is: `cat ${file}`"
    
    # Note: You can achieve the same by just using the tool wc like this
    echo "Expected number of lines: `wc -l $in`"
    
    # restore stdin from filedescriptor 10
    # and close filedescriptor 10
    exec 0<&10 10<&-
            ''')
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 2021-03-29
      • 2016-03-26
      相关资源
      最近更新 更多