兄弟或siblingAtRow()函数如何从QTableWidget中的隐藏列中检索值



我有一个数据库,数据从该数据库进入QTableWidget。数据库中的表有以下列,

  • ID(主键,自动递增值(
  • 名称
  • 位置

QTableWidget有以下列(我已经添加(

  • ID(我已经隐藏了这一列。它包含数据库表中"ID"列的值(
  • Sr#(表示表的行号(
  • 名称(包含数据库表中的"名称"(
  • 位置(包含"数据库表中的位置"(
  • Actions(包含该行的删除按钮(

所谓隐藏,我的意思是说我已经使用following命令将此列隐藏起来

self.ui.table.setColumnHidden(0, True); 

这就是我填充QTableWidget并创建删除函数的方式

def get_data(self):
mycursor = self.DB.cursor()
Subquery = "select id, name, location "
Subquery += " from tbl_person"
mycursor.execute(Subquery)
numcols = len(mycursor.fetchall()[0])
mycursor.execute(Subquery)
numrows = len(mycursor.fetchall())
self.ui.table.setRowCount(numrows)
self.ui.table.setColumnCount(numcols+2)
mycursor.execute(Subquery)
tablerow = 0
for row in mycursor.fetchall():
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
delete_button = QPushButton("Delete Data")
delete_button.clicked.connect(self.executeDeleteFunction)
# delete_button.setStyleSheet(delete_push_button) -> Only for styling
self.ui.table.setItem(tablerow, 0, PySide2.QtWidgets.QTableWidgetItem(str(row[0])))
self.ui.table.setItem(tablerow, 1, PySide2.QtWidgets.QTableWidgetItem(str(tablerow+1)))
self.ui.table.setItem(tablerow, 2, PySide2.QtWidgets.QTableWidgetItem(str(row[1])))
self.ui.table.setItem(tablerow, 3, PySide2.QtWidgets.QTableWidgetItem(str(row[2])))
self.ui.table.setCellWidget(tablerow, 4, delete_button)
tablerow += 1
self.ui.table.setColumnHidden(0, True)
#self.ui.registered_table.horizontalHeader().setSectionResizeMode(PySide2.QtWidgets.QHeaderView.Stretch)
self.ui.table.resizeColumnsToContents()
def executeDeleteFunction(self):
self.person_id = self.ui.table.selectionModel().selectedIndexes()[0]
self.person_id = self.person_id.row()
mycursor = self.DB.cursor()
sql = "delete from tbl_person where id = %s"
val = (id, )
mycursor.execute(sql, val)
print("Deletion Successful")

在删除功能上,这个代码基本上是从QTableWidget中获取**Sr#**Column的值,并根据该值删除数据,即它是从visible first column而不是actual first column中获取值。但是,我希望数据来自;ID";隐藏的QTableWidget的列

我试着在how to get the value from the first hidden column on the QTableWidget上查找,最后得到了这个链接:如何从隐藏的';id';QtableWidget 中的列

这显然解决了我的问题,但我似乎无法让它为我的代码工作。我不想检索多行的值,而只想检索一行的值。那么我该怎么做呢(因为我只删除了一行。但在提到的问题中,我相信它是从多行中获取数据的,因为每个循环都是这样(?

此外,我试图找到关于sibling功能的帮助(在上面问题的答案中提供(,但我找不到任何关于该功能的好资源(即如何使用该功能,或一些实际示例等(

我尝试了以下Sibling函数来获得所选行的第一个隐藏列的值,但没有成功,

self.value = self.table.selectedItems()[0]
self.value = sibling(self.value.row(), 0)

给定的代码存在一些概念问题。

首先,应该优先选择QtSql模块,而不是人为地创建模型。对于基本表,QSqlTableModel足够好,而对于自定义查询,QSqlQueryModel是一个不错的选择。

现在的问题是,基于UI的选择总是基于可见项:如果您在视图中选择了具有隐藏列的行,则将无法获得属于这些列的隐藏索引。

为了获得表小部件上隐藏列的索引(如在QModelIndex中(,唯一的方法与表视图相同:您需要访问模型并获得行的索引,或者您获得实际的模型索引,然后获得同级索引(这在概念上与底层函数相同(:

item = self.table.selectedItems()[0]
index = self.table.indexForItem(index)
firstRowIndex = index.sibling(index.row(), 0)
sqlIndex = firstRowIndex.data() # might be a string

注意,您也可以使用siblingAtColumn():

firstRowIndex = index.siblingAtColumn(0)

这是因为当你创建QTableWidget项目时,你实际上是在创建一个新的模型,而该模型的行并不能反映实际的";行";源模型中该索引的;第二行中的项将为row()返回1,即使它们的实际行不同,这是因为该项已作为第二个添加到表小部件中,因为它是查询中的第二个

因此,解决方案是获得第一列索引同级的增量行值,或者使用预定义的Sql模型之一。

对于简单的模型,后一种解决方案足够好,但如果您需要更复杂的模型,第一种肯定更准确可靠。

最新更新