目前,QTextEdit允许选择文本,然后仅通过在锚点对面的选择一侧进行shift-click-drag来更改该选择。锚点放置在选择开始的位置。如果用户试图在开始附近改变选择,则选择围绕锚点而不是延伸。我想允许从任何一方改变选择。
我的第一个尝试是简单地在光标所在的另一侧设置锚点。例如,选择从10到20.如果光标在位置8处按住Shift键单击拖动,则锚点将设置为20.如果光标在位置22处按住Shift键单击拖动,则锚点将设置为10.稍后,我会尝试更强大的东西,可能基于选择的中心点。
我认为这段代码可行,但它似乎根本不会影响默认行为。我错过了什么?
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class TextEditor(QTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.setReadOnly(True)
self.setMouseTracking(True)
def mouseMoveEvent(self, event):
point = QPoint()
x = event.x() #these are relative to the upper left corner of the text edit window
y = event.y()
point.setX(x)
point.setY(y)
self.mousepos = self.cursorForPosition(point).position() # get character position of current mouse using local window coordinates
if event.buttons()==Qt.LeftButton:
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ShiftModifier:
start = -1 #initialize to something impossible
end = -1
cursor = self.textCursor()
select_point1 = cursor.selectionStart()
select_point2 = cursor.selectionEnd()
if select_point1 < select_point2: # determine order of selection points
start = select_point1
end = select_point2
elif select_point2 < select_point1:
start = select_point2
end = select_point1
if self.mousepos > end: # if past end when shift-click then trying to extend right
cursor.setPosition(start, mode=QTextCursor.MoveAnchor)
elif self.mousepos < start: # if before start when shift-click then trying to extend left
cursor.setPosition(end, mode=QTextCursor.MoveAnchor)
if start != -1 and end != -1: #if selection exists then this should trigger
self.setTextCursor(cursor)
super().mouseMoveEvent(event)
答案 0 :(得分:1)
这是实施当前选择的shift + click扩展的第一次尝试。它似乎工作正常,但我没有测试它死亡,所以可能有一两个故障。预期的行为是选择上方或下方的移位+点击应该在该方向上扩展整个选择;并且使用拖动移动+单击应该只是连续执行相同的操作。
请注意,我还设置了文本交互标记,以便插入符号在只读模式下可见,并且也可以通过键盘以各种方式操作选择(例如,ctrl + shift + right将选择范围扩展到下一个字)。
enter code here
$(document).ready(function () {
$(window).scroll(function () {
var wScroll = $(this).scrollTop();
//parallax for the main title
$('element you want to move').css({
'transform' : 'translateX('wScroll+'px)'
});
});
});