Python Forum
[PyQt] Qtableview adapte size to - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] Qtableview adapte size to (/thread-25180.html)



Qtableview adapte size to - WBPYTHON - Mar-22-2020

Hello,

Is it possible to adapte the size of a Qtableview to the main window ? when I pull the window on the right side, I would like the Qtableview to adapt and expand horizontaly. But I don't want it to fill ALL the main window.

I have this :
[Image: 1584902327-capture1.png]
When I pull the window on the right, it should horizontaly adjusts:
[Image: 1584902332-capture2.png]
But at the moment it does nothing:
[Image: 1584902336-capture3.png]
So I tried many codes with QHboxLayout and other, but nothing works, the last I tried was this for instance:

      def initUI(self):
        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')
    
        self.centralwidget = QWidget(self)
        self.horizontalLayoutWidget = QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(30, 10, 721, 171)
        self.horizontalLayout = QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.widget = QWidget(self.horizontalLayoutWidget)
        self.table_view = QTableView(self.widget)
        
        self.model = pandasModel(df)

        self.table_view.setModel(self.model)
        self.table_view.show()
        self.table_view.resizeColumnsToContents()
        
        self.setCentralWidget(self.centralwidget)
        self.resize(800, 600)  
        self.setWindowTitle('test')
        
    
        self.show()
But the result is even worse.

[Image: 1584902340-capture4.png]

Thank you in advance for you help.


RE: Qtableview adapte size to - deanhystad - Mar-22-2020

I think you are thinking about this problem the wrong way.

In Qt you can set the position and size of each widget explicitly or you can put the widgets in layouts and let the layout managers handle things. You are trying to do half and half and that does not work well.

I would start by paring down you code to something like this:
def initUI(self):
  self.statusBar()
 
  menubar = self.menuBar()
  fileMenu = menubar.addMenu('File')
     
  self.mainlayout = QVBoxLayout(self)
  self.table_view = QTableView()
  self.mainlayout.addWidget(self.tableView)
         
  self.model = pandasModel(df)
 
  self.table_view.setModel(self.model)
  self.table_view.show()
  self.table_view.resizeColumnsToContents()
         
  self.setWindowTitle('test')  
  self.show()
The table_layout should fill the entire window except the surrounding margins. If you resize the window the table_layout will resize along with it. I know that you want the bottom half of the window empty, but my guess is you plan to add widgets to the bottom half of the window. If these widgets are added to the layout manager, the amount of vertical space the table gets will automatically shrink to allow space for the added widgets.


RE: Qtableview adapte size to - WBPYTHON - Mar-22-2020

Thank you (again) for your help. When executing your code, it displays the Qtableview in another window.
And I also have this error message in terminal.
Error:
QLayout: Attempting to add QLayout "" to Window1 "", which already has a layout
Window1 is the name of my MainWindow class :

 class Window1(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
               
    def initUI(self):  
You are right, I plan to add stuff in the bottom but what if I don't want the other widgets to fit with main window ? they will be hidden by Qtableview ? And if it put a Qframe, that I add to layout manager, since I don't add geometry, will it share the place equaly with the tableview ?


RE: Qtableview adapte size to - deanhystad - Mar-23-2020

Sorry about the error. I don't have pandas and I was winging it. I also don't use QMainWindow all that much. Your were correct to create a widget and assign it as the central widget.

The layout widgets do their best to adjust widget sizes to fill the available space. The only time widgets get squeezed out is when widgets are crushed down to their minimum size and the window is shrunk even further. Normally that's not even possible because the layout tells the desktop manager what the minimum size is for the window.

The window manager adjusts widget sizes based on size hints set for the widget and by the stretch factors set in the layout. You should start by just adding all your widgets to layout managers and seeing what the results look like. I usually start with a vertical box layout and then add rows. A row can be a single widget, or I can create a horizontal box layout to have multiple widgets oriented horizontally. There is also a grid layout if your layout follows a grid pattern. Start playing with these to learn how they work and then you can start thinking about tweaking things. For example, you can set a minimum and/or maximum size of a widget in a layout and the layouts will adjust. You can set different stretch factors for different widgets, so they don't grow or shrink at the same rate.