[PyQt] Positioning autocompletion text after QLineEdit

John F Sturtz john at sturtz.org
Sat Mar 9 22:21:11 GMT 2019


Hi again all.

I have a QTableView with QLineEdit editor delegate.  I'm trying to implement autocompletion -- basically, the user types some input into the editor widget, and the auto-completed text shows up to the right of what the user has typed.

(maurizio.berti at gmail.com, if you're reading this, you'll probably recognize it.  I finally decided to implement this using separate widgets, QLineEdit for the user input and QLabel for the auto-completion, instead of using a QTextEdit for all of it per your suggestion.  I realize there is a QCompleter class that can be used with a QLineEdit, but I'm intent on implementing it myself.)

The issue is that the QLabel doesn't position itself properly at the end of what the user has typed in.  I am using fontMetrics().horizontalAdvance(text) on the text the user has typed in, to determine where to position the Qlabel.  According to the documentation, that should be "the distance appropriate for drawing a subsequent character after text."  But it comes out too close to the preceding text.

Screen shots of the behavior I'm getting are attached.  Code is also attached (note that you have to have model.py in the same directory as test.py, to provide the data model for the view).  To see the behavior, run test.py and type abcdefghijkl in any of the cells.

Wondering if anyone has an idea why this isn't working the way I expect?  I thought horizontalAdvance() was going to be just the right tool for this job ...

Thanks again.

/John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190309/245c31a9/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.py
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190309/245c31a9/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: model.py
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190309/245c31a9/attachment-0001.ksh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Image 2.png
Type: image/png
Size: 2912 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190309/245c31a9/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Image 1.png
Type: image/png
Size: 3422 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190309/245c31a9/attachment-0001.png>


More information about the PyQt mailing list