Issue
I'm building a GUI for our testing platform have been instructed the user should be able select tests from a table to run. I'm having issues getting the data into the table itself. I can fetch the data fine, but cannot populate the QTableWidget
This is a sample of the data I'm trying to populate the table with
Id | Test name | Owner | Script source
---|----------- |-------|--------------
1 | Login | 1 | test_login_s
2 | Logout | 1 | test_logout_s
3 | User > Edit | 1 | test_user_edit_s
Initially, I tried to use the sqlite3
package to achieve this task, but reading has led me to the QtSQL
package, which seems easier to use with the given task.
Here's my latest attempt at implimentation using QtSQL
.
def load_data(self):
connection = sqlite3.connect('data.db')
cursor = connection.cursor()
cursor.execute('''SELECT * FROM Tests''')
rows = cursor.fetchall()
for row in range(len(rows)):
for column in range(len(rows[row])):
self.tblTests.setItem(row, column, QtWidgets.QTableWidgetItem(rows[row][column]))
This reults in None
being returned from self.tblTests.setItem(row, column, QtWidgets.QTableWidgetItem(rows[row][column]))
.
I have also tried the following approach
for row, key in enumerate(rows):
for column, data in enumerate(rows[row]):
self.tblTests.setItem(row, column, QtWidgets.QTableWidgetItem(str(data)))
In this case, data
returns
1
Login
1
test_login_s
2
Logout
1
test_logout_s
3
User > Edit
1
test_user_edit_s
This seems like it's on the right track. It still, however, does not populate tblTests
Solution
After attempting to debug the above code, and seeing that data
returned values, I thought something must be off.
After peeking through the setupUI()
function, I came across the line self.tblTests.setColumnCount(0)
. Intitially, I had thought this line was redundant due to the setItem()
call, but decided to leave it in the code for the sake of debugging.
I took a stab in the dark, and set self.tblTests.setColumnCount(0)
to 4, and it now works as intended.
I saw nothing in the documentation to suggest setColumnCount
needs to be entered manually when reading the documentation.
Hopefully this helps somebody else too!
Answered By - Glazbee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.