How to embed a pptk viewer in a PyQt5 window

不羁岁月 提交于 2019-12-20 05:48:12

问题


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?


回答1:


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.

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_())



回答2:


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 :

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



来源:https://stackoverflow.com/questions/58816766/how-to-embed-a-pptk-viewer-in-a-pyqt5-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!