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. Struggling with setting column sizes

Struggling with setting column sizes

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 2.5k 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.
  • P Offline
    P Offline
    Parthe
    wrote on last edited by Parthe
    #1

    Hi,

    I'm very new to Qt (like, this is my second day of coding with it) so I apologise if I'm missing something basic here, or doing something wrong.

    Using Qt6 I have a window I created from the QMainWindow class.
    In it I have a VBoxLayout in which I am adding a table created from QTableView.

    class myMainWindow(QMainWindow):
        def __init__(self):
            # Call the super __init__() function to set up the window via Qt.
            super().__init__()
    
            self.setWindowTitle('This is a test window')
            self.layout = QVBoxLayout()
            self.mainTable = QTableView()
            self.layout.addWidget(self.mainTable)
            self.widget = QWidget()
            self.widget.setLayout(self.layout)
            self.setCentralWidget(self.widget)
    

    I then create a table based on TableModel, passing in a list of lists. This creates the table data correctly.

            table_data=[
    [ 'Mr', 'Joe', 'Baker', 'Mr Baker was a well-known cooked-dough provide in town.', '1944-12-15']
    [ 'Mrs', 'Josephine', 'Bloggs', 'Mrs Bloggs (Jo to her friends) was a regular church goer and was held in high estimation in the village', '1949-06-24']
    [ 'Mstr, 'Harold', 'Bloggs', 'Young Master Bloggs was a tearabout in his youth, but grew up to join in his fathers business', '1965-05-14']
            ]
            # Store the data in the table
            self.model = TableModel(table_data)
    
    

    I then set the model of the QTableView (mainTable) to the TableModel and display the window.

    
            self.mainTable.setModel(self.model)
    
            window.show()  # IMPORTANT!!!!! Windows are hidden by default.
    
            # Start the event loop.
            app.exec()
    
    

    The problem is that I cannot work out how to set the column widths automatically. Ideally I want all columns to fit to the width of the data apart from column 4 (the description) which can be overlapped if the main window is too narrow.

    From the documentation I've read I should be able to do this using the QHeaderView.Stretch and QHeaderView. ResizeToContents attributes, but I cannot find out where or how I should set these.

    Can you help please?

    F 1 Reply Last reply
    0
    • P Parthe

      Hi,

      I'm very new to Qt (like, this is my second day of coding with it) so I apologise if I'm missing something basic here, or doing something wrong.

      Using Qt6 I have a window I created from the QMainWindow class.
      In it I have a VBoxLayout in which I am adding a table created from QTableView.

      class myMainWindow(QMainWindow):
          def __init__(self):
              # Call the super __init__() function to set up the window via Qt.
              super().__init__()
      
              self.setWindowTitle('This is a test window')
              self.layout = QVBoxLayout()
              self.mainTable = QTableView()
              self.layout.addWidget(self.mainTable)
              self.widget = QWidget()
              self.widget.setLayout(self.layout)
              self.setCentralWidget(self.widget)
      

      I then create a table based on TableModel, passing in a list of lists. This creates the table data correctly.

              table_data=[
      [ 'Mr', 'Joe', 'Baker', 'Mr Baker was a well-known cooked-dough provide in town.', '1944-12-15']
      [ 'Mrs', 'Josephine', 'Bloggs', 'Mrs Bloggs (Jo to her friends) was a regular church goer and was held in high estimation in the village', '1949-06-24']
      [ 'Mstr, 'Harold', 'Bloggs', 'Young Master Bloggs was a tearabout in his youth, but grew up to join in his fathers business', '1965-05-14']
              ]
              # Store the data in the table
              self.model = TableModel(table_data)
      
      

      I then set the model of the QTableView (mainTable) to the TableModel and display the window.

      
              self.mainTable.setModel(self.model)
      
              window.show()  # IMPORTANT!!!!! Windows are hidden by default.
      
              # Start the event loop.
              app.exec()
      
      

      The problem is that I cannot work out how to set the column widths automatically. Ideally I want all columns to fit to the width of the data apart from column 4 (the description) which can be overlapped if the main window is too narrow.

      From the documentation I've read I should be able to do this using the QHeaderView.Stretch and QHeaderView. ResizeToContents attributes, but I cannot find out where or how I should set these.

      Can you help please?

      F Offline
      F Offline
      friedemannkleint
      wrote on last edited by
      #2

      You can call .QTableView.resizeColumnToContents(column), QTableView.resizeColumnsToContents() ( see https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QTableView.html#PySide6.QtWidgets.PySide6.QtWidgets.QTableView.resizeColumnToContents ) .

      P 1 Reply Last reply
      1
      • F friedemannkleint

        You can call .QTableView.resizeColumnToContents(column), QTableView.resizeColumnsToContents() ( see https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QTableView.html#PySide6.QtWidgets.PySide6.QtWidgets.QTableView.resizeColumnToContents ) .

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

        Excellent! Thanks @friedemannkleint , that did the job nicely.

        For the sake of completeness and to help others who might have this issue in the future, I modified my code as follows.

            self.mainTable.setModel(self.model)
        
            # Now that the table has the data and the view is set to the table,
            # resize the columns correctly. Ignore column 3 since the contents
            # can be very large, we want that to overlap.
            for r in range(self.model.rowCount()):
                if r == 2:
                    continue
        
                self.mainTable.resizeColumnToContents(r)
        

        Next I need to work on setting the resize settings to the columns to negate the need for this, but that's a different problem.

        1 Reply Last reply
        0
        • P Parthe has marked this topic as solved on

        • Login

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