Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyQt] Qtableview adapte size to

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 :

When I pull the window on the right, it should horizontaly adjusts:

But at the moment it does nothing:

So I tried many codes with QHboxLayout and other, but nothing works, the last I tried was this for instance:

      def initUI(self):

        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.resize(800, 600)  
But the result is even worse.


Thank you in advance for you help.
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):
  menubar = self.menuBar()
  fileMenu = menubar.addMenu('File')
  self.mainlayout = QVBoxLayout(self)
  self.table_view = QTableView()
  self.model = pandasModel(df)
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.
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.
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):
    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 ?
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.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyGUI] Showing text in QTableView sequence 0 705 Jan-20-2019, 05:00 PM
Last Post: sequence
  [PySide2][PyQt5] update QTableView cpuin 0 2,050 Mar-07-2018, 10:20 PM
Last Post: cpuin
  Change color of a row of a QTableView? panoss 7 16,741 Sep-23-2017, 03:02 PM
Last Post: Mafireyi
  Displaying error in QTableview and with Checkbox in Column under macOsX nieselfriem 1 2,554 Mar-19-2017, 10:43 PM
Last Post: nilamo
  PyGtk3 why is Locale Folder Size Half of Module Size ? harun2525 1 1,584 Mar-09-2017, 03:46 AM
Last Post: Analyser

Forum Jump:

Users browsing this thread: 1 Guest(s)