Add QWidget to QListWidget

纵饮孤独 提交于 2019-11-29 01:22:14

问题


I am trying to make a QListWidget in which each item is a simple widget that contains text and a pushbutton. I use the following:

itemN = QtGui.QListWidgetItem() 
#Create widget
widget = QtGui.QWidget()
widgetText =  QtGui.QLabel("I love PyQt!")
widgetButton =  QtGui.QPushButton("Push Me")
widgetLayout = QtGui.QHBoxLayout()
widgetLayout.addWidget(widgetText)
widgetLayout.addWidget(widgetButton)
widgetLayout.addStretch()
widget.setLayout(widgetLayout)
#Add widget to QListWidget funList
funList.addItem(itemN)
funList.setItemWidget(itemN, widget)

The problem is, nothing shows up. I get a blank line that I can navigate using my keyboard, but it is blank. When the widget contains just a pushbutton, it works, so it isn't as if the pushbutton alone is messing things up. Are there limits on the complexity of widgets that setItemWidget can handle? Perhaps I need to go beyond the convenience classes, as suggested in some of the related posts below?

Related posts

pyqt adding a widget to a QListWidget
Note the previous post has a similar title to mine, but seems to be a relatively poorly expressed question about a complex pastiche of code from QtDesigner (mixed with some custom stuff). It is not clear at all that this is actually the question the person should have been asking. While the title makes it seem like a duplicate, the question (I pray) is not.

I would say something similar about this post.

QListWidgetItem with Radio Button

QListView/QListWidget with custom items and custom item widgets

Adding Custom Widget to QListWidget in QT click issue in QT?

pyqt adding a widget to a QListWidget

http://www.qtcentre.org/threads/8660-Drawing-a-widget-in-QItemDelegate-s-paint-method

http://developer.nokia.com/community/discussion/showthread.php/211634-Adding-a-button-inside-QListWidgetItem


回答1:


Try this:

itemN = QtGui.QListWidgetItem() 
#Create widget
widget = QtGui.QWidget()
widgetText =  QtGui.QLabel("I love PyQt!")
widgetButton =  QtGui.QPushButton("Push Me")
widgetLayout = QtGui.QHBoxLayout()
widgetLayout.addWidget(widgetText)
widgetLayout.addWidget(widgetButton)
widgetLayout.addStretch()

widgetLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
widget.setLayout(widgetLayout)  
itemN.setSizeHint(widget.sizeHint())    

#Add widget to QListWidget funList
funList.addItem(itemN)
funList.setItemWidget(itemN, widget)

As you can see, you need setSizeConstraint to the layout and setSizeHint to item.




回答2:


If you use PyQt5, there is some changes, you must use QtWidgets for widget not QtGui. Use this code if using with PyQt5:

Don't forget to import PyQt5:

from PyQt5 import QtCore, QtGui, QtWidgets`

itemN = QtWidgets.QListWidgetItem()
# Create widget
widget = QtWidgets.QWidget()
widgetText = QtWidgets.QLabel("I love PyQt!")
widgetButton = QtWidgets.QPushButton("Push Me")
widgetLayout = QtWidgets.QHBoxLayout()
widgetLayout.addWidget(widgetText)
widgetLayout.addWidget(widgetButton)
widgetLayout.addStretch()

widgetLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
widget.setLayout(widgetLayout)
itemN.setSizeHint(widget.sizeHint())

# Add widget to QListWidget funList
funList.addItem(itemN)
funList.setItemWidget(itemN, widget)



来源:https://stackoverflow.com/questions/26199374/add-qwidget-to-qlistwidget

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