我有这个代码有标签和树视图。 我希望树视图中的每一行都有一些小按钮。 如何在一排树视图中添加按钮?
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()
答案 0 :(得分:3)
您可以使用setIndexWidget
在一行的任何列上设置小部件:
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)
答案 1 :(得分:1)
QStandardItemModel
是一个模型,它只包含数据。视图及其委托处理可视化。
也许您的用例最好使用QTreeWidget
并使用其setItemWidget()
功能
答案 2 :(得分:1)
如果按"按钮"你的意思是每行开头的按钮的简单图像,你可以使用QStandardItem :: setData(const QVariant& value,int role = Qt :: UserRole + 1),其中role = Qt :: DecorationRole,值为a QIcon或QPixmap。
如果您想要一个QPushButton,那么您可以考虑通过继承QStyledItemDelegate并渲染模型的特定显示来构建自定义委托。