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