[英]How to get text next to cursor in QTextEdit in PyQt4
I am making a light IDE for bash scripts, and i want an option, when i press Ctrl + / >> one line, where my 'textCursor' is, will be commented, and i have managed to do that, but i also want to uncomment line with the same command. 我正在为bash脚本制作一个轻量级的IDE,我想要一个选项,当我按Ctrl + / >>一行时,“ textCursor”所在的位置将被注释,而我设法做到了,但是我也想要用相同的命令取消注释行。 But i cannot get text next to '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("#")
This part of the code moves my textCursor to the start of that line where I insert '#' (comment sign) with self.insertPlainText() function. 这部分代码将我的textCursor移动到该行的开头,在此行中我使用self.insertPlainText()函数插入了“#”(注释符号)。
note: class is inheriting QTextEdit and that's why i am using it only with self . 注意:class继承了QTextEdit ,这就是为什么我仅将它与self一起使用 。
To sum up, i just need an method to check character next to textCursor and if that character is '#' i will remove it, and if it is not, then i will insert new '#' any help would be appretiated. 综上所述,我只需要一种方法来检查textCursor旁边的字符,如果该字符为“#”,我将其删除,如果不是,则将插入新的“#”,将不胜感激。
You must move to the beginning of the line, then skip the blank spaces, select the first character and compare it, then for the removal process you must remove the "#"
and the blank spaces to the right, and for the insertion it is recommended to insert "# "
您必须移至该行的开头,然后跳过空格,选择第一个字符并进行比较,然后对于删除过程,您必须删除"#"
和右边的空格,对于插入,它是建议插入"# "
Ps : I could not try the Shortcut Ctrl + / for what I use: Ctrl + R 附言 :我无法尝试使用快捷键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.