[英]PyQt4 - Using a QItemDelegate to display widget in a QListView
I want to have a QListView
which displays custom widgets. 我想要一个显示自定义窗口小部件的
QListView
。 I guess the best way to do this would be a QItemDelegate
. 我想最好的方法是
QItemDelegate
。 Unfortunately I don't quite understand how to subclass it correctly and how to implement the paint()
method, which seems to be the most important one. 不幸的是,我不太了解如何正确地对其进行子类化以及如何实现
paint()
方法,这似乎是最重要的方法。 I couldn't find anything about using a delegate to create another widget. 我找不到有关使用委托创建另一个小部件的任何信息。
I already tried to implement something similar without a delegate, but that didn't work out that well, because QListView
is not supposed to display widgets. 我已经尝试过在没有委托的情况下实现类似的功能,但是效果并不理想,因为
QListView
不应该显示小部件。
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class Model(QtCore.QAbstractListModel):
def __init__(self, parent=None):
super(QtCore.QAbstractListModel, self).__init__(parent)
self._widgets = []
def headerData(self, section, orientation, role):
""" Returns header for columns """
return "Header"
def rowCount(self, parentIndex=QtCore.QModelIndex()):
""" Returns number of interfaces """
return len(self._widgets)
def data(self, index, role):
""" Returns the data to be displayed """
if role == QtCore.Qt.DisplayRole:
row = index.row()
return self._widgets[row]
def insertRow(self, widget, parentIndex=QtCore.QModelIndex()):
""" Inserts a row into the model """
self.beginInsertRows(parentIndex, 0, 1)
self._widgets.append(widget)
self.endInsertRows()
class Widget(QtGui.QWidget):
def __init__(self, parent=None, name="None"):
super(QtGui.QWidget, self).__init__(parent)
self.layout = QtGui.QHBoxLayout()
self.setLayout(self.layout)
self.checkbox = QtGui.QCheckBox()
self.button = QtGui.QPushButton(self)
self.label = QtGui.QLabel(self)
self.label.setText(name)
self.layout.addWidget(self.checkbox)
self.layout.addWidget(self.button)
self.layout.addWidget(self.label)
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(QtGui.QMainWindow, self).__init__(parent)
self.view = QtGui.QListView(self)
self.model = Model()
self.view.setModel(self.model)
self.setCentralWidget(self.view)
self.model.insertRow(
widget=Widget(self)
)
self.model.insertRow(
widget=Widget(self)
)
self.model.insertRow(
widget=Widget(self)
)
self.model.insertRow(
widget=Widget(self)
)
self.show()
app = QtGui.QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())
So, how would I need to implement a delegate in order to do what I want? 那么,我将需要实现一个委托才能完成我想要的事情吗?
Here's an example of a QTableWidget
with a button and text on each row. 这是一个
QTableWidget
的示例,每行上都有一个按钮和文本。 I defined an add_item
method to add a whole row at once: insert a new row, put a button in column 0, put a regular item in column 1. 我定义了一个
add_item
方法来一次添加整行:插入新行,将按钮放在第0列,将常规项放在第1列。
import sys
from PyQt4 import QtGui,QtCore
class myTable(QtGui.QTableWidget):
def __init__(self,parent=None):
super(myTable,self).__init__(parent)
self.setColumnCount(2)
def add_item(self,name):
#new row
row=self.rowCount()
self.insertRow(row)
#button in column 0
button=QtGui.QPushButton(name)
button.setProperty("name",name)
button.clicked.connect(self.on_click)
self.setCellWidget(row,0,button)
#text in column 1
self.setItem(row,1,QtGui.QTableWidgetItem(name))
def on_click(self):
# find the item with the same name to get the row
text=self.sender().property("name")
item=self.findItems(text,QtCore.Qt.MatchExactly)[0]
print("Button click at row:",item.row())
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
widget = myTable()
widget.add_item("kitten")
widget.add_item("unicorn")
widget.show()
sys.exit(app.exec_())
Bonus: how to know on which button did the user clicked ? 奖励:如何知道用户单击了哪个按钮? A button doesn't have a row property, but we can create one when we instantiate the buttons, like so:
按钮没有row属性,但是我们可以在实例化按钮时创建一个属性,如下所示:
button.setProperty("row",row)
Problem is, if you sort your table or delete a row, the row numbers will not match any more. 问题是,如果您对表进行排序或删除一行,则行号将不再匹配。 So instead we set a "name" property, same as the text of the item in column 1. Then we can use
findItems
to get the row (see on_click
). 因此,我们改为设置一个“名称”属性,与第1列中项目的文本相同。然后,我们可以使用
findItems
来获取行(请参见on_click
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.