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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on 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 last edited by
      #3
      This post is deleted!
      1 Reply Last reply
      0
      • A Offline
        A Offline
        Andrade33
        wrote on 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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
              0
              • SGaistS SGaist

                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 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 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()))
                  
                  SGaistS 1 Reply Last reply
                  0
                  • A 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()))
                    
                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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
                    1
                    • SGaistS SGaist

                      @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 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
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • SGaistS SGaist

                          So you have it working ?

                          A Offline
                          A Offline
                          Andrade33
                          wrote on 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
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 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
                            0
                            • SGaistS SGaist

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

                              A Offline
                              A Offline
                              Andrade33
                              wrote on 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
                              0
                              • A 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?

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 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

                                • Login

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