Reputation:
I have got this code to have tabs and tree view. I would like that each row in a tree view would have some small buttons. How I can add a buttons to a rows of tree view?
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class tabdemo(QTabWidget):
def __init__(self, parent=None):
super(tabdemo, self).__init__(parent)
self.setGeometry(300, 300, 500, 300)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1, "Tab 1")
self.addTab(self.tab2, "Tab 2")
self.addTab(self.tab3, "Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("tab demo")
def tab3UI(self):
layout = QFormLayout()
layout.addRow("Name", QLineEdit())
layout.addRow("Address", QLineEdit())
self.setTabText(0, "Contact Details")
self.tab3.setLayout(layout)
def tab2UI(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton("Male"))
sex.addWidget(QRadioButton("Female"))
layout.addRow(QLabel("Sex"), sex)
layout.addRow("Date of Birth", QLineEdit())
self.setTabText(1, "Personal Details")
#self.button.clicked.connect(self.handleButton)
self.tab2.setLayout(layout)
def tab1UI(self):
layout = QGridLayout()
layout.addWidget(QLabel("subjects"), 0, 0)
layout.addWidget(QCheckBox("Physics"), 0, 1)
layout.addWidget(QCheckBox("Maths"), 0, 2)
view = QTreeView()
view.setSelectionBehavior(QAbstractItemView.SelectRows)
model = QStandardItemModel()
model.setHorizontalHeaderLabels(['col1', 'col2', 'col3'])
view.setModel(model)
view.setUniformRowHeights(True)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# populate data
for i in range(3):
parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
parent1.setCheckable(True)
for j in range(3):
child1 = QStandardItem('Child {}'.format(i * 3 + j))
child2 = QStandardItem('row: {}, col: {}'.format(i, j + 1))
child3 = QStandardItem('row: {}, col: {}'.format(i, j + 2))
parent1.appendRow([child1, child2, child3])
model.appendRow(parent1)
# span container columns
view.setFirstColumnSpanned(i, view.rootIndex(), True)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# expand third container
index = model.indexFromItem(parent1)
view.expand(index)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# select last row
selmod = view.selectionModel()
index2 = model.indexFromItem(child3)
selmod.select(index2, QItemSelectionModel.Select | QItemSelectionModel.Rows)
layout.addWidget(view , 1, 0)
self.setTabText(2, "Education Details")
self.tab1.setLayout(layout)
def main():
app = QApplication(sys.argv)
ex = tabdemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Upvotes: 1
Views: 6661
Reputation: 2211
If by "button" you mean a simple image of a button at the beginning of each row , you may use QStandardItem::setData(const QVariant & value, int role = Qt::UserRole + 1)
with role = Qt::DecorationRole
and value being a QIcon
or a QPixmap
.
if you want a QPushButton
for example, then you might consider building your custom delegate by inheriting QStyledItemDelegate
and render a specific display of your model.
Upvotes: 2
Reputation: 120778
You can use setIndexWidget
to set a widget on any column of a row:
class tabdemo(QTabWidget):
...
def tab1UI(self):
for i in range(3):
parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
parent1.setCheckable(True)
model.appendRow(parent1)
for j in range(3):
child1 = QStandardItem('Child {}'.format(i * 3 + j))
child2 = QStandardItem('row: {}, col: {}'.format(i, j + 1))
child3 = QStandardItem('row: {}, col: {}'.format(i, j + 2))
child4 = QStandardItem()
parent1.appendRow([child1, child2, child3, child4])
button = QToolButton()
button.setMaximumSize(button.sizeHint())
view.setIndexWidget(child4.index(), button)
Upvotes: 5
Reputation: 5207
QStandardItemModel
is a model, it just contains data. The view and its delegate(s) handle the visualizations.
Maybe your use case it better served with a QTreeWidget
and using its setItemWidget()
feature
Upvotes: 1