Hey, it's been a while since I send this question, how can we get this
resolved ? (At least in PySide6 but would be awesome if backported to
PySide2, as a Maya develloper I have no choice than using PySide2 for now.)

Thank you ! :)

Le mer. 25 janv. 2023 à 21:45, Grégory Pijat <pijat.greg...@gmail.com> a
écrit :

> Hey pyside mailing list, it's my first time participating in a mailing
> list, I hope I'm at the right place for my question/bug report.
>
> *What I'm doing:*
> I'm working on a custom widget to display an immense amount of words
> (QPlainTextEdit is too slow for what I need) so here's how I'm doing it:
> - Custom Widget
> - QTextDocument
> - QTextDocument.draw with a QAbstractTextDocumentLayout.PaintContext
>
> *My Problem:*
> I need to implement selection, so I'm calculating which part of the text
> is selected by comparing press and release position in the document. This
> works fine, I can retrieve the selected text.
> BUT, when I want to highlight the selection using the `selections`
> argument that should be available in
> the QAbstractTextDocumentLayout.PaintContext instance, it fails, this seems
> to be missing.
>
> I tested this in PyQt too and it works fine, it just looks like it's
> missing from PySide. (I'm using PySide2).
>
> Here's my code:
> ```
>
> import sysfrom PyQt5 import QtWidgets, QtGui, QtCore
>
> LOREM_IPSUM = \'''
> Lorem ipsum dolor sit amet, consectetur adipiscing elit.
> Morbi dictum ligula mi, ut venenatis urna molestie et.
> Phasellus velit nunc, consequat nec felis ut, egestas pulvinar mi.
> In hac habitasse platea dictumst.
> Mauris eget faucibus est, vitae suscipit neque.
> Curabitur elementum tellus nec mauris rutrum laoreet.
> Duis condimentum consequat dui sit amet semper.
> Nam varius aliquet nibh, sit amet molestie ex scelerisque a.
> Donec sodales, sapien non consequat luctus, ante quam pretium nibh, sed 
> egestas augue nunc et augue.
> '''
>
> class TextEdit(QtWidgets.QWidget):
>     def __init__(self, document, parent=None):
>         super(TextEdit, self).__init__(parent=None)
>
>         self._document = document
>         self._selectionStart = 0
>         self._selectionEnd = 0
>
>         self.cur = QtGui.QTextCursor(self._document)
>
>         self._selectionFormat = QtGui.QTextCharFormat()
>         self._selectionFormat.setBackground(QtGui.QColor('#2B4A69'))
>
>     def paintEvent(self, event):
>         painter = QtGui.QPainter(self)
>
>         # This is the problematic area
>         ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
>         selection = QtGui.QAbstractTextDocumentLayout.Selection()
>         selection.cursor = self.cur
>         selection.format = self._selectionFormat
>         # print(ctx.selections)  # Works in PyQt5, not PySide2 where the 
> attribute is missing.
>         ctx.selections = [selection]
>
>         self._document.documentLayout().draw(painter, ctx)
>
>     def mousePressEvent(self, event):
>         start = end = self._document.documentLayout().hitTest(event.pos(), 
> QtCore.Qt.FuzzyHit)
>         self.cur.setPosition(start)
>         self.cur.setPosition(end, QtGui.QTextCursor.KeepAnchor)
>
>         self.update()
>
>     def mouseMoveEvent(self, event):
>         end = self._document.documentLayout().hitTest(event.pos(), 
> QtCore.Qt.FuzzyHit)
>         self.cur.setPosition(end, QtGui.QTextCursor.KeepAnchor)
>
>         self.update()
>
>
> if __name__ == "__main__":
>     if QtWidgets.QApplication.instance() is not None:
>         app = QtWidgets.QApplication.instance()
>     else:
>         app = QtWidgets.QApplication([])
>
>     doc = QtGui.QTextDocument()
>     doc.setPlainText(LOREM_IPSUM)
>
>     widget = TextEdit(doc)
>     widget.show()
>
>     sys.exit(app.exec_())```
>
> I've asked this on StackOverflow but I was told I should try in the mailing 
> list, so here am I.
> -> 
> https://stackoverflow.com/questions/75077964/qabstracttextdocumentlayout-paintcontext-works-in-pyqt5-but-not-pyside2?noredirect=1#comment132502605_75077964
>
> Looks like someone opened a bug report but was ignored:
> -> https://bugreports.qt.io/browse/PYSIDE-1220
> *Does anyone knows something about this ? Is it normal ? Is there a 
> workaround ?
> If it's a real bug, I guess it won't be fixed for PySide2, only in a future 
> release of PySide6 ? *
>
> Thank you ! :)
>
>
_______________________________________________
PySide mailing list
PySide@qt-project.org
https://lists.qt-project.org/listinfo/pyside

Reply via email to