[PyQt] move view to first occurence of searched string

David Cortesi davecortesi at gmail.com
Mon Jul 28 16:42:53 BST 2014


> Message: 2
> Date: Mon, 28 Jul 2014 12:22:50 +0200
> From: Peter Irbizon <peterirbizon at gmail.com>
> To: pyqt <pyqt at riverbankcomputing.com>
> Subject: [PyQt] move view to first occurence of searched string
> Hello,
>
> I am displaying website with pyqt. Now I need to display/move to that part
> of site where custom text is present. Something like when I click on ctrl+f
> in browsers, write string to search and it moves me to first occurence of
> this string.
>
> How can I achieve this? Is there any sample code I can inspire from?
>

There does not appear to be a simple example of using QWebView in the Qt
docs.

In summary, you must subclass QWebView and

* supply a keyPressEvent() handler which detects ^f.

* On ^f put up a modal dialog (QInputDialog) to get some text to look for.
Alternatively, you could query QWebView.selectedText() to get text the user
has selected already, and search for that, put up the dialog only if that
is null.

* Pass that text to QWebView.findText() to select the next occurrence.
Pass FindWrapsAroundDocument to wrap around. Now if the user hits ^f again,
the found text is selected, so this is find-again. User has to click to
clear selection to start new find.

* If the findText() call fails, do something like beep to show it failed.

Good luck,
Dave Cortesi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140728/31ffd32c/attachment.html>


More information about the PyQt mailing list