How to embed a pptk viewer in a PyQt5 window

Question:

I am building a GUI program with PyQt5 (Qt Designer) which also uses the pptk library. This library can plot huge amount of points which is very interesting for my purpose (display finite element post processing results).

As it is explained in this post, the viewer class from pptk is a standalone window. Like the author of the previous post, I would like to embed the viewer in my GUI. It seems that I need to write some wrapper. After some research, I still don’t know if that means that I have to look inside the C++ code to re-write some stuff. In that case, it’ll be more complex than I thought and I’ll have to give up for the moment. In the end, if I could create a viewer widget that can be integrated inside my main window, it would be perfect.

Can someone please clarify for me what I have to go through?

Asked By: Foussy

||

Answers:

Here’s what I did from the begining to make it work :

# imports
from PyQt5 import QtWidgets, QtGui
import numpy as np
import pptk
import win32gui
import sys

# local imports
from designer import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)

        self.cloudpoint = np.random.rand(100, 3)
        self.v = pptk.viewer(self.cloudpoint)                # generate the viewer window
        hwnd = win32gui.FindWindowEx(0, 0, None, "viewer")   # retrieve the window ID of the viewer
        self.window = QtGui.QWindow.fromWinId(hwnd)          # get the viewer inside a window

        # embed the window inside the centralwidget of the MainWindow :
        self.windowcontainer = self.createWindowContainer(self.window, self.centralwidget)

        # finally, resize the container as you wish.
        self.windowcontainer.resize(self.width() - 100 , self.height() - 100)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    form = MainWindow()
    form.show()
    sys.exit(app.exec_())

the designer is as simple scratch from QtDesigner (QMainWindow with its QWidget centralwidget). I just saved the scratch, converted it in a .py file.

Here’s what I got :

Embedded pptk viewer

There’s still black bars on the sides of the windowcontainer, I haven’t discovered yet how to make them disappear.

Answered By: Foussy

Below is a demo script that shows how to add the viewer to a layout. I cannot test it on Windows, but on Linux (without the win32gui part), I get the results show below. As you can see, there is no weird border, and the window can be freely resized as normal.

enter image description here

from PyQt5 import QtWidgets, QtGui
import numpy as np
import pptk
import win32gui
import sys

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        widget = QtWidgets.QWidget()
        layout = QtWidgets.QGridLayout(widget)
        self.setCentralWidget(widget)

        self.cloudpoint = np.random.rand(100, 3)
        self.v = pptk.viewer(self.cloudpoint)
        hwnd = win32gui.FindWindowEx(0, 0, None, "viewer")
        self.window = QtGui.QWindow.fromWinId(hwnd)    
        self.windowcontainer = self.createWindowContainer(self.window, widget)

        layout.addWidget(self.windowcontainer, 0, 0)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    form = MainWindow()
    form.setWindowTitle('PPTK Embed')
    form.setGeometry(100, 100, 600, 500)
    form.show()
    sys.exit(app.exec_())
Answered By: ekhumoro

Found out how to overcome the black border issue. The PPTK viewer needs to be maximised before being embedded in PyQt, like so:

    hwnd = win32gui.FindWindowEx(0, 0, None, "viewer")
    win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
    window = QtGui.QWindow.fromWinId(hwnd)
    windowContainer = QtWidgets.QWidget.createWindowContainer(window)

And then just add windowContainer to the widget you’re displaying it in (as explained in other answers above). The second line is the key one which should solve the black border problem.

Answered By: Vikas Shenoy
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.