1

ekhumoroから取得したコードを使用して、作成した GUI の xml ファイルからツリーを動的に作成していますが、pyuic4 から生成された _ui.py ファイル内でクラスを呼び出そうとすると、Ui_MainWindow が取得されます予想される引数の代わりにクラスが返されました。

ツリー項目を作成する XmlHandler (私の理解によると):

from PyQt4 import QtCore, QtGui, QtXml
from PyQt4.QtXml import *


class XmlHandler(QXmlDefaultHandler):
    def __init__(self, root):
        QtXml.QXmlDefaultHandler.__init__(self)
        self._root = root
        self._item = None
        self._text = ''
        self._error = ''

    def startElement(self, namespace, name, qname, attributes):
        if qname == 'Machine' or qname == 'Feature':
            if self._item is not None:
                self._item = QtGui.QTreeWidgetItem(self._item)
            else:
                self._item = QtGui.QTreeWidgetItem(self._root)
            self._item.setData(0, QtCore.Qt.UserRole, qname)
            self._item.setText(0, 'Unknown Machine')
            if qname == 'Machine':
                self._item.setExpanded(False)
                self._item.setCheckState(0, QtCore.Qt.Unchecked)
                self._item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsDragEnabled|QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsTristate)
            elif qname == 'FeatureName':
                self._item.setText(1, self._text)
        self._text = ''
        return True

    def endElement(self, namespace, name, qname):
        if qname == 'FeatureName' or qname == 'MachineName':
            if self._item is not None:
                self._item.setText(0, self._text)
                self._item.setCheckState(0, QtCore.Qt.Unchecked)
        elif qname == 'Feature' or qname == 'Machine':
            self._item = self._item.parent()
        return True

    def characters(self, text):
        self._text += text
        return True

    def fatalError(self, exception):
        print('Parse Error: line %d, column %d:\n  %s' % (
              exception.lineNumber(),
              exception.columnNumber(),
              exception.message(),
              ))
        return False

XmlHandler を使用してウィジェットを作成するクラス:

class MakeWidget(QtGui.QTreeWidget):
    def __init__(self):
        QtGui.QTreeWidget.__init__(self)
        self.header().setResizeMode(QtGui.QHeaderView.Stretch)
        self.setHeaderLabels(['Servers and Services'])
        source = QtXml.QXmlInputSource()
        source.setData(xml)
        handler = XmlHandler(self)
        reader = QtXml.QXmlSimpleReader()
        reader.setContentHandler(handler)
        reader.setErrorHandler(handler)
        reader.parse(source)

私は手探りして、GUIでそれを使用しようとしています:

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(950, 500)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMaximumSize(QtCore.QSize(950, 500))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tabWidget = QtGui.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(0, 0, 960, 421))
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.tabWidget.sizePolicy().hasHeightForWidth())
        self.tabWidget.setSizePolicy(sizePolicy)
        self.tabWidget.setMaximumSize(QtCore.QSize(960, 430))
        self.tabWidget.setAutoFillBackground(True)
        self.tabWidget.setStyleSheet(_fromUtf8(""))
        self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
        self.tabLogPuller = QtGui.QWidget()
        self.tabLogPuller.setObjectName(_fromUtf8("tabLogPuller"))
        self.groupBoxServiceSelection = QtGui.QGroupBox(self.tabLogPuller)
        self.groupBoxServiceSelection.setGeometry(QtCore.QRect(0, 0, 381, 351))
        font = QtGui.QFont()
        font.setBold(False)
        font.setWeight(50)
        self.groupBoxServiceSelection.setFont(font)
        self.groupBoxServiceSelection.setAutoFillBackground(True)
        self.groupBoxServiceSelection.setObjectName(_fromUtf8("groupBoxServiceSelection"))
        self.treeServiceSelection = MakeWidget()
        self.treeServiceSelection.setHeaderLabels(['Servers and Services'])
        self.treeServiceSelection.source = QtXml.QXmlInputSource()
        self.treeServiceSelection.source.setData(xml)
        self.treeServiceSelection.handler = XmlHandler(self)
        self.treeServiceSelection.reader = QtXml.QXmlSimpleReader()
        self.treeServiceSelection.reader.setContentHandler(self.treeServiceSelection.handler)
        self.treeServiceSelection.reader.setErrorHandler(self.treeServiceSelection.handler)
        self.treeServiceSelection.reader.parse(self.treeServiceSelection.source)
        self.treeServiceSelection.setGeometry(QtCore.QRect(10, 80, 161, 261))
        self.treeServiceSelection.setObjectName(_fromUtf8("treeServiceSelection"))

そして、ファイルを実行しようとしたときに発生するエラー:

error triggered by consumer
    self._item = QtGui.QTreeWidgetItem(self._root)
TypeError: arguments did not match any overloaded call:
  QTreeWidgetItem(int type=QTreeWidgetItem.Type): argument 1 has unexpected type 'Ui_MainWindow'
  QTreeWidgetItem(list-of-str, int type=QTreeWidgetItem.Type): argument 1 has unexpected type 'Ui_MainWindow'
  QTreeWidgetItem(QTreeWidget, int type=QTreeWidgetItem.Type): argument 1 has unexpected type 'Ui_MainWindow'
  QTreeWidgetItem(QTreeWidget, list-of-str, int type=QTreeWidgetItem.Type): argument 1 has unexpected type 'Ui_MainWindow'
  QTreeWidgetItem(QTreeWidget, QTreeWidgetItem, int type=QTreeWidgetItem.Type): argument 1 has unexpected type 'Ui_MainWindow'
  QTreeWidgetItem(QTreeWidgetItem, int type=QTreeWidgetItem.Type): argument 1 has unexpected type 'Ui_MainWindow'
  QTreeWidgetItem(QTreeWidgetItem, list-of-str, int type=QTreeWidgetItem.Type): argument 1 has unexpected type 'Ui_MainWindow'
  QTreeWidgetItem(QTreeWidgetItem, QTreeWidgetItem, int type=QTreeWidgetItem.Type): argument 1 has unexpected type 'Ui_MainWindow'
  QTreeWidgetItem(QTreeWidgetItem): argument 1 has unexpected type 'Ui_MainWindow'

だから私の質問は次のとおりだと思います:このクラスを呼び出して、UI_MainWindowクラス内で正しい引数を渡すには、親であると想定しているものを呼び出すのではなく?

4

1 に答える 1