问题描述
我从 qtdesigner 创建了 2 个文件,我创建了一个包含类的新文件,我想在其中使用我创建的这些 UI.
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.
这是创建 QListWidget 的文件
从 PyQt4 导入 QtCore、QtGui尝试:_fromUtf8 = QtCore.QString.fromUtf8除了属性错误:_fromUtf8 = lambda s: s
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))
这是创建小部件的类,我想插入到 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))
在编译了 2 个 .ui 文件后,我创建了这个文件,我正在尝试使用它们,所以我可以将其中一个放在另一个中.
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_()
但我无法在 setItemWidget 上成功,我收到一条错误消息TypeError: QListWidget.setItemWidget(QListWidgetItem, QWidget): 参数 2 有意外类型 'Ui_Frame_nodes'
but i cant make it on the setItemWidget i get an error sayingTypeError: 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.
谢谢各位这是用户界面的图像https://dl.dropboxusercontent.com/u/14346803/ui_qt_designer.png
thanks guysand here is an image of the UIshttps://dl.dropboxusercontent.com/u/14346803/ui_qt_designer.png
推荐答案
您的问题可能是您没有正确设置节点.我认为发生的事情是它试图初始化为 python 对象(不是 Qt).我目前无法对此进行测试.
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)
然后使用:
item_widget = CustomNodes(qframe)
self.ui.listWidget_nodes.setItemWidget(wid2, item_widget)
请注意:我尚未测试此代码.
这篇关于pyqt 向 QListWidget 添加小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!