[PySide] Nicely resizing a QTableWidget inside a QMainWindow

chaica at ohmytux.com chaica at ohmytux.com
Tue Aug 21 15:01:14 CEST 2012


Hi,

I've got some issues with  QTableWidget inside a QMainWindow.

My problem is that when resizing the main window, the QTableWidget 
does not follow and does not resize accordingly to its main window. I 
tried several ways to correct it but could not succeed. Help will be 
greatly appreciated.

The code follows:

class DrawTrackerth(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        # create a QT table widget with len(players) numbers of rows 
and 4 columns
        ...some stuff
        columnnames = ['foo', 'bar']
        tableWidget = QTableWidget(len(lines), len(columnnames))
        tableWidget.setHorizontalHeaderLabels(columnnames)
        for line in lines:
            newItem1 = QTableWidgetItem(line)
            tableWidget.setItem(..., 0, newItem1)
            newItem2 = QTableWidgetItem(line)
            tableWidget.setItem(..., 1, newItem2)
        tableWidget.resizeColumnsToContents()
        tableWidget.resizeRowsToContents()
        exitaction = QAction(QIcon('exit.png'), '&Exit', self)
        exitaction.setShortcut('Ctrl+Q')
        exitaction.setStatusTip('Exit application')
        exitaction.triggered.connect(self.close)
        self.statusBar()
        # define the menu bar
        menubar = self.menuBar()
        filemenu = menubar.addMenu('&Commands')
        filemenu.addAction(exitaction)
        # define the table as central in the window
        self.setCentralWidget(tableWidget)
        self.setWindowTitle('MyApp')
        self.resize(800,600)
        self.show()

Regards,
Carl Chenet




More information about the PySide mailing list