How to embed a pptk viewer in a PyQt5 window

后端 未结 3 796
灰色年华
灰色年华 2020-12-22 12:03

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 (

相关标签:
3条回答
  • 2020-12-22 12:22

    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_())
    
    0 讨论(0)
  • 2020-12-22 12:26

    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.

    0 讨论(0)
  • 2020-12-22 12:40

    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.

    0 讨论(0)
提交回复
热议问题