1

在添加小部件时,有关 vispy 的一些问题提到了使用 canvas.native。在 qt 设计器中作为占位符制作的小部件如何用于 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"))
4

2 回答 2

3

QFrame没有布局,所以必须设置布局,而run()方法是内部调用app.exec()的阻塞器,不需要再次调用。

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())
于 2019-08-13T13:35:43.687 回答
1

仅供参考,我在 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

于 2021-05-30T12:59:31.860 回答