简体   繁体   English

兄弟姐妹或兄弟姐妹AtRow()function如何从QTableWidget中的隐藏列中检索值?

[英]How does sibling or siblingAtRow() function works to retrieve the value from hidden Column in QTableWidget?

I have a database from which data is coming into a QTableWidget.我有一个数据库,数据从中进入 QTableWidget。 The table in the database has the following Columns,数据库中的表有以下几列,

  • ID (Primary key, auto-increment value) ID (主键,自增值)
  • Name姓名
  • Location地点

The QTableWidget has the following columns (that I have added) QTableWidget 具有以下列(我已添加)

  • ID (this column, I have hidden. and it contains the value of "ID" column from the Database Table) ID (此列,我已隐藏。它包含数据库表中“ID”列的值)
  • Sr # (Represents the Row Number of the table) sr# (代表表的行号)
  • Name (Contains "name" from the database table)名称(包含数据库表中的“名称”)
  • Location (Contains "Location from the database table)位置(包含“数据库表中的位置”)
  • Actions (Contains a Delete Button for that Row)操作(包含该行的删除按钮)

By hidden, I mean to say that I have made this column hidden using the folliwng command,隐藏,我的意思是说我已经使用以下命令隐藏了这个列,

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

This is how I am populating my QTableWidget and creating a Delete Function,这就是我填充 QTableWidget 并创建删除 Function 的方式,

    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")

On the Deletion Function, what this code does is basically gets the value of the **Sr # ** Column from the QTableWidget and deletes the data according to that, ie it is getting me the value from the visible first column and not the actual first column .在删除 Function 上,此代码所做的基本上是从 QTableWidget 获取 **Sr # ** 列的值并据此删除数据,即它从visible first column中获取值而不是actual first columnactual first column But, I want the data from the "ID" column of the QTableWidget which is hidden但是,我想要隐藏的 QTableWidget 的“ID”列中的数据

I tried to look up on how to get the value from the first hidden column on the QTableWidget and ended up with this link: How to get data from hidden 'id' column in QtableWidget我试图查找how to get the value from the first hidden column on the QTableWidget并最终得到此链接: How to get data from hidden 'id' column in QtableWidget

This apparently solves my issue but I can not seem to make it work for my code.这显然解决了我的问题,但我似乎无法让它适用于我的代码。 I don't want to retrieve values of multiple Rows but only of one row so how do I do this (as I am only deleting one row. But in the question mentioned, I believe that it is getting data from multiple rows due to that for each loop)?我不想检索多行的值,而只想检索一行的值,所以我该怎么做(因为我只删除一行。但在提到的问题中,我相信它正在从多行获取数据每个循环)?

Moreover, I tried to find help regarding the functionality of sibling function (which is provided in the answer of above question) however I could not find any good resource on this function (ie how to use this, or some practical example and etc.)此外,我试图找到有关sibling function 功能的帮助(在上述问题的答案中提供)但是我在这个 function 上找不到任何好的资源(即如何使用它,或一些实际示例等)

I tried the following with Sibling function to obtain the value of first hidden column of the Selected Row but it did not work,我用 Sibling function 尝试了以下方法来获取所选行的第一个隐藏列的值,但它没有用,

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

There are some conceptual problems with the given code.给定的代码存在一些概念性问题。

First of all, the QtSql module should be preferred instead of artificially creating a model.首先,应该首选QtSql模块,而不是人为地创建一个model。 For basic tables, QSqlTableModel is fine enough, while for custom queries, QSqlQueryModel is a good choice.对于基本表, QSqlTableModel就足够了,而对于自定义查询, QSqlQueryModel是一个不错的选择。

Now the problem is that UI-based selection is always based on visible items: if you select a row in a view that has hidden columns, you will not get the hidden indexes that belong to those columns.现在的问题是基于 UI 的选择始终基于可见项:如果您 select 视图中有隐藏列的行,您将不会获得属于这些列的隐藏索引。

In order to get the indexes (as in QModelIndex ) of hidden columns on a table widget, the only way is the same for a table view : you need to access the model and get the index for the row, or you get the actual model index and then get the sibling (which is conceptually the same, as the underlying function does):为了获取表小部件上隐藏列的索引(如在QModelIndex中),表视图的唯一方法是相同的:您需要访问model并获取行的索引,或者获取实际的 model索引,然后获取兄弟(在概念上与底层 function 相同):

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

And that's because when you create QTableWidget items, you're actually creating a new model, and the row for that model doesn't reflect the actual "row" of that index in the source model;那是因为当您创建 QTableWidget 项目时,您实际上是在创建一个的 model,并且model 的行不反映源 Z20F35E630DAF439DBFA4C3F68CZ 中该索引的实际“行”; items in the second row will return 1 for row() , even if their actual row is different, and that's because that item has been added as second to the table widget, since it's the second item in the query.第二行中的项目将为row()返回1 ,即使它们的实际行不同,这是因为该项目已作为第二个添加到表小部件中,因为它是查询中的第二个项目

So, the solution is that you either get the incremental row value for the first column index sibling, or you use one of the predefined Sql models.因此,解决方案是获取第一列索引兄弟的增量行值,或者使用预定义的 Sql 模型之一。

For simple models, the latter solution is fine enough, but if you need more complex models, the first is certainly more accurate and reliable.对于简单的模型,后一种解决方案已经足够了,但如果你需要更复杂的模型,第一种肯定更准确可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM