QUiLoader在PySide中与自定义小部件崩溃

时间:2014-04-24 18:58:30

标签: python qt qt-creator pyside pyqtgraph

我正在尝试将PySide PyQtGraph小部件嵌入到使用QT Creator创建的GraphicsView窗口中。但是,当我导入UI文件并使用"升级到"时会发生分段错误。 QT Creator中的功能。

之前已经观察到此错误,并且已经提出了覆盖QUiLoader的createWidget()的解决方法:

http://www.mail-archive.com/pyside@qt-project.org/msg00306.html

Calling custom class in .ui file fails

但是,当我实施变通方法时,我仍然会遇到分段错误。

有谁知道为什么这个解决方法在我的代码中似乎不起作用?或者是否有另一种在动态导入UI文件时嵌入PyQtGraph的方法?

非常感谢,

塔可

一些示例代码:

#!/usr/bin/python
import os
import sys

from PySide.QtUiTools import QUiLoader


import pyqtgraph as pg

SCRIPT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))

class UiLoader(QUiLoader):
    def __init__(self, baseinstance):
        QUiLoader.__init__(self, baseinstance)
        self.baseinstance = baseinstance

    def createWidget(self, className, parent = None, name = ""):
        if className in QUiLoader.availableWidgets(self):
            widget = QUiLoader.createWidget(self, className, parent, name)
        else:
            if hasattr(self.baseinstance, "customWidgets"):
                if className in self.baseinstance.customWidgets.keys():
                    widget = self.baseinstance.customWidgets[className](parent)
                else:
                    raise KeyError("Unknown widget '%s'" % className)
            else:
                raise AttributeError("Trying to load custom widget '%s', but base instance '%s' does not specify custom widgets." % (className, repr(self.baseinstance)))

        if self.baseinstance is not None:
            setattr(self.baseinstance, name, widget)

        return widget


def loadUi(uifile, baseinstance=None):
    loader = UiLoader(baseinstance)
    loader.registerCustomWidget(pg.PlotWidget)
    widget = loader.load(uifile)
    QMetaObject.connectSlotsByName(widget)
    return widget

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.ui = loadUi(os.path.join(SCRIPT_DIRECTORY, 'example1.ui'), self)
        self.ui.plotBtn.clicked.connect(self.PlotTest)

    def PlotTest(self):
        self.ui.plottest.plot(np.random.normal(size=50), clear=True)

def main():
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

UI文件:example1.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="PlotWidget" name="centralwidget">
   <widget class="QGraphicsView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>30</y>
      <width>761</width>
      <height>471</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="plotBtn">
    <property name="geometry">
     <rect>
      <x>670</x>
      <y>510</y>
      <width>114</width>
      <height>32</height>
     </rect>
    </property>
    <property name="text">
     <string>Plot</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PlotWidget</class>
   <extends>QWidget</extends>
   <header>pyqtgraph</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:1)

您可以使用pg.Qt.loadUiType('uicfile.ui')来解决QtUiLoader的PySides问题。以下是基于您的代码的示例:

from PySide.QtGui import *
import pyqtgraph as pg
import numpy as np

formClass, baseClass = pg.Qt.loadUiType('example1.ui')
class MainWindow(baseClass):
    def __init__(self, parent=None):
        baseClass.__init__(self, parent)
        self.ui = formClass()
        self.ui.setupUi(self)
        self.setCentralWidget(self.ui.centralwidget)
        self.ui.plotBtn.clicked.connect(self.PlotTest)

    def PlotTest(self):
        self.ui.centralwidget.plot(np.random.normal(size=50), clear=True)

def main():
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

注意:

  • 您的.ui文件似乎构建不正确 - 您将'centralwidget'提升为PlotWidget,而我认为您希望将GraphicsView升级为。

  • 我遇到了loadUiType的一些unicode问题,不得不使用最新的pyqtgraph开发版本。您可能需要这样做。