Skip to content

Instantly share code, notes, and snippets.

@naquad
Last active May 21, 2024 15:14
Show Gist options
  • Save naquad/3f8edeeb9b6c2575a4ecd899582fef60 to your computer and use it in GitHub Desktop.
Save naquad/3f8edeeb9b6c2575a4ecd899582fef60 to your computer and use it in GitHub Desktop.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QWidget, QVBoxLayout, QLabel, QTextBrowser
from PyQt5.QtGui import QTextCursor, QTextCharFormat, QFont, QTextImageFormat, QTextObjectInterface, QPainter, QTextFormat, QWindow
from PyQt5.QtCore import QSize, QSizeF, QObject, QPoint
from subprocess import Popen, PIPE, STDOUT
class EmbeddedWinFormat(QObject, QTextObjectInterface):
def __init__(self, text_edit, widget):
super().__init__()
self.text_edit = text_edit
self.widget = widget
def _sizeHint(self):
width = self.text_edit.width()
return QSize(width, max(100, self.widget.height()))
def drawObject(self, painter, rect, doc, posInDocument, format):
self.widget.resize(self._sizeHint())
self.widget.render(painter, QPoint(int(rect.x()), int(rect.y())))
self.widget.move(int(rect.x()) + 1, int(rect.y()) + 1)
def intrinsicSize(self, doc, posInDocument, format):
return QSizeF(self._sizeHint())
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.counter = QTextFormat.UserObject + 1
self.setWindowTitle("QTextEdit Widget Embedding Example")
self.setMinimumSize(1024, 768)
self.text_edit = QTextEdit()
self.setCentralWidget(self.text_edit)
self.text_edit.append("Embedded widget")
win_id = self.start_client()
self.embed_widget_into_text_edit(self._embed_window(win_id))
self.text_edit.append("\nNext line")
def start_client(self):
p = Popen(['python3', '-u', 'test-window.py'], stdout=PIPE, stderr=STDOUT, text=True)
win_id = int(p.stdout.readline().split()[-1].strip())
self.destroyed.connect(lambda *_: p.kill())
return win_id
def _embed_window(self, win_id):
external_window = QWindow.fromWinId(win_id)
external_window.show()
return QWidget.createWindowContainer(external_window, self.text_edit)
def embed_widget_into_text_edit(self, widget):
cursor = self.text_edit.textCursor()
cursor.movePosition(QTextCursor.End)
self.counter += 1
fmt_id = self.counter
self.text_edit.document().documentLayout().registerHandler(
fmt_id,
EmbeddedWinFormat(self.text_edit, widget)
)
cursor.insertBlock()
widget_format = QTextCharFormat()
widget_format.setObjectType(fmt_id)
cursor.insertText(chr(0xfffc), widget_format)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment