Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Retrieve QTableWidget name when clicked/select cells in tablewdiget

Retrieve QTableWidget name when clicked/select cells in tablewdiget

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.8k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    What do you mean by "tablewidget name" ?
    Do you mean the text on the item(s) you click on ?
    There is signals for that.

    If you can have one or more selected, there is also
    http://doc.qt.io/qt-5/qtablewidget.html#selectedItems

    P 1 Reply Last reply
    2
    • mrjjM mrjj

      Hi
      What do you mean by "tablewidget name" ?
      Do you mean the text on the item(s) you click on ?
      There is signals for that.

      If you can have one or more selected, there is also
      http://doc.qt.io/qt-5/qtablewidget.html#selectedItems

      P Offline
      P Offline
      Piyush
      wrote on last edited by Piyush
      #3

      @mrjj So I have multiple tablewidgets in my GUI. I actually need to find the selected cell/item falls under which tablewidget? Is there any signal for that?

      So whenever i select any item or cell in a tablewidget, it should give me the tablewidget's name.

      mrjjM 1 Reply Last reply
      0
      • P Piyush

        @mrjj So I have multiple tablewidgets in my GUI. I actually need to find the selected cell/item falls under which tablewidget? Is there any signal for that?

        So whenever i select any item or cell in a tablewidget, it should give me the tablewidget's name.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Piyush
        Hi
        But how come there can be any doubts ??

        Do you have hook all tablewidgets to same slot or how come its even an issue ?

        You have to explain why you need this as normally its non issue :)
        u want to hangle item slection from same slot for all tablewidgets or why
        do u need name ?

        P 1 Reply Last reply
        1
        • P Offline
          P Offline
          Piyush
          wrote on last edited by Piyush
          #5
          This post is deleted!
          1 Reply Last reply
          0
          • mrjjM mrjj

            @Piyush
            Hi
            But how come there can be any doubts ??

            Do you have hook all tablewidgets to same slot or how come its even an issue ?

            You have to explain why you need this as normally its non issue :)
            u want to hangle item slection from same slot for all tablewidgets or why
            do u need name ?

            P Offline
            P Offline
            Piyush
            wrote on last edited by
            #6

            @mrjj Well, the bigger picture is, I want to copy selected cells irrespective of the tablewidget I choose. Currently, I have a code by which I can copy selected cells for a particular table. But I want to make it dynamic in the sense that I do not have to provide tablewidget name manually. It should automatically understand which tablewidget has the selected cells.

            def keyPressEvent(self, e):
                if (e.modifiers() & QtCore.Qt.ControlModifier):
                    self.table = self.ui.tableWidget
                    selected = self.table.selectedRanges()
                    s = ''
                    if e.key() == QtCore.Qt.Key_C: #copy
                        if self.table.horizontalHeaderItem(0):
                            s = '\t'+"\t".join([str(self.table.horizontalHeaderItem(i).text()) for i in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1)])
                            s = s + '\n'
            
                        for r in xrange(selected[0].topRow(), selected[0].bottomRow()+1):
                            if self.table.verticalHeaderItem(0):
                                s += self.table.verticalHeaderItem(r).text() + '\t'
                            for c in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1):
                                try:
                                    s += str(self.table.item(r,c).text()) + "\t"
                                except AttributeError:
                                    s += "\t"
                            s = s[:-1] + "\n" #eliminate last '\t'
                        self.clip.setText(s)
            
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Ok so its for generic handling.

              If using signals
              there is the sender() to know whom emitted the signal.

              I assume QTableWidgetItem have a parent or something like that that would also tell you name.
              (dont have docs here. if not, you can subclass and add it)
              and there is QTableWidgetItem::setData() where you can simply stuff the info and read when needed.
              ( this should be pretty easy even if a bit wastefull since all items then have it :)

              P 1 Reply Last reply
              2
              • mrjjM mrjj

                Ok so its for generic handling.

                If using signals
                there is the sender() to know whom emitted the signal.

                I assume QTableWidgetItem have a parent or something like that that would also tell you name.
                (dont have docs here. if not, you can subclass and add it)
                and there is QTableWidgetItem::setData() where you can simply stuff the info and read when needed.
                ( this should be pretty easy even if a bit wastefull since all items then have it :)

                P Offline
                P Offline
                Piyush
                wrote on last edited by
                #8

                @mrjj i tried using self.sender(), but it gives the mainwindow name.
                How do i subclass it and get the name?

                jsulmJ 1 Reply Last reply
                0
                • P Piyush

                  @mrjj i tried using self.sender(), but it gives the mainwindow name.
                  How do i subclass it and get the name?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Piyush Where did you use sender()?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  P 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @Piyush Where did you use sender()?

                    P Offline
                    P Offline
                    Piyush
                    wrote on last edited by Piyush
                    #10

                    @jsulm I added it in the keypressevent function shared above, which gives me mainwindow as the sender. This was my bad.

                    However, now i created a cellclicked signal for all the tables as below to get the name and it works. Now the issue is keypressevent function is not responding (not copying the selected values). I made self.table as a class global variable, in which i have both the functions

                        def __init__(self):
                            self.table = ''
                            self.ui.table1.cellClicked.connect(self.get_obj_name)
                            self.ui.table2.cellClicked.connect(self.get_obj_name)
                            self.ui.table3.cellClicked.connect(self.get_obj_name)
                            
                            
                        def get_obj_name(self, row, col):
                            abc= self.sender()
                            table = abc.objectName()
                            self.table = "self.ui." + table
                    

                    its a type issue i guess. The string i made is <type 'unicode'>, however, I should pass <class 'PyQt5.QtWidgets.QTableWidget'>

                    How do i correct this issue?

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Piyush
                      wrote on last edited by
                      #11

                      Got it. just had to equate self.table = QTableWdiget().

                      Thanks guys, for the patience and help.

                      1 Reply Last reply
                      2

                      • Login

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