将 vispy 与 pyqt5 和 uic 集成

Integrating vispy with pyqt5 and uic

关于 vispy 的一些问题提到在添加小部件时使用 canvas.native。在qt designer中作为占位符制作的小部件如何用于vispy?

这个想法是从这里开始的

canvas = vispy.app.Canvas()
w = QMainWindow()
widget = QWidget()
w.setCentralWidget(widget)
widget.setLayout(QVBoxLayout())
widget.layout().addWidget(canvas.native)
widget.layout().addWidget(QPushButton())
w.show()
vispy.app.run()

到一个版本,其中有一个 ui 或生成的 python 文件有一个名为 "frameFor3d" 的框架。

class myWindow(QtWidgets.QMainWindow):
    def __init__(self):

        super(myWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        #declare this here?
        canvas = vispy.app.Canvas()
        self.ui.frameFor3d.layout().addWidget(canvas.native)

if __name__ == '__main__': 
    app = QtWidgets.QApplication([])
    application = myWindow()
    vispy.app.run() #does it go here?
    application.show()
    sys.exit(app.exec())

这个错误 None 因为 layout() 是 none。

uic 样本

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'myWindow.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(440, 299)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.frameFor3d = QtWidgets.QFrame(self.centralwidget)
        self.frameFor3d.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frameFor3d.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frameFor3d.setObjectName("frameFor3d")
        self.gridLayout.addWidget(self.frameFor3d, 0, 0, 1, 1)
        self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setObjectName("horizontalSlider")
        self.gridLayout.addWidget(self.horizontalSlider, 1, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 440, 18))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

QFrame没有布局,所以必须设置布局,而运行()方法是内部调用app.exec()的blocker,所以没必要调用又来了。

class myWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(myWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        canvas = vispy.app.Canvas()
        lay = QtWidgets.QVBoxLayout(self.ui.frameFor3d) # create layout
        lay.addWidget(canvas.native)

if __name__ == '__main__': 
    app = QtWidgets.QApplication([])
    application = myWindow()
    application.show()
    vispy.app.run()
    # sys.exit(app.exec())

仅供参考,我在 vispy github 存储库中找到了两个与 pyqt 接口相关的更一般和详细的示例。

https://github.com/vispy/vispy/blob/main/examples/basics/scene/isocurve_for_trisurface_qt.py

https://github.com/vispy/vispy/blob/main/examples/demo/gloo/primitive_mesh_viewer_qt.py