4
votes

I would like to remove the blank space that there is in the table I put in my layout:

Blank space

I would like to have something like this:

what I would obtain

This is my code:

import sys
from PyQt4 import QtGui
from PyQt4.QtGui import * 
from PyQt4.QtSql import * 
from PyQt4.QtCore import * 
import sys


class Example(QMainWindow):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()


    def initUI(self):


        self.resize(640, 480)
        self.center()
        self.setWindowTitle('')
        self.setWindowIcon(QtGui.QIcon('icon.ico')) 

        exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)        
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(QtGui.qApp.quit)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)



        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)

        table = QTableWidget()
        db = QSqlDatabase.addDatabase("")
        db.setDatabaseName('xxx.db')

        db.open()

        query = QSqlQuery ("xxxx")
        queryCount = QSqlQuery ("xxxx")
        queryCount.next()
        numberUserRow = queryCount.value(0).toInt()
        table.setColumnCount(2)
        table.setRowCount(numberUserRow[0])

        table.setHorizontalHeaderItem(0, QTableWidgetItem("Post"));
        table.setHorizontalHeaderItem(1, QTableWidgetItem("Data"));


        MSG_POST = 3
        DATA_SALVATAGGIO_POST = 4
        index=0
        while (query.next()):
            table.setItem(index,0,QTableWidgetItem(query.value(MSG_POST).toString()))
            table.setItem(index,1,QTableWidgetItem(query.value(DATA_SALVATAGGIO_POST).toString()))    
            index = index+1

        self.statusBar().showMessage('Ready')
        window = QWidget()

        mainLayout = QGridLayout()

        verticalLayout = QVBoxLayout()
        horizontalLayout = QHBoxLayout()

        horizontalLayout.addWidget(table)
        verticalLayout.addLayout(horizontalLayout)

        mainLayout.addLayout(verticalLayout,0,0)

        window.setLayout(mainLayout)


        self.setCentralWidget(window);
        self.show()


    def center(self):
        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())        

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()    

I tried to put a button on the right of my table, and this helps me to resize the table itself. But I don't need to use a button, so how can I fix the width of my table?

2

2 Answers

3
votes

The best approach for this is to use a QSpacerItem on the right of the table.

    horizontalSpacer = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Expanding, QSizePolicy.Expanding)
    horizontalLayout.addWidget(table)
    # Add the widget and the spacer to the horizontal layout
    horizontalLayout.addItem(horizontalSpacer)

In addition, you are going to need to set the widget Horizontal sizePolicy to Minimum, to get rid of the remaining white space in the table. To achieve this there are different options, depending on your needs. You can stretch size to contents following this, or put a fixed size, which is probably the best to achieve the closest result to your image:

    table.setSizePolicy(QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
    header = table.horizontalHeader()
    table.setMaximumWidth(header.length())
    table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

The result is shown here.

3
votes

This can be achieved very simply like this:

    horizontalLayout.addWidget(table)
    horizontalLayout.addStretch()
    table.horizontalHeader().setResizeMode(QHeaderView.Stretch)

This will add an expanding spacer which pushes the table over to the left, and it will also make the table columns stretch to fill the available space.

If you don't want the table to resize when the main window resizes, do this:

    horizontalLayout.addStretch(1)

Setting a stretch-factor gives priority to the spacer, so the table will be fixed at its minimum width. You can of course adjust that if you wish:

    table.setMinimumWidth(400)