[PyQt] Snippet to embed a widget in an editor.

David Boddie david at boddie.org.uk
Tue Jul 19 00:59:11 BST 2011


On Sun Jul 17 04:57:00 BST 2011, Phlip wrote:

> Hi, group. I'm trying to embed an object into a QTextEdit which is NOT
> (shocked gasp!) an SVG image.
>
> Foundational for embedding a panel, I'm trying to embed a widget. But
> I can't seem to get the size right. Here's the sauce:

[...]

> If you ran that, you'd get:
>
>    previously
>    subsequently
>    [I am here!]
>
> Presumably if the size were set right, the editor would push down the
> "subsequently".

The size is a null size, so I'm guessing the text engine just ignores it.

I changed the code for the text object to use the widget's size hint.

  class _TextObject(QtGui.QPyTextObject):

      def intrinsicSize(self, doc, posInDocument, format):

          size = widget.sizeHint()
          return QtCore.QSizeF(size)

      def drawObject(self, painter, rect, doc, posInDocument, format):
          widget.resize(widget.sizeHint())
          widget.render(painter, QtCore.QPoint(int(rect.x()), int(rect.y())))

I also made the widget resize itself to its size hint before rendering it.
I'm not sure why that is necessary.

> Contrarily, maybe I'm setting the size correctly, but then screwing
> something up with the cursors.

No, I think it's the size. The above code should at least get you started.

David


More information about the PyQt mailing list