Python Forum
[PyQt] Qtableview adapte size to
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Qtableview adapte size to
#1
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.
Reply
#2
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.
Reply
#3
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 ?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 QTableView SQLite : edit cell HeinKurz 2 2,286 Mar-27-2023, 10:41 AM
Last Post: HeinKurz
  [PyQt] QStyledItemDelegate and QTableView malonn 1 1,600 Feb-07-2023, 07:15 PM
Last Post: malonn
  [PyQt] QTableView set header labels HeinKurz 2 6,426 Jan-23-2023, 08:46 AM
Last Post: HeinKurz
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 957 Oct-27-2022, 09:29 PM
Last Post: random_nick
  [PyQt] QTableView: scroll to top cell of the screen random_nick 2 2,767 Oct-08-2022, 12:29 AM
Last Post: random_nick
  [PyQt] [Solved]Add a Blank Row To QTableView Extra 3 5,373 Oct-02-2022, 04:53 PM
Last Post: Extra
  How to update the list of a combo box in a QTableView panoss 10 6,132 Feb-05-2022, 03:24 PM
Last Post: panoss
  [PyQt] How to Copy-Paste a table from Office apps to QTableView? Vittorio 5 7,138 Aug-05-2021, 11:14 AM
Last Post: Axel_Erfurt
  [Tkinter] Trying to change font size w/o changing button size python63 3 9,738 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  [PyGUI] Showing text in QTableView sequence 0 3,033 Jan-20-2019, 05:00 PM
Last Post: sequence

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020