本文介绍了将 QWidget 添加到 QListWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 QListWidget,其中每个项目都是一个包含文本和按钮的简单小部件.我使用以下内容:

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)

问题是,什么都没有显示.我得到一个可以使用键盘导航的空白行,但它是空白的.当小部件包含一个按钮时,它就可以工作,所以它不会像单独的按钮那样把事情搞砸.setItemWidget 可以处理的小部件的复杂性是否有限制?也许我需要超越便利类,正如下面一些相关帖子中所建议的那样?

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?

相关帖子

pyqt 向 QListWidget 添加小部件
请注意,上一篇文章的标题与我的相似,但似乎是一个关于 QtDesigner 的复杂代码拼凑(与一些自定义内容混合)的表达相对较差的问题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .根本不清楚这实际上是这个人应该问的问题.虽然标题使它看起来像重复,但问题(我祈祷)不是.

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

带有自定义项和自定义项小部件的 QListView/QListWidget

在 QT 中向 QListWidget 添加自定义小部件QT 中的点击问题?

pyqt 向 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

推荐答案

试试这个:

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)

如您所见,布局需要setSizeConstraint,项目需要setSizeHint.

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

这篇关于将 QWidget 添加到 QListWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:02