如何将按钮添加到列表视图的每个项目

时间:2019-03-22 08:44:38

标签: python python-2.7 pyqt4

这是我的示例代码,我想向列表视图的每一行添加一个按钮。我没有找到任何将小部件设置为模型的方法。任何人都可以帮助我如何为列表视图的每一行。谢谢您。

下面是我的代码:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyCustomWidget(QWidget):
    def __init__(self,parent=None):
        super(MyCustomWidget, self).__init__(parent)

        self.row = QHBoxLayout()
        self.row.addWidget(QPushButton("add"))
        self.setLayout(self.row)
class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent=parent)
        vLayout = QtGui.QVBoxLayout(self)
        hLayout = QtGui.QHBoxLayout()
        self.lineEdit = QtGui.QLineEdit()
        hLayout.addWidget(self.lineEdit)
        self.filter = QtGui.QPushButton("filter", self)
        hLayout.addWidget(self.filter)
        self.list = QtGui.QListView(self)
        vLayout.addLayout(hLayout)
        vLayout.addWidget(self.list)
        self.model = QtGui.QStandardItemModel(self.list)
        codes = [
            'windows',
            'windows xp',
            'windows7',
            'hai',
            'habit',
            'hack',
            'good'
        ]
        self.list.setModel(self.model)
        for code in codes:
            item = QtGui.QStandardItem(code)
            self.model.appendRow(item)
            self.list.setIndexWidget(item.index(), QtGui.QPushButton("button"))

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())

enter image description here

1 个答案:

答案 0 :(得分:1)

您必须创建一个自定义窗口小部件,您必须在其中设置带有布局的右侧按钮。

import sys
from PyQt4 import QtCore, QtGui

class CustomWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(CustomWidget, self).__init__(parent)
        self.button = QtGui.QPushButton("button")
        lay = QtGui.QHBoxLayout(self)
        lay.addWidget(self.button, alignment=QtCore.Qt.AlignRight)
        lay.setContentsMargins(0, 0, 0, 0)

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent=parent)
        vLayout = QtGui.QVBoxLayout(self)
        hLayout = QtGui.QHBoxLayout()
        self.lineEdit = QtGui.QLineEdit()
        hLayout.addWidget(self.lineEdit)
        self.filter = QtGui.QPushButton("filter", self)
        hLayout.addWidget(self.filter)
        self.list = QtGui.QListView(self)
        vLayout.addLayout(hLayout)
        vLayout.addWidget(self.list)
        self.model = QtGui.QStandardItemModel(self.list)
        codes = [
            'windows',
            'windows xp',
            'windows7',
            'hai',
            'habit',
            'hack',
            'good'
        ]
        self.list.setModel(self.model)
        for code in codes:
            item = QtGui.QStandardItem(code)
            self.model.appendRow(item)
            self.list.setIndexWidget(item.index(), CustomWidget())

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())

enter image description here