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:
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:
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_()