0

In a sample Qt application, I create a QMainWindow with a QTableView as its central widget (see code at the end of question).

Importantly, the size of the main window is computed to fit the table:

Properly sized window

However, if I add a status bar, the size is not longer computed to fit the table completely. There is a little bit of table that is hidden, which prompts the table view to add scroll bars:

Improperly sized window

This seems like it should not be happening. Just adding a status bar should not prevent the main window from fitting its contents accordingly.

How can this be avoided without having to manually compute the necessary size (which would of course not be easily maintainable as it would have to be recomputed every time the design and widgets in the window change)?

The code is below. It's in PyQt but it is not Python-specific and I believe the same problem should occur in C++.

import sys

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QStatusBar

use_status_bar = False  # includes a status bar; but also messes up the size adjustment


class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        super().__init__()

    def data(self, index, role=None):
        if role == Qt.DisplayRole:
            return 42

    def rowCount(self, index):
        return 10

    def columnCount(self, index):
        return 4


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("A simple table app")

        self.tableView = QTableView()

        self.model = TableModel()
        self.tableView.setModel(self.model)
        self.tableView.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)

        if use_status_bar:
            self.setStatusBar(QStatusBar(self))

        self.setCentralWidget(self.tableView)


app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
user118967
  • 4,895
  • 5
  • 33
  • 54

1 Answers1

0

In PyQt5-5.15.1 it works as intended if you add

self.tableView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.tableView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

(otherwise there will be two blank areas to the left and to the bottom of the table).

Which version or PyQt5 do you use?

mugiseyebrows
  • 4,138
  • 1
  • 14
  • 15
  • I am using the same version. Yes, it works without the scroll bars, but we shouldn't have to turn scroll bars off in order to get the window properly sized just because we added a status bar. – user118967 Apr 18 '21 at 20:18
  • That is strange, I cannot reproduce second image, everything works like it should – mugiseyebrows Apr 18 '21 at 22:17
  • Thanks for checking. However, if things were working, why did you say in your answer it works as intended *if* one removed scroll bars? And, BTW, which OS are you using? I'm on Windows 10. – user118967 Apr 18 '21 at 23:57
  • if i dont remove scrollbars I get blank areas next to the table not scrolbars like on image2. Im on win10 too. https://i.imgur.com/9BcvYif.png – mugiseyebrows Apr 19 '21 at 16:34