[PyQt] Here are some images... what widgets should I base the app around?

David Cortesi davecortesi at gmail.com
Wed Dec 16 02:15:57 GMT 2015

Date: Tue, 15 Dec 2015 11:49:01 -0800
> From: Brian Merchant <bhmerchant at gmail.com>
> To: pyqt at riverbankcomputing.com
> Subject: [PyQt]

> I have made some images to demo a text-editor like app I'd like to make;
> http://imgur.com/a/2OKph
> Some features are very familiar: type text to enter it, auto-complete
> boxes, etc

Things like this -- dynamic behavior based on the user's keystrokes in the
context of the text near the insertion point -- is done using a
QSyntaxHighlighter [1].

The main use of this is to highlight the different classes of tokens as the
user types them. For a simple example, to put a wavy red line underneath a
word that fails spellcheck. Or to make a keyword one color and a variable a
different color. Obviously this requires close linkage between the user's
keystrokes and the highlighter. The user types "fro" and that's a spelling
error, underline it; then the next keystroke is "g" and now it is not a
spelling error, clear the underline.

The relationship between a QSyntaxHighlighter and an editor is not well
explained in the doc. The Qt design is to make a syntax highlighter a
separate object from the editor, I suppose so that you could design it
separately, and apply one highlighter to multiple different text edit
widgets. Anyway you subclass QSyntaxHighlighter and specify its behaviors
by implementing .highlightBlock().

You apply a highlighter, making the highlights appear, by calling its
.setDocument() method, passing a QTextDocument. At this moment, the
highlightBlock() method is called for every existing line (or maybe only
the visible lines?). From then on, highlightBlock() is called for the line
with the insertion point whenever a character changes. To remove
highlighting, call its .setDocument() passing None.

I think using this you could implement your auto suggestion dropdown.
Although I couldn't say how to figure out the x/y coordinates of the
insertion point to locate the menu.

> Some features are not so familiar: you can drag and drop some elements of
> the text to "flip" their position, some keywords are automatically
> converted into symbols when typed, and so on

Qt's edit widgets (PlainTextEdit and TextEdit) support drag/drop of
selected text, complete with a nice transparent overlay of the dragged text
and moving insertion point to show where it will drop. You want to
implement this but make it behave based on lexical units, not merely based
on character-by-character moves. Read carefully through the QTextEdit doc
[2] about drag/drop of mime types. You might be able to,

1. Select the current lexical unit
2. Cut it
3. Make it a mime package
4. Start a drag/drop
5. Let Qt do its thing
6. Override the QTextEdit dragMoveEvent() to highlight lexical units under
the cursor showing where the dragged mime unit can legally go
6. Override the QTextEdit dropEvent() to refuse to drop in an illegal
place, and to animate moving lexical units around when it's legal.

However I have done some experiments with QDrag [3] and friends and found
it a very difficult API to work with.

> My plan is to use PyQt4.

Why?!? We are a couple of years into the Qt5 release after all.

Dave Cortesi

[1] http://doc.qt.io/qt-5/qsyntaxhighlighter.html

[2] http://doc.qt.io/qt-5/qtextedit.html#details

[3] http://doc.qt.io/qt-5/qdrag.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20151215/219d829a/attachment.html>

More information about the PyQt mailing list