Ad

How Can I Add Push Button To Each And Every Item Of The List View

- 1 answer

Here is my sample code,i want to add a push button to each and every row of the list view.I am not found any method to set the widget to model.Can any one please help me how to add widget for each and every row of the list view.Thank you in advance.

Given below is my code:

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

Ad

Answer

You have to create a custom widget where you must set the button on the right side with a layout.

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

Ad
source: stackoverflow.com
Ad