Double click in QTableWidget and putting data in QLabels
-
wrote on 25 Jan 2022, 20:22 last edited by
Does anyone knows (after a double click), how to get a row from a QTableWidget (with three columns) and put this data in two labels, without a select?
Detail: Datas already showed in QTableWidget and double click already working.Line:
x = any line
Column:
- id, col_a, col_b
Labels (This is the point i need help)
- label_a.setText(text col_a)
- label_b.setText(text col_b)
-
Hi,
With the double click you already have the index so you can now grab the data of its column siblings.
-
wrote on 25 Jan 2022, 23:55 last edited byThis post is deleted!
-
wrote on 26 Jan 2022, 00:37 last edited by Andrade33
Another way, passing the index as parameters, but the return is None.
If i remove ".data()" then it shows the memory adress.
self.ui.tx_A.setText(str(self.ui.tb_Dados.selectionModel().currentIndex().sibling(
self.ui.tb_Dados.currentIndex().row(),
self.ui.tb_Dados.currentIndex().column()).data())) -
Since you are using a QTableWidget:
def onItemDoubleClicked(self, item): """ Connect this slot to the QTableWidget itemDoubleClicked signal """ for column, label in [(0, self.ui.label_a), (1, self.ui.label_b)]: sibling = self.ui.tb_Dados.item(item.row(), column) label.setText(sibling.text())
This is a basic way to do what you want.
@Andrade33 said in Double click in QTableWidget and putting data in QLabels:
self.ui.tx_A.setText(str(self.ui.tb_Dados.selectionModel().currentIndex().sibling(
self.ui.tb_Dados.currentIndex().row(),
self.ui.tb_Dados.currentIndex().column()).data()))Please avoid that kind of coding. You are making your code uselessly hard to read. You are getting the current index several times from different sources which makes things even less clear. When reusing an object that is returned from a method several times, just make a local variable out of it.
-
wrote on 26 Jan 2022, 16:25 last edited by Andrade33
Hi guy
.
Sorry for posting the code.
I'm new to all of this, both in python and in the forum.
I thank you for the information and the reply. -
Posting your code is not an issue. On the contrary, it will allow you to get feedback to improve it :-)
-
Posting your code is not an issue. On the contrary, it will allow you to get feedback to improve it :-)
-
wrote on 27 Jan 2022, 17:24 last edited by Andrade33
With your code show me sibling has no attibute text()...
I'm actually using a QTW, but in PySide6 hasn't "onITemDoubleClicked", as you said. But I've tried it with all kinds of signals, without success.
I am trying this code, shows the index, but i can't grab the cell content.
But doesn't matter... i'm just studying... Maybe I need more practice.
If i get an answer i will post. Thanks.if not self.ui.tx_A.text() and not self.ui.tx_B.text(): for idx in self.ui.tb_Dados.selectionModel().selectedIndexes(): self.ui.tx_A.setText(str(idx.row())) self.ui.tx_B.setText(str(idx.column()))
-
With your code show me sibling has no attibute text()...
I'm actually using a QTW, but in PySide6 hasn't "onITemDoubleClicked", as you said. But I've tried it with all kinds of signals, without success.
I am trying this code, shows the index, but i can't grab the cell content.
But doesn't matter... i'm just studying... Maybe I need more practice.
If i get an answer i will post. Thanks.if not self.ui.tx_A.text() and not self.ui.tx_B.text(): for idx in self.ui.tb_Dados.selectionModel().selectedIndexes(): self.ui.tx_A.setText(str(idx.row())) self.ui.tx_B.setText(str(idx.column()))
@Andrade33 said in Double click in QTableWidget and putting data in QLabels:
With your code show me sibling has no attibute text()...
I'm actually using a QTW, but in PySide6 hasn't "onITemDoubleClicked", as you said. But I've tried it with all kinds of signals, without success.onItemDoubleClicked is a slot that you have to add to your class and to which you have to connect the itemDoubleClicked signal.
-
@Andrade33 said in Double click in QTableWidget and putting data in QLabels:
With your code show me sibling has no attibute text()...
I'm actually using a QTW, but in PySide6 hasn't "onITemDoubleClicked", as you said. But I've tried it with all kinds of signals, without success.onItemDoubleClicked is a slot that you have to add to your class and to which you have to connect the itemDoubleClicked signal.
wrote on 27 Jan 2022, 20:50 last edited by Andrade33@SGaist
I understood your explanation very well. And I'm grateful.
This is the implementation class.################################################# (...) #Populate table self.populate_table_Widget() ################################################# # Button to save self.ui.bt_salvar.clicked.connect(self.cadDados) ################################################# def populate_table_Widget(self): (...) self.ui.tb_Dados.itemDoubleClicked.connect(self.onItemDoubleClicked) self.PreencherTabela() (...) ################################################# def onItemDoubleClicked(self, item): if not self.ui.tx_A.text() and not self.ui.tx_B.text(): for column, label in [(0, self.ui.tx_A), (1, self.ui.tx_B)]: sibling = self.ui.tb_Dados.item(item.row(), column) self.ui.tx_A.setText(sibling.text()) ################################################# def preencher_Tabela(self): lista = CrudDados() lista.listaDados() i = 0 while self.ui.tb_Dados.rowCount() > 0: (...) #################################################
-
So you have it working ?
-
wrote on 27 Jan 2022, 21:31 last edited by
@SGaist
Does not work.
Configured this way (with itemDoubleClick as signal, and onItemDoubleClick as slot), it does not accept the click.
I managed to put the indices on the labels but I used the doubleClick signal, with my code as slot. -
Can you create a minimal runnable script that shows that issue ?
-
wrote on 28 Jan 2022, 23:59 last edited by Andrade33
@SGaist
Hi friend.
I've already created a minimal app and it's running very well... add data to the database, populate the table dynamically, get the data from database and show the data in the table cells...
There is communication between signal and slot... everything is ok.
However, I'll try to isolate some parts...As I said, it's just a study... and I've already wasted a lot of time on it... let's move on.
At another time I will see this.
Now i'm learning to use github... as soon as i learn i'll put the code there and if you can see I pass the link to you (if you want, obviously)
For now, thank you very much for your attention.Just answer me one more thing:
As the problem was not resolved (or unsolved), how I proceed with the post? -
@SGaist
Hi friend.
I've already created a minimal app and it's running very well... add data to the database, populate the table dynamically, get the data from database and show the data in the table cells...
There is communication between signal and slot... everything is ok.
However, I'll try to isolate some parts...As I said, it's just a study... and I've already wasted a lot of time on it... let's move on.
At another time I will see this.
Now i'm learning to use github... as soon as i learn i'll put the code there and if you can see I pass the link to you (if you want, obviously)
For now, thank you very much for your attention.Just answer me one more thing:
As the problem was not resolved (or unsolved), how I proceed with the post?wrote on 29 Jan 2022, 06:51 last edited by@Andrade33 said in Double click in QTableWidget and putting data in QLabels:
As the problem was not resolved (or unsolved), how I proceed with the post?
Just leave thread as-is Unsolved if you wish, the forum will continue to function OK :) Or mark it as solved even though it wasn't for you, again the forum will work OK!
1/16