PDA

View Full Version : multiple cursor selection



vuletic
19th March 2015, 15:47
Hello. How can I achieve multiple cursor selection, like here :

http://i.imgur.com/PF6sX2v.png

so I can rewrite everything that is found.

So far I got this:

http://i.imgur.com/wY66D5l.png ( can only rewrite last word )

I'm using plainTextEdit widget.

wysota
19th March 2015, 20:58
QPlainTextEdit::extraSelections() and friends.

vuletic
19th March 2015, 22:03
Here is code I use:




word = self.lineEdit.text()

extraSelections = []

self.plainTextEdit.moveCursor(QtGui.QTextCursor.St art)
while(self.plainTextEdit.find(word,QtGui.QTextDocu ment.FindWholeWords)):
'''
selection = QtWidgets.QTextEdit.ExtraSelection()
selection.format.setProperty(QtGui.QTextFormat.Ful lWidthSelection, True)
selection.cursor = self.plainTextEdit.textCursor()
selection.cursor.clearSelection()
extraSelections.append(selection)
'''
cursor = self.plainTextEdit.textCursor()
cursor.select(QtGui.QTextCursor.WordUnderCursor)
currentWord = QtWidgets.QTextEdit.ExtraSelection()
Color = QtGui.QColor(191, 191, 191, 189)
currentWord.format.setBackground(Color)
currentWord.cursor = cursor
extraSelections.append(currentWord)

self.plainTextEdit.setExtraSelections(extraSelecti ons)
self.plainTextEdit.setFocus()

What am I doing wrong?

wysota
19th March 2015, 23:24
What is the effect you are getting? How many items does extraSelections have before line #22? Is the cursor for each of the selections set properly?

vuletic
19th March 2015, 23:37
You can see on second picture what effect I'm getting, if I'm reading this right for(test example) I have

[<PyQt5.QtWidgets.ExtraSelection object at 0x7f25924b1dd8>, <PyQt5.QtWidgets.ExtraSelection object at 0x7f25924b1c88>, <PyQt5.QtWidgets.ExtraSelection object at 0x7f25924b1eb8>]

3 items in extraSelections

wysota
19th March 2015, 23:46
That would probably mean your find() call is incorrect. Maybe you should use QTextDocument::find() instead?

vuletic
20th March 2015, 00:29
No matter what I try every word is painted correctly, but only last word found is selected.

wysota
20th March 2015, 12:07
That's because "extra selections" are not real selections. The logic of your program should update all cursors at the same time while you are performing the edit.

vuletic
20th March 2015, 12:15
Since english is not my native language, maybe I didn't explain it right, but if you look at these two videos:

this is what I want:
https://www.youtube.com/watch?v=Ux3_B2TgmUU&feature=youtu.be

and this is what I have at this moment:

https://www.youtube.com/watch?v=NouOQQSLRqQ&feature=youtu.be


the logic of your program should update all cursors at the same time while you are performing the edit.

that's what I wish to achieve, but I don't know how.

wysota
20th March 2015, 16:37
QTextCursor API can be used to edit the document. So if you want to enter "foo" in 10 places, you need to have cursors set to each of the place and use its API to insert "foo" through each cursor.