我正在将数据从SQLite数据库填充到PyQt5 TreeView控件(QTreeView)中。 数据通过QStandardItemModel()写入。
问题:我想记住每行的row_id,而不显示它。 我曾经在查询中选择它,但隐藏了该列。 但是,当从index = 0的列查询row_id时,此操作将失败,因为它不可见。
我不知道a。)如何存储row_id以便b。)稍后将其检索。 我想遍历选定的行,并根据row_id执行某些操作(例如,批量删除,批量编辑,批量复制等)
model.setData始终需要列索引... Qt.UserRole似乎在检索数据时失败。
代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class App(QWidget):
MAIL_RANGE = 4
ID, FROM, SUBJECT, DATE = range(MAIL_RANGE)
def __init__(self):
super().__init__()
self.left = 10
self.top = 10
self.width = 640
self.height = 240
self.initUI()
self.dataView.setSelectionMode(QAbstractItemView.ExtendedSelection) # <- enable selection of rows in tree
self.dataView.setEditTriggers(QAbstractItemView.NoEditTriggers) # <- disable editing items in tree
for i in range(0, 2):
self.dataView.resizeColumnToContents(i)
self.pbOk = QPushButton(self)
self.pbOk.setText("Ok")
self.pbOk.move(400,0)
self.pbOk.show()
# connect handlers
self.dataView.doubleClicked.connect(self.on_dataView_doubleClicked)
self.pbOk.clicked.connect(self.on_pbOk_clicked)
def on_dataView_doubleClicked(self):
print("on_dataView_doubleClicked() called.")
def on_pbOk_clicked(self):
print("on_pbOk_clicked() called.")
# get all IDs
message: str = ""
col_ind: int = 0
for item in self.dataView.selectedIndexes():
if col_ind % (self.MAIL_RANGE) == 0: # indicates a row beginning
text = item.data(Qt.DisplayRole) # or ix.data()
message = message + "\n" + str(text)
self.create_dialog(text)
col_ind += 1
print(message)
def create_dialog(self, id):
print("dialog called for " + str(id))
myDlg = QDialog(self)
lbl = QLabel(myDlg)
lbl.setText("Hello id: " + str(id))
myDlg.show()
myDlg.resize(300,200)
def initUI(self):
self.setGeometry(self.left, self.top, self.width, self.height)
self.dataGroupBox = QGroupBox("Inbox")
self.dataView = QTreeView()
self.dataView.setRootIsDecorated(False)
self.dataView.setAlternatingRowColors(True)
dataLayout = QHBoxLayout()
dataLayout.addWidget(self.dataView)
self.dataGroupBox.setLayout(dataLayout)
model = self.createMailModel(self)
self.dataView.setModel(model)
self.addMail(model, 1, 'service@github.com', 'Your Github Donation','03/25/2017 02:05 PM')
self.addMail(model, 2, 'support@github.com', 'Github Projects','02/02/2017 03:05 PM')
self.addMail(model, 3, 'service@phone.com', 'Your Phone Bill','01/01/2017 04:05 PM')
self.addMail(model, 4, 'service@abc.com', 'aaaYour Github Donation','03/25/2017 02:05 PM')
self.addMail(model, 5, 'support@def.com', 'bbbGithub Projects','02/02/2017 03:05 PM')
self.addMail(model, 6, 'service@xyz.com', 'cccYour Phone Bill','01/01/2017 04:05 PM')
self.dataView.setColumnHidden(0, True)
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.dataGroupBox)
self.setLayout(mainLayout)
self.show()
def createMailModel(self,parent):
model = QStandardItemModel(0, self.MAIL_RANGE, parent)
model.setHeaderData(self.ID, Qt.Horizontal, "ID")
model.setHeaderData(self.FROM, Qt.Horizontal, "From")
model.setHeaderData(self.SUBJECT, Qt.Horizontal, "Subject")
model.setHeaderData(self.DATE, Qt.Horizontal, "Date")
return model
def addMail(self, model, mailID, mailFrom, subject, date):
model.insertRow(0)
model.setData(model.index(0, self.ID), mailID)
model.setData(model.index(0, self.FROM), mailFrom)
model.setData(model.index(0, self.SUBJECT), subject)
model.setData(model.index(0, self.DATE), date)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
总结:
1。)在addMail()上,我想将ID添加到插入的行中。
2。)在on_pbOk_clicked()事件上,我想遍历选定的行并获取每一行的所有ID。
如果我将ID添加为单独的列,则以后将无法检索它,因为这不适用于隐藏的列。
答案 0 :(得分:0)
您确定选择是按行进行的,那么仅需要获取该行并对其进行迭代,在这种情况下,请使用set获取所选的行并在列上进行迭代。
def on_pbOk_clicked(self):
message: str = ""
rows = set(ix.row() for ix in self.dataView.selectedIndexes())
for row in rows:
values_for_row = []
for col in range(App.MAIL_RANGE):
it = self.dataView.model().item(row, col)
values_for_row.append(it.text())
text = " ".join(values_for_row)
self.create_dialog(text)
message += "\n" + text
print(message)