2

I have a QTableView that displays some data from sqlite database (currently 2 rows and 2 columns). The QTableView is inside a QFrame that in turn is the central widget of QMainWindow. The QFrame uses QVBoxLayout. The problem is that when I add the QTableView to the QVBoxLayout, there's too much space given to the QTableView. As you can see from the picture, there's a white space right to the last column and a white space below the last row.

I've tried different SizePolicies, resizes(), sizeHints() on the tableView and on the QFrame, but nothing seems to work. I have not changed default SizePolicy or anything. Now, I can use the solutions suggested in this question (calculating the height and width of the table and setting maximum size for the tableview) and it would even be an acceptable solution to my application. But the question is shouldn't the tableView be given just the right amount of space with all the default sizePolicies? I mean, am I doing something wrong that causes this behaviour to happen?

The picture:

QTableView given too much space

The code:

import sys
from PySide import QtGui, QtSql, QtCore

class MainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.initUI()

    def initUI(self):

        #-------
        #CREATE WIDGETS
        #-------
        frame = QtGui.QFrame()

        someLabel = QtGui.QLabel("SomeLabel")
        someOtherLabel = QtGui.QLabel("SomeOtherLabel")
        self.tableView = QtGui.QTableView()

        db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
        db.setDatabaseName('database.db')
        db.open()

        tableViewModel = QtSql.QSqlQueryModel()
        tableViewModel.setQuery('SELECT currencySymbol, balanceAmount FROM cashBalances', db)
        tableViewModel.setHeaderData(0, QtCore.Qt.Horizontal, "Currency") #set column names
        tableViewModel.setHeaderData(1, QtCore.Qt.Horizontal, "Balance")
        self.tableView.setModel(tableViewModel)
        #--------
        #CREATE LAYOUT
        #--------

        self.setCentralWidget(frame)
        frameLayout = QtGui.QVBoxLayout()
        frameLayout.addWidget(someLabel)
        frameLayout.addWidget(self.tableView)
        frameLayout.addWidget(someOtherLabel)
        frame.setLayout(frameLayout)

        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
Community
  • 1
  • 1
flouwer
  • 327
  • 1
  • 4
  • 13
  • 1
    All scrollable views behave in this way. A list-widget doesn't resize when items are added/removed - would you expect it to? – ekhumoro Feb 17 '16 at 19:54
  • @ekhumoro When I remove the QTableView from the layout the MainWindow becomes just as small that it nicely fits the two labels (no extra space anywhere, it fits snugly). That is why I tought that QTableView would take into account how many rows/columns I'm displaying and allocating just enough space to fit everything perfectly. In my example it is possible to display 6 rows (I think) without the QTableView becoming scrollable. But thank you for your comment, as I understand there's some default size given to QTableView when I add it to layout and I have to do the resizing myself. – flouwer Feb 18 '16 at 12:22

1 Answers1

1

for the columns your issue can be solved easily. For the rows it depends a bit on what you want.

You can specify which of the columns should stretch if not many. For this, you usually use the header views, e.g.

horizontalHeader()->setStretchLastSection(true);

or

horizontalHeader()->setResizeMode(0 /* first column */, QHeaderView::Stretch);

Probably you can do something similar for the rows but what exactly would you want? Stretching the rows to the full height would not be that nice, probably. And the label should not stretch, either. If you want just some area that is neither used by the table or the label and this area should lie below the table, you might try to insert a spacer item.

I know that these kinds of things are a bit annoying... first step should always be to use the header views.

IceFire
  • 4,016
  • 2
  • 31
  • 51