[英]How to get text next to cursor in QTextEdit in PyQt4
我正在為bash腳本制作一個輕量級的IDE,我想要一個選項,當我按Ctrl + / >>一行時,“ textCursor”所在的位置將被注釋,而我設法做到了,但是我也想要用相同的命令取消注釋行。 但是我無法在“ 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()函數插入了“#”(注釋符號)。
注意:class繼承了QTextEdit ,這就是為什么我僅將它與self一起使用 。
綜上所述,我只需要一種方法來檢查textCursor旁邊的字符,如果該字符為“#”,我將其刪除,如果不是,則將插入新的“#”,將不勝感激。
您必須移至該行的開頭,然后跳過空格,選擇第一個字符並進行比較,然后對於刪除過程,您必須刪除"#"
和右邊的空格,對於插入,它是建議插入"# "
附言 :我無法嘗試使用快捷鍵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_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.