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. How to create a resizable table widget?
Forum Updated to NodeBB v4.3 + New Features

How to create a resizable table widget?

Scheduled Pinned Locked Moved Solved Qt for Python
13 Posts 3 Posters 3.9k Views
  • 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.
  • P Offline
    P Offline
    Prathamesh
    wrote on 31 May 2021, 07:56 last edited by
    #1

    I want to create a resizable table widget, and this table should pop-up after clicking a button. How can it be done using python code? Or I have to do it using Qt designer only?

    J 1 Reply Last reply 31 May 2021, 07:58
    0
    • P Prathamesh
      31 May 2021, 09:35

      @JonB I am using PyQt. I have 2 approaches for my table widget. One is directly writing a code .
      And the other one is creating layout in designer.

      I have created a message-box and put a table inside it using following code -->

      class ShortCutsInfo(QtWidgets.QMessageBox):
      """
      Class for displaying a pop up for showing information about short cuts
      """

      def __init__(self):
          QtWidgets.QMessageBox.__init__(self)
          self.table_widget = QtWidgets.QTableWidget()
          self.setWindowTitle('Short Cuts')
          self.setStandardButtons(QtWidgets.QMessageBox.Ok)
          self.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname(
              os.path.realpath(__file__)), 'gui_images', 'logo_icon.png'))))
          self.add_table_widget(self)
          self.exec_()
      
      def add_table_widget(self, parent_item):
          """
          Method to create a table having short cuts information
      
          :param parent_item: used for adding table widget to a QMessageBox
          """
          self.table_widget = QtWidgets.QTableWidget(parent_item)
          self.table_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
          row_count = (len(SHORT_CUTS))
          column_count = (len(SHORT_CUTS[0]))
          self.table_widget.setColumnCount(column_count)
          self.table_widget.setRowCount(row_count)
          self.table_widget.setHorizontalHeaderLabels((list(SHORT_CUTS[0].keys())))
          for row in range(row_count):
              for column in range(column_count):
                  item = (list(SHORT_CUTS[row].values())[column])
                  self.table_widget.setItem(row, column, QtWidgets.QTableWidgetItem(item))
      
      def event(self, e):
          """
          This virtual function receives events to an object and should return true if the event e
          was recognized and processed.
          """
          result = QtWidgets.QMessageBox.event(self, e)
          self.setMinimumWidth(50)
          self.setMaximumWidth(380)
          self.setMinimumHeight(50)
          self.setMaximumHeight(250)
          self.setSizePolicy(
              QtWidgets.QSizePolicy.Expanding,
              QtWidgets.QSizePolicy.Expanding
          )
          self.resize(550, 300)
      
          return result
      

      This way I can see a table after clicking a button. But by this approch, the message-box is not getting resized manually by dragging it. So in future if we need to add any new entries in table we have to resize message-box in code. So is there any way to resize a message-box in code itself so that the table present inside it will also get adapted according to the size of message-box?

      J Offline
      J Offline
      JonB
      wrote on 31 May 2021, 10:40 last edited by
      #12

      @Prathamesh
      I don't know what you're doing. This says nothing about which approach you are taking to process your .ui file --- running a uic to generate a class or loading it at runtime. I thought you said you had designed the QTableWidget in the Designer? Yet you create it in code. Plus you create two of them, one in __init__() and another one in add_table_widget(). And do not call QMessageBox.exec_() inside QMessageBox.__init__(), call exec_() from the outside world when you actually want to display the message box. And do not do resizing unconditionally in event(), that's far too frequent. And I don't see that the table widget is put on any kind of layout, which should be needed for resizing.

      I leave it to someone else, because there is a lot to do here.

      P 1 Reply Last reply 31 May 2021, 11:21
      0
      • P Prathamesh
        31 May 2021, 07:56

        I want to create a resizable table widget, and this table should pop-up after clicking a button. How can it be done using python code? Or I have to do it using Qt designer only?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 31 May 2021, 07:58 last edited by
        #2

        @Prathamesh Simply put the table into a layout, then it will resize if widgets is resized...

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

        P 2 Replies Last reply 31 May 2021, 08:08
        2
        • J jsulm
          31 May 2021, 07:58

          @Prathamesh Simply put the table into a layout, then it will resize if widgets is resized...

          P Offline
          P Offline
          Prathamesh
          wrote on 31 May 2021, 08:08 last edited by
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • J jsulm
            31 May 2021, 07:58

            @Prathamesh Simply put the table into a layout, then it will resize if widgets is resized...

            P Offline
            P Offline
            Prathamesh
            wrote on 31 May 2021, 08:32 last edited by Prathamesh
            #4

            @jsulm I have created a layout and a put a tree widget inside it (I have been asked to do so). Then I have written a code for adding data to the table as follows -->

            def add_table_widget():
            """
            Method to create a table having short cuts information
            """
            table_widget = QtWidgets.QTableWidget()
            table_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
            row_count = (len(SHORT_CUTS))
            column_count = (len(SHORT_CUTS[0]))
            table_widget.setColumnCount(column_count)
            table_widget.setRowCount(row_count)
            table_widget.setHorizontalHeaderLabels((list(SHORT_CUTS[0].keys())))
            for row in range(row_count):
            for column in range(column_count):
            item = (list(SHORT_CUTS[row].values())[column])
            table_widget.setItem(row, column, QtWidgets.QTableWidgetItem(item))

            Here SHORT_CUTS is a dictionary which I have created which is containg data to be filled in table.
            Now how do I call or connect my newly created ui file in my function?

            And please point out if there is any mistake in a code as I am new to qt5. Thank you

            J J 2 Replies Last reply 31 May 2021, 08:36
            0
            • P Prathamesh
              31 May 2021, 08:32

              @jsulm I have created a layout and a put a tree widget inside it (I have been asked to do so). Then I have written a code for adding data to the table as follows -->

              def add_table_widget():
              """
              Method to create a table having short cuts information
              """
              table_widget = QtWidgets.QTableWidget()
              table_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
              row_count = (len(SHORT_CUTS))
              column_count = (len(SHORT_CUTS[0]))
              table_widget.setColumnCount(column_count)
              table_widget.setRowCount(row_count)
              table_widget.setHorizontalHeaderLabels((list(SHORT_CUTS[0].keys())))
              for row in range(row_count):
              for column in range(column_count):
              item = (list(SHORT_CUTS[row].values())[column])
              table_widget.setItem(row, column, QtWidgets.QTableWidgetItem(item))

              Here SHORT_CUTS is a dictionary which I have created which is containg data to be filled in table.
              Now how do I call or connect my newly created ui file in my function?

              And please point out if there is any mistake in a code as I am new to qt5. Thank you

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 31 May 2021, 08:36 last edited by
              #5

              @Prathamesh said in How to create a resizable table widget?:

              Now how do I call or connect my newly created ui file in my function?

              What ui file and what function?

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

              P 1 Reply Last reply 31 May 2021, 08:58
              1
              • P Prathamesh
                31 May 2021, 08:32

                @jsulm I have created a layout and a put a tree widget inside it (I have been asked to do so). Then I have written a code for adding data to the table as follows -->

                def add_table_widget():
                """
                Method to create a table having short cuts information
                """
                table_widget = QtWidgets.QTableWidget()
                table_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
                row_count = (len(SHORT_CUTS))
                column_count = (len(SHORT_CUTS[0]))
                table_widget.setColumnCount(column_count)
                table_widget.setRowCount(row_count)
                table_widget.setHorizontalHeaderLabels((list(SHORT_CUTS[0].keys())))
                for row in range(row_count):
                for column in range(column_count):
                item = (list(SHORT_CUTS[row].values())[column])
                table_widget.setItem(row, column, QtWidgets.QTableWidgetItem(item))

                Here SHORT_CUTS is a dictionary which I have created which is containg data to be filled in table.
                Now how do I call or connect my newly created ui file in my function?

                And please point out if there is any mistake in a code as I am new to qt5. Thank you

                J Offline
                J Offline
                JonB
                wrote on 31 May 2021, 08:48 last edited by
                #6

                @Prathamesh
                As @jsulm has just said. At a guess about your situation:

                • Either you are using a UI file from Designer, in which case you don't want to create your own table_widget = QtWidgets.QTableWidget(), you want to use and fill the one created from the UI.

                • Or you are not using a Designer one, in which case you need put the one you create in your own code somewhere onto whatever widget you are displaying.

                P 1 Reply Last reply 31 May 2021, 09:13
                1
                • J jsulm
                  31 May 2021, 08:36

                  @Prathamesh said in How to create a resizable table widget?:

                  Now how do I call or connect my newly created ui file in my function?

                  What ui file and what function?

                  P Offline
                  P Offline
                  Prathamesh
                  wrote on 31 May 2021, 08:58 last edited by
                  #7

                  @jsulm In this ui design file I have created a layout and put a blank tree widget inside it? Now I want to fill that table with data using python code ,so I have written a function as above.

                  J 1 Reply Last reply 31 May 2021, 09:03
                  0
                  • P Prathamesh
                    31 May 2021, 08:58

                    @jsulm In this ui design file I have created a layout and put a blank tree widget inside it? Now I want to fill that table with data using python code ,so I have written a function as above.

                    J Offline
                    J Offline
                    JonB
                    wrote on 31 May 2021, 09:03 last edited by
                    #8

                    @Prathamesh
                    So did you read what I answered? You are in the first of the two bullet point situations.

                    1 Reply Last reply
                    0
                    • J JonB
                      31 May 2021, 08:48

                      @Prathamesh
                      As @jsulm has just said. At a guess about your situation:

                      • Either you are using a UI file from Designer, in which case you don't want to create your own table_widget = QtWidgets.QTableWidget(), you want to use and fill the one created from the UI.

                      • Or you are not using a Designer one, in which case you need put the one you create in your own code somewhere onto whatever widget you are displaying.

                      P Offline
                      P Offline
                      Prathamesh
                      wrote on 31 May 2021, 09:13 last edited by
                      #9

                      @JonB Yeah so I don't have to use QTableWidget here then. Then what can be my code? I have created a layout in designer ,and I will be filling data from python code. This is what I understood.

                      J 1 Reply Last reply 31 May 2021, 09:21
                      0
                      • P Prathamesh
                        31 May 2021, 09:13

                        @JonB Yeah so I don't have to use QTableWidget here then. Then what can be my code? I have created a layout in designer ,and I will be filling data from python code. This is what I understood.

                        J Offline
                        J Offline
                        JonB
                        wrote on 31 May 2021, 09:21 last edited by JonB
                        #10

                        @Prathamesh
                        You want to access a QTableWidget here. But you don't want to create your own via table_widget = QtWidgets.QTableWidget(), you want to access the one which you have already put into your UI.

                        We don't know the code for that, because we don't know which approach you are taking to using or loading that UI file. Don't you already access any other widgets in the UI elsewhere in your code so you know how to get at the table widget? Or you must tell us how your code loads/uses the UI file you created: you don't say whether you are using PyQt or PySide, which is relevant, e.g. see the various approaches possible in, say, https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html.

                        P 1 Reply Last reply 31 May 2021, 09:35
                        3
                        • J JonB
                          31 May 2021, 09:21

                          @Prathamesh
                          You want to access a QTableWidget here. But you don't want to create your own via table_widget = QtWidgets.QTableWidget(), you want to access the one which you have already put into your UI.

                          We don't know the code for that, because we don't know which approach you are taking to using or loading that UI file. Don't you already access any other widgets in the UI elsewhere in your code so you know how to get at the table widget? Or you must tell us how your code loads/uses the UI file you created: you don't say whether you are using PyQt or PySide, which is relevant, e.g. see the various approaches possible in, say, https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html.

                          P Offline
                          P Offline
                          Prathamesh
                          wrote on 31 May 2021, 09:35 last edited by
                          #11

                          @JonB I am using PyQt. I have 2 approaches for my table widget. One is directly writing a code .
                          And the other one is creating layout in designer.

                          I have created a message-box and put a table inside it using following code -->

                          class ShortCutsInfo(QtWidgets.QMessageBox):
                          """
                          Class for displaying a pop up for showing information about short cuts
                          """

                          def __init__(self):
                              QtWidgets.QMessageBox.__init__(self)
                              self.table_widget = QtWidgets.QTableWidget()
                              self.setWindowTitle('Short Cuts')
                              self.setStandardButtons(QtWidgets.QMessageBox.Ok)
                              self.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname(
                                  os.path.realpath(__file__)), 'gui_images', 'logo_icon.png'))))
                              self.add_table_widget(self)
                              self.exec_()
                          
                          def add_table_widget(self, parent_item):
                              """
                              Method to create a table having short cuts information
                          
                              :param parent_item: used for adding table widget to a QMessageBox
                              """
                              self.table_widget = QtWidgets.QTableWidget(parent_item)
                              self.table_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
                              row_count = (len(SHORT_CUTS))
                              column_count = (len(SHORT_CUTS[0]))
                              self.table_widget.setColumnCount(column_count)
                              self.table_widget.setRowCount(row_count)
                              self.table_widget.setHorizontalHeaderLabels((list(SHORT_CUTS[0].keys())))
                              for row in range(row_count):
                                  for column in range(column_count):
                                      item = (list(SHORT_CUTS[row].values())[column])
                                      self.table_widget.setItem(row, column, QtWidgets.QTableWidgetItem(item))
                          
                          def event(self, e):
                              """
                              This virtual function receives events to an object and should return true if the event e
                              was recognized and processed.
                              """
                              result = QtWidgets.QMessageBox.event(self, e)
                              self.setMinimumWidth(50)
                              self.setMaximumWidth(380)
                              self.setMinimumHeight(50)
                              self.setMaximumHeight(250)
                              self.setSizePolicy(
                                  QtWidgets.QSizePolicy.Expanding,
                                  QtWidgets.QSizePolicy.Expanding
                              )
                              self.resize(550, 300)
                          
                              return result
                          

                          This way I can see a table after clicking a button. But by this approch, the message-box is not getting resized manually by dragging it. So in future if we need to add any new entries in table we have to resize message-box in code. So is there any way to resize a message-box in code itself so that the table present inside it will also get adapted according to the size of message-box?

                          J 1 Reply Last reply 31 May 2021, 10:40
                          0
                          • P Prathamesh
                            31 May 2021, 09:35

                            @JonB I am using PyQt. I have 2 approaches for my table widget. One is directly writing a code .
                            And the other one is creating layout in designer.

                            I have created a message-box and put a table inside it using following code -->

                            class ShortCutsInfo(QtWidgets.QMessageBox):
                            """
                            Class for displaying a pop up for showing information about short cuts
                            """

                            def __init__(self):
                                QtWidgets.QMessageBox.__init__(self)
                                self.table_widget = QtWidgets.QTableWidget()
                                self.setWindowTitle('Short Cuts')
                                self.setStandardButtons(QtWidgets.QMessageBox.Ok)
                                self.setWindowIcon(QtGui.QIcon(os.path.abspath(os.path.join(os.path.dirname(
                                    os.path.realpath(__file__)), 'gui_images', 'logo_icon.png'))))
                                self.add_table_widget(self)
                                self.exec_()
                            
                            def add_table_widget(self, parent_item):
                                """
                                Method to create a table having short cuts information
                            
                                :param parent_item: used for adding table widget to a QMessageBox
                                """
                                self.table_widget = QtWidgets.QTableWidget(parent_item)
                                self.table_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
                                row_count = (len(SHORT_CUTS))
                                column_count = (len(SHORT_CUTS[0]))
                                self.table_widget.setColumnCount(column_count)
                                self.table_widget.setRowCount(row_count)
                                self.table_widget.setHorizontalHeaderLabels((list(SHORT_CUTS[0].keys())))
                                for row in range(row_count):
                                    for column in range(column_count):
                                        item = (list(SHORT_CUTS[row].values())[column])
                                        self.table_widget.setItem(row, column, QtWidgets.QTableWidgetItem(item))
                            
                            def event(self, e):
                                """
                                This virtual function receives events to an object and should return true if the event e
                                was recognized and processed.
                                """
                                result = QtWidgets.QMessageBox.event(self, e)
                                self.setMinimumWidth(50)
                                self.setMaximumWidth(380)
                                self.setMinimumHeight(50)
                                self.setMaximumHeight(250)
                                self.setSizePolicy(
                                    QtWidgets.QSizePolicy.Expanding,
                                    QtWidgets.QSizePolicy.Expanding
                                )
                                self.resize(550, 300)
                            
                                return result
                            

                            This way I can see a table after clicking a button. But by this approch, the message-box is not getting resized manually by dragging it. So in future if we need to add any new entries in table we have to resize message-box in code. So is there any way to resize a message-box in code itself so that the table present inside it will also get adapted according to the size of message-box?

                            J Offline
                            J Offline
                            JonB
                            wrote on 31 May 2021, 10:40 last edited by
                            #12

                            @Prathamesh
                            I don't know what you're doing. This says nothing about which approach you are taking to process your .ui file --- running a uic to generate a class or loading it at runtime. I thought you said you had designed the QTableWidget in the Designer? Yet you create it in code. Plus you create two of them, one in __init__() and another one in add_table_widget(). And do not call QMessageBox.exec_() inside QMessageBox.__init__(), call exec_() from the outside world when you actually want to display the message box. And do not do resizing unconditionally in event(), that's far too frequent. And I don't see that the table widget is put on any kind of layout, which should be needed for resizing.

                            I leave it to someone else, because there is a lot to do here.

                            P 1 Reply Last reply 31 May 2021, 11:21
                            0
                            • J JonB
                              31 May 2021, 10:40

                              @Prathamesh
                              I don't know what you're doing. This says nothing about which approach you are taking to process your .ui file --- running a uic to generate a class or loading it at runtime. I thought you said you had designed the QTableWidget in the Designer? Yet you create it in code. Plus you create two of them, one in __init__() and another one in add_table_widget(). And do not call QMessageBox.exec_() inside QMessageBox.__init__(), call exec_() from the outside world when you actually want to display the message box. And do not do resizing unconditionally in event(), that's far too frequent. And I don't see that the table widget is put on any kind of layout, which should be needed for resizing.

                              I leave it to someone else, because there is a lot to do here.

                              P Offline
                              P Offline
                              Prathamesh
                              wrote on 31 May 2021, 11:21 last edited by Prathamesh
                              #13
                              This post is deleted!
                              1 Reply Last reply
                              0

                              1/13

                              31 May 2021, 07:56

                              • Login

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