Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Double click in QTableWidget and putting data in QLabels
Forum Updated to NodeBB v4.3 + New Features

Double click in QTableWidget and putting data in QLabels

Scheduled Pinned Locked Moved Unsolved Qt for Python
16 Posts 3 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrade33
    wrote on 25 Jan 2022, 20:22 last edited by
    #1

    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)
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Jan 2022, 20:28 last edited by
      #2

      Hi,

      With the double click you already have the index so you can now grab the data of its column siblings.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Andrade33
        wrote on 25 Jan 2022, 23:55 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • A Offline
          A Offline
          Andrade33
          wrote on 26 Jan 2022, 00:37 last edited by Andrade33
          #4

          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()))

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 26 Jan 2022, 07:46 last edited by SGaist
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • A Offline
              A Offline
              Andrade33
              wrote on 26 Jan 2022, 16:25 last edited by Andrade33
              #6

              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.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 26 Jan 2022, 19:58 last edited by
                #7

                Posting your code is not an issue. On the contrary, it will allow you to get feedback to improve it :-)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply 27 Jan 2022, 17:12
                0
                • S SGaist
                  26 Jan 2022, 19:58

                  Posting your code is not an issue. On the contrary, it will allow you to get feedback to improve it :-)

                  A Offline
                  A Offline
                  Andrade33
                  wrote on 27 Jan 2022, 17:12 last edited by
                  #8

                  @SGaist
                  Oh yeah... but I talked about the way I posted. Don't worry.
                  Sorry mistakes... my native language is portuguese.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Andrade33
                    wrote on 27 Jan 2022, 17:24 last edited by Andrade33
                    #9

                    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()))
                    
                    S 1 Reply Last reply 27 Jan 2022, 19:42
                    0
                    • A Andrade33
                      27 Jan 2022, 17:24

                      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()))
                      
                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 27 Jan 2022, 19:42 last edited by
                      #10

                      @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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      A 1 Reply Last reply 27 Jan 2022, 20:50
                      1
                      • S SGaist
                        27 Jan 2022, 19:42

                        @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.

                        A Offline
                        A Offline
                        Andrade33
                        wrote on 27 Jan 2022, 20:50 last edited by Andrade33
                        #11

                        @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:
                            (...)
                        
                            #################################################
                        
                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 27 Jan 2022, 21:17 last edited by
                          #12

                          So you have it working ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          A 1 Reply Last reply 27 Jan 2022, 21:31
                          0
                          • S SGaist
                            27 Jan 2022, 21:17

                            So you have it working ?

                            A Offline
                            A Offline
                            Andrade33
                            wrote on 27 Jan 2022, 21:31 last edited by
                            #13

                            @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.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 28 Jan 2022, 20:46 last edited by
                              #14

                              Can you create a minimal runnable script that shows that issue ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              A 1 Reply Last reply 28 Jan 2022, 23:59
                              0
                              • S SGaist
                                28 Jan 2022, 20:46

                                Can you create a minimal runnable script that shows that issue ?

                                A Offline
                                A Offline
                                Andrade33
                                wrote on 28 Jan 2022, 23:59 last edited by Andrade33
                                #15

                                @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?

                                JonBJ 1 Reply Last reply 29 Jan 2022, 06:51
                                0
                                • A Andrade33
                                  28 Jan 2022, 23:59

                                  @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?

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on 29 Jan 2022, 06:51 last edited by
                                  #16

                                  @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 Reply Last reply
                                  0

                                  1/16

                                  25 Jan 2022, 20:22

                                  • Login

                                  • Login or register to search.
                                  1 out of 16
                                  • First post
                                    1/16
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved