pyqt adding a widget to a QListWidget

◇◆丶佛笑我妖孽 提交于 2019-12-25 04:24:24

问题


hi i created 2 files from qtdesigner and i created a new file with a class where i want to use these UIs that i created.

this is the file creating the QListWidget
from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: _fromUtf8 = lambda s: s

class Ui_main_Dialog_lists(object):
    def setupUi(self, main_Dialog_lists):
        main_Dialog_lists.setObjectName(_fromUtf8("main_Dialog_lists"))
        main_Dialog_lists.resize(590, 521)
        self.main_verticalLayout = QtGui.QVBoxLayout(main_Dialog_lists)
        self.main_verticalLayout.setObjectName(_fromUtf8("main_verticalLayout"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.listWidget_nodes = QtGui.QListWidget(main_Dialog_lists)
        self.listWidget_nodes.setObjectName(_fromUtf8("listWidget_nodes"))
        self.horizontalLayout.addWidget(self.listWidget_nodes)
        self.label_arrow = QtGui.QLabel(main_Dialog_lists)
        self.label_arrow.setObjectName(_fromUtf8("label_arrow"))
        self.horizontalLayout.addWidget(self.label_arrow)
        self.listWidget_nodes_to_render = QtGui.QListWidget(main_Dialog_lists)
        self.listWidget_nodes_to_render.setObjectName(_fromUtf8("listWidget_nodes_to_render"))
        self.horizontalLayout.addWidget(self.listWidget_nodes_to_render)
        self.main_verticalLayout.addLayout(self.horizontalLayout)

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

    def retranslateUi(self, main_Dialog_lists):
        main_Dialog_lists.setWindowTitle(QtGui.QApplication.translate("main_Dialog_lists", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.label_arrow.setText(QtGui.QApplication.translate("main_Dialog_lists", ">", None, QtGui.QApplication.UnicodeUTF8))

this is the class creating the widget i want to insert into the rows of the QListWidget

from PyQt4 import QtCore, QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Frame_nodes(object):    
    def setupUi(self, Frame_nodes):
        Frame_nodes.setObjectName(_fromUtf8("Frame_nodes"))
        Frame_nodes.resize(508, 128)
        Frame_nodes.setFrameShape(QtGui.QFrame.StyledPanel)
        Frame_nodes.setFrameShadow(QtGui.QFrame.Raised)
        self.verticalLayout_2 = QtGui.QVBoxLayout(Frame_nodes)
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.horizontalLayout_names = QtGui.QHBoxLayout()
        self.horizontalLayout_names.setObjectName(_fromUtf8("horizontalLayout_names"))
        self.label_node = QtGui.QLabel(Frame_nodes)
        self.label_node.setObjectName(_fromUtf8("label_node"))
        self.horizontalLayout_names.addWidget(self.label_node)
        self.label_name_start = QtGui.QLabel(Frame_nodes)
        self.label_name_start.setObjectName(_fromUtf8("label_name_start"))
        self.horizontalLayout_names.addWidget(self.label_name_start)
        self.label_name_end = QtGui.QLabel(Frame_nodes)
        self.label_name_end.setObjectName(_fromUtf8("label_name_end"))
        self.horizontalLayout_names.addWidget(self.label_name_end)
        self.label_name_inter = QtGui.QLabel(Frame_nodes)
        self.label_name_inter.setObjectName(_fromUtf8("label_name_inter"))
        self.horizontalLayout_names.addWidget(self.label_name_inter)
        self.verticalLayout_2.addLayout(self.horizontalLayout_names)
        self.horizontalLayout_Inputs = QtGui.QHBoxLayout()
        self.horizontalLayout_Inputs.setObjectName(_fromUtf8("horizontalLayout_Inputs"))
        self.label_node_name = QtGui.QLabel(Frame_nodes)
        self.label_node_name.setObjectName(_fromUtf8("label_node_name"))
        self.horizontalLayout_Inputs.addWidget(self.label_node_name)
        self.lineEdit_node_start = QtGui.QLineEdit(Frame_nodes)
        self.lineEdit_node_start.setObjectName(_fromUtf8("lineEdit_node_start"))
        self.horizontalLayout_Inputs.addWidget(self.lineEdit_node_start)
        self.lineEdit = QtGui.QLineEdit(Frame_nodes)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.horizontalLayout_Inputs.addWidget(self.lineEdit)
        self.lineEdit_3 = QtGui.QLineEdit(Frame_nodes)
        self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
        self.horizontalLayout_Inputs.addWidget(self.lineEdit_3)
        self.verticalLayout_2.addLayout(self.horizontalLayout_Inputs)

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

    def retranslateUi(self, Frame_nodes):
        Frame_nodes.setWindowTitle(QtGui.QApplication.translate("Frame_nodes", "Frame", None, QtGui.QApplication.UnicodeUTF8))
        self.label_node.setText(QtGui.QApplication.translate("Frame_nodes", "Node", None, QtGui.QApplication.UnicodeUTF8))
        self.label_name_start.setText(QtGui.QApplication.translate("Frame_nodes", "Start", None, QtGui.QApplication.UnicodeUTF8))
        self.label_name_end.setText(QtGui.QApplication.translate("Frame_nodes", "End", None, QtGui.QApplication.UnicodeUTF8))
        self.label_name_inter.setText(QtGui.QApplication.translate("Frame_nodes", "Inter", None, QtGui.QApplication.UnicodeUTF8))
        self.label_node_name.setText(QtGui.QApplication.translate("Frame_nodes", "Node Name", None, QtGui.QApplication.UnicodeUTF8))

after compiling the 2 .ui files i created this file and i am trying to use them, so i can put one inside the other.

import lists
import nodes

from PyQt4 import QtCore, QtGui
import sys

class Master(QtGui.QFrame, QtGui.QWidget, nodes.Ui_Frame_nodes):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        QtGui.QFrame.__init__(self)

        self.ui = lists.Ui_main_Dialog_lists()
        self.ui.setupUi(self)

        for i in range(2):
            item_widget = nodes.Ui_Frame_nodes()
            qframe = QtGui.QFrame()
            item_widget.setupUi(qframe)

            wid2 = QtGui.QListWidgetItem()
            self.ui.listWidget_nodes.addItem(wid2)
            #here is where the script doesnt like it
            self.ui.listWidget_nodes.setItemWidget(wid2, item_widget)

app = QtGui.QApplication(sys.argv)
m = Master()
m.show()
app.exec_()

but i cant make it on the setItemWidget i get an error saying TypeError: QListWidget.setItemWidget(QListWidgetItem, QWidget): argument 2 has unexpected type 'Ui_Frame_nodes'

this is my first time trying to put a widget inside another i am not sure if i am doing it properly.

thanks guys and here is an image of the UIs https://dl.dropboxusercontent.com/u/14346803/ui_qt_designer.png


回答1:


Your problem might be that you haven't setup nodes correctly. I think what's happening is its trying to initialise as a python object (not Qt). I can't currently test this.

You probably need something like:

class CustomNodes(QWidget):
    def __init__(self, parent=None):
        super(CustomNodes, self).__init__(parent)

        self.ui = nodes.Ui_Frame_nodes()
        self.ui.setupUi(self)

Then use:

item_widget = CustomNodes(qframe)
self.ui.listWidget_nodes.setItemWidget(wid2, item_widget)

Please note: I haven't tested this code.



来源:https://stackoverflow.com/questions/24417285/pyqt-adding-a-widget-to-a-qlistwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!