Change the data in label by selected rows in Table View
-
Good morning/afternoon everyone,
I need a help in changing the text in label (ID of sql query's result) by selecting row from Table View in which ID doesn't presented. I've done that:
dane = self.dlg.tableView_3.selectionModel().currentIndex() rzad = dane.row()#int row a=self.dlg.tableView_3.selectRow(rzad) self.dlg.label_67.setText(str(self.dlg.comboBox_9.currentText())) #powiat SqlId= 'SELECT ID FROM [tableName] where 1=1 and TERYT ='#get ID of table ter= ' \'' + self.dlg.comboBox_10.currentText() + '\'' Sqlter = SqlId + ter + ' ORDER BY ID DESC' query = QSqlQuery(db) query.exec_(Sqlter) while query.next(): Iden = str(int(query.record().value('ID')))#get ID from query print (Iden) a1 = self.dlg.label_32.setText(Iden)
but I works only if only record appears in table View.
I need to find the way of changing the ID in labels if another row (for those where are more results in tableView) after clicking/selecting the rowHave I put the screen of my app for better understanding of my problem?
Thanks for every kind of help!
-
Good morning/afternoon everyone,
I need a help in changing the text in label (ID of sql query's result) by selecting row from Table View in which ID doesn't presented. I've done that:
dane = self.dlg.tableView_3.selectionModel().currentIndex() rzad = dane.row()#int row a=self.dlg.tableView_3.selectRow(rzad) self.dlg.label_67.setText(str(self.dlg.comboBox_9.currentText())) #powiat SqlId= 'SELECT ID FROM [tableName] where 1=1 and TERYT ='#get ID of table ter= ' \'' + self.dlg.comboBox_10.currentText() + '\'' Sqlter = SqlId + ter + ' ORDER BY ID DESC' query = QSqlQuery(db) query.exec_(Sqlter) while query.next(): Iden = str(int(query.record().value('ID')))#get ID from query print (Iden) a1 = self.dlg.label_32.setText(Iden)
but I works only if only record appears in table View.
I need to find the way of changing the ID in labels if another row (for those where are more results in tableView) after clicking/selecting the rowHave I put the screen of my app for better understanding of my problem?
Thanks for every kind of help!
@Karoluss96
I don't understand what you asking. You show code which picks up anID
from a combobox (don't know what is in it) and passes that to a SQL query to select just that one row and put theID
from that (which will be the same as the value from the combobox) into some label.What does " selecting row from Table View in which ID doesn't presented." and "another row (for those where are more results in tableView) after clicking/selecting the row" mean? If you don't have the desired
ID
available in the combobox's list (unless you allow the user to type in a value) then the user cannot pick it to query for that row. -
OK, I'll show it on screen, but please wait a few time
-
@Karoluss96
I don't understand what you asking. You show code which picks up anID
from a combobox (don't know what is in it) and passes that to a SQL query to select just that one row and put theID
from that (which will be the same as the value from the combobox) into some label.What does " selecting row from Table View in which ID doesn't presented." and "another row (for those where are more results in tableView) after clicking/selecting the row" mean? If you don't have the desired
ID
available in the combobox's list (unless you allow the user to type in a value) then the user cannot pick it to query for that row.@JonB
photo of my problem
So in another way:- After choosing parameters (like TERYT code) from textboxes and comboboxes on left, the result of sql are shown on the tableView below.
- The 1st record is immediately selected, and putting his values in adequates labes. The problem, about which I asked on this forum refers to ID of record, which isn't show in Table View below (my CEO decided so that), so I need to find another way to get and present ID of selected Record.
My idea is to make sql again, but select only ID. It works, but only if there's one result record. If there're more records it shows it ID of the last one and don't change after select the different record (it still shows the ID of last record). I've tried to connect it with vertical header (in which the ID will be show), but it also doesn't work.
Short version of question: How to change label's text by selecting row in table View (in which ID isn't presented).
It that clear?
-
@JonB
photo of my problem
So in another way:- After choosing parameters (like TERYT code) from textboxes and comboboxes on left, the result of sql are shown on the tableView below.
- The 1st record is immediately selected, and putting his values in adequates labes. The problem, about which I asked on this forum refers to ID of record, which isn't show in Table View below (my CEO decided so that), so I need to find another way to get and present ID of selected Record.
My idea is to make sql again, but select only ID. It works, but only if there's one result record. If there're more records it shows it ID of the last one and don't change after select the different record (it still shows the ID of last record). I've tried to connect it with vertical header (in which the ID will be show), but it also doesn't work.
Short version of question: How to change label's text by selecting row in table View (in which ID isn't presented).
It that clear?
@Karoluss96 said in Change the data in label by selected rows in Table View:
My idea is to make sql again, but select only ID. It works, but only if there's one result record. If there're more records it shows it ID of the last one and don't change after select the different record (it still shows the ID of last record). I've tried to connect it with vertical header (in which the ID will be show), but it also doesn't work.
This is as unclear to me as your original question!
Short version of question: How to change label's text by selecting row in table View (in which ID isn't presented).
Well, you have to get the
ID
somehow from the row, else you don't know which one is selected!The problem, about which I asked on this forum refers to ID of record, which isn't show in Table View below (my CEO decided so that), so I need to find another way to get and present ID of selected Record.
Right, so maybe this is the crux of the question! You choose not to show the ID for each row in the table, but you still need to access it, right? Then I can think of a few ways:
-
Do include fetching the
ID
in the model/table'sSELECT
query along with the columns you want to show. But set that column to be hidden/not visible in theQTableView
. You can still access its value in the model from theQModelIndex
selected in the view. -
Do include
ID
as above, but interpose aQAbstractProxyModel
between the SQL model and the view, where the proxy adjust columns to not pass theID
column through to the view. The proxy still lets you retrieve theID
column's value in a row from the source model. -
If you have another unique column in the rows you could issue a query using that to obtain
ID
(or look through rows already in the model). But I don't think you are saying you have that anyway, plus it's messy.
First two are preferable, were you only looking for the "hidden columns" feature per the first one?
-
@JonB said in Change the data in label by selected rows in Table View:
but interpose a QAbstractProxyModel between the SQL model and the view, where the proxy adjust columns to not pass the ID column through to the view. The proxy still lets you retrieve the ID column's value in a row from the source model.
OK, I'll try like you suggest! The main problem is that, the data to tableView going through the QStandardItemModel Class not the QTabaleViewClass
Example:self.model4.setItem(x, 0, QStandardItem(str(query.record().value(ier[1]))))
I can punt ID from query as:
iden=int(query.record().value(ier[0]))
ier is a table with the name of attributes [ID,TERYT,Area,sign,,,]
-
@JonB said in Change the data in label by selected rows in Table View:
but interpose a QAbstractProxyModel between the SQL model and the view, where the proxy adjust columns to not pass the ID column through to the view. The proxy still lets you retrieve the ID column's value in a row from the source model.
OK, I'll try like you suggest! The main problem is that, the data to tableView going through the QStandardItemModel Class not the QTabaleViewClass
Example:self.model4.setItem(x, 0, QStandardItem(str(query.record().value(ier[1]))))
I can punt ID from query as:
iden=int(query.record().value(ier[0]))
ier is a table with the name of attributes [ID,TERYT,Area,sign,,,]
@Karoluss96 said in Change the data in label by selected rows in Table View:
The main problem is that, the data to tableView going through the QStandardItemModel Class not the QTabaleViewClass
Don't know what you mean,
QStandardItemModel
is a model,QTableViewClass
is a view.Since you ultimately have some SQL database it is a "shame" if you are using a
QStandardItemModel
Class rather than someQSql...
model class, as you will have extra work copying stuff around. You will need to include theID
from the query even in theQStandardItemModel
wherever you populate it, so that it is accessible. But either hiding view columns or proxy-hiding a source model column can be applied just as much to aQStandardItemModel
as to a SQL model, so the principle is the same. -
OK, I wrote:
in function to show records in table viewa1=int(query.record().value(ier[0]))#take ID self.model4.setItem(x, 32, QStandardItem(str(a1))) self.dlg.tableView_3.setColumnHidden(32,True)#...and hide it!
in function to select records:
Iden = dane.siblingAtRow(rzad).siblingAtColumn(32).data()#Niewidoczny ID self.dlg.label_32.setText(Iden)
And it finally works!
Thanks for help!