简体   繁体   中英

Using SCI_SEARCHINTARGET in Qscintilla/PyQt4

I'm trying to search for the position of a string in a Qscintilla window.

Here's a piece of (runnable) mock code:

import sys
from PyQt4 import QtGui, Qsci

app = QtGui.QApplication(sys.argv)
window = Qsci.QsciScintilla()
window.show()

# Search in target for string
text = "Hello world"
window.setText(text)
messenger = window.SendScintilla
messenger(window.SCI_SETTARGETSTART, 0)
messenger(window.SCI_SETTARGETEND, len(text))
pos = messenger(window.SCI_SEARCHINTARGET, len(text), "world")
print(pos);

app.exec_()

It searches for the string "world" in a text editor window currently holding the string "Hello world" . However the search returns -1 (failed to find string), when the string clearly exists in the text editor window.

What might the problem be?

See http://www.scintilla.org/ScintillaDoc.html#SCI_SEARCHINTARGET : The length parameter is the length of the text you're searching for, not of the text you're searching in.

This will get you what you want:

pos = messenger(window.SCI_SEARCHINTARGET, len("world"), "world")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM