2
votes

I've been able to use QTableWidget and QTableWidgetItem to populate info from our database into a QTableWidget based on the text returned in the search bar. While I'm able to get the searched item to populate all the info into a tablewidget, I am not able to continuely add products from the search bar into the tablewidget. Instead the result is just being overrode and replaced. Here's an example of my code.

# Defines Search Bar Button
def search_Bar(self):
    searchFor = self.searchBar.text()
    #print(searchFor)
    searchQ = c.execute(" SELECT * FROM databasetable WHERE title LIKE ('%' || ? || '%') ", (searchFor,))
    self.tableWidget.setRowCount(0)
    for row_number, row_data in enumerate(searchQ):
        self.tableWidget.insertRow(row_number)
        for column_number, data in enumerate(row_data):
            self.tableWidget.setItem(row_number, column_number, QTableWidgetItem(str(data)))

The above code will yield me the desired results however when I try to add multiple items to the QTableWidget it overwrites the previously selected item.

Is it possible to setItem without having it override the previous selection? I'd like to be able to create a table of the desired search results. We'd like the option to clear the table as well if needed.

2

2 Answers

2
votes

In order to append search results to the table without overwriting the previous table content, just don't call setRowCount(0) before adding items and use the current rowCount() as the row_number:

# Append search results to table
def search_Bar(self):
    searchFor = self.searchBar.text()
    searchQ = c.execute(" SELECT * FROM databasetable WHERE title LIKE ('%' || ? || '%') ", (searchFor,))
    for row_data in searchQ:
        # insert new row at the end of the tableWidget
        row_number = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row_number)
        for column_number, data in enumerate(row_data):
            self.tableWidget.setItem(
                row_number, column_number, QTableWidgetItem(str(data)))

If you want to clear the table (e.g. by clicking a "Clear Table" button), you can call the following function:

# Clear the table
def clear_table(self):
    self.tableWidget.setRowCount(0)
1
votes

in case anyone else stumbles on this i was able to get this working with the below.

The row count needs to be inside the loop for it keep incrementing i also had that line above my loop at first and was having issues.

for elem in res_a:
    domain_a = elem.host
    row_number = self.w_tablewidget.rowCount()
    self.w_tablewidget.insertRow(row_number)
    self.w_tablewidget.setItem(
    row_number, 0, QTableWidgetItem(str(site)))
    self.w_tablewidget.setItem(
                    row_number, 1, QTableWidgetItem(str('A')))
    self.w_tablewidget.setItem(
                    row_number, 2, QTableWidgetItem(str(elem.host)))

And like mentioned above you can clear it with "setRowCount(0)" or also via ".clearContents()"

Like:

self.w_tablewidget.clearContents()
self.w_tablewidget.setRowCount(0)