Extract the data from table when selected.
-
Hello,
I want to extract the data from a table when selected.
in the picture above, what I currently got is: Once row 1 is selected, the button (2) will become available to click, and it will load up a new window. In that new window, I want to query 3 tables in my database for that Test_Identifier found in the row selected.What I am struggling with is, how to get the data from the table so I can then query it in the tables I have.
-
Do you have a QTableWidget or QTableView?
For QTableWidget: https://doc.qt.io/qt-5/qtablewidget.html#selectedItems
For QTableView: https://doc.qt.io/qt-5/qabstractitemview.html#selectionModel -
Do you have a QTableWidget or QTableView?
For QTableWidget: https://doc.qt.io/qt-5/qtablewidget.html#selectedItems
For QTableView: https://doc.qt.io/qt-5/qabstractitemview.html#selectionModel@Christian-Ehrlicher I am using QTableWidget, I have checked that documentation but its not very beginner-friendly.
I gave it a go:selected_row = self.OverviewDatabaseTable.selectedItems() print(selected_row)
to see what it would output, and I got this:
which i am guessing its the location of the items in that selected row. How can I get the text rather than that? -
@Christian-Ehrlicher I am using QTableWidget, I have checked that documentation but its not very beginner-friendly.
I gave it a go:selected_row = self.OverviewDatabaseTable.selectedItems() print(selected_row)
to see what it would output, and I got this:
which i am guessing its the location of the items in that selected row. How can I get the text rather than that?@DouglinhasZN
Hi its a list with the actual items
so
selected_row.at(x)->text gives the text. ( c++ syntax)If you only select one item, the list contains only one at index 0
-
@DouglinhasZN
Hi its a list with the actual items
so
selected_row.at(x)->text gives the text. ( c++ syntax)If you only select one item, the list contains only one at index 0
@mrjj
I think I am on the right tracks:row = self.OverviewDatabaseTable.currentRow() # Index of Row firstColumnInRow = self.OverviewDatabaseTable.item(row, 0) # returns QTableWidgetItem text = firstColumnInRow.text() # content of this print (str(text)) # if this is a index, you propably dont want it as text
This will output exactly what I need in the terminal. I guess its just a matter of querying the database using "text"
-
@mrjj
I think I am on the right tracks:row = self.OverviewDatabaseTable.currentRow() # Index of Row firstColumnInRow = self.OverviewDatabaseTable.item(row, 0) # returns QTableWidgetItem text = firstColumnInRow.text() # content of this print (str(text)) # if this is a index, you propably dont want it as text
This will output exactly what I need in the terminal. I guess its just a matter of querying the database using "text"
Hi
That should work fine too
if you always just want first col's text regardless of the row
and if the user clicks on some other celll on that row.Do note that cells can be real empty and then you get a NULL back and will crash on
firstColumnInRow.text()so you should always check the returned item. (forgive my python noobness)
row = self.OverviewDatabaseTable.currentRow() # Index of Row firstColumnInRow = self.OverviewDatabaseTable.item(row, 0) # returns QTableWidgetItem if firstColumnInRow is not None: text = firstColumnInRow.text() # content of this print (str(text)) # if this is a index, you propably dont want it as text
-
Hi
That should work fine too
if you always just want first col's text regardless of the row
and if the user clicks on some other celll on that row.Do note that cells can be real empty and then you get a NULL back and will crash on
firstColumnInRow.text()so you should always check the returned item. (forgive my python noobness)
row = self.OverviewDatabaseTable.currentRow() # Index of Row firstColumnInRow = self.OverviewDatabaseTable.item(row, 0) # returns QTableWidgetItem if firstColumnInRow is not None: text = firstColumnInRow.text() # content of this print (str(text)) # if this is a index, you propably dont want it as text
@mrjj Thank you, i've added that in even though the user wont be able to save if the field is empty. Always good to make sure.
I have been attempting to query all my 3 tables for test_id :
def Open_Selected_Finding(self): whole_row = self.DatabaseTable.currentRow() # Index of Row firstColumnInRow = self.DatabaseTable.item(whole_row, 0) # returns QTableWidgetItem if firstColumnInRow is not None: test_id_ = firstColumnInRow.text() # content of this print (test_id_) # if this is a index, you propably dont want it as text conn = sqlite3.connect('Main_Database.db') content = 'SELECT * FROM Test_Details, CVE_Details,Findings WHERE Test_ID=?',(test_id_) executing_content = conn.execute(content) self.window = QtWidgets.QDialog() self.ui = Ui_FindingDetailWindow() self.ui.setupUi(self.window) self.window.show() for colm_index, colm_data in enumerate(executing_content): self.FindingWindowNameTextEdit.setText(str(colm_data))
The goal is to get all the content in every column from the row that contains the "test_id" selected.
Then input that content into a new window:
-
Hi
It might just be due to it being python but it looks odd to me as im used to the c++ wayexecuting_content = conn.execute(content)
Does that really return the selected rows from the database ?
Normally execute just returns true / false to indicated if it went well or not.
Normally it would be like
query = QSqlQuery("SELECT country FROM artist")
while query.next():
country = query.value(0)Sorry if im just python noob and it does indeed work :)
-
Hi
It might just be due to it being python but it looks odd to me as im used to the c++ wayexecuting_content = conn.execute(content)
Does that really return the selected rows from the database ?
Normally execute just returns true / false to indicated if it went well or not.
Normally it would be like
query = QSqlQuery("SELECT country FROM artist")
while query.next():
country = query.value(0)Sorry if im just python noob and it does indeed work :)
@mrjj
Perhaps, I was trying to use the code I used in another block that works fine.
At this point I am just trying until it sticks. However, for the QSqlQuery wouldn't that require me to have used QSql to begin with? im using Sqlite3.
That's the error I am getting. Unfortunately the documentation isn't very user-friendly, so ill have to keep bashing my head in this until it worksEdit#1 - This is what I am using to select from the database using my variable:
querying = 'SELECT * FROM Findings WHERE Test_ID=?', (test_id_)
The question now is, how do I input the contents from that query to a new window
Update#1 - Its working, but it isnt.
This is my code:querying1 = c.execute('SELECT Test_ID FROM Findings WHERE Test_ID=?', (test_id_,)) self.window = QtWidgets.QDialog() self.ui = Ui_FindingDetailWindow() self.ui.setupUi(self.window) self.window.show() self.ui.FindingWindowNameTextEdit.setText(str(querying1))
However, instead of outputting the actual text, it outputs this:
i got it
ah that was easier than i thought
for Test_ID, Name, Description, Remediation, Criticality, Type in querying1.fetchall():
that was the key