How do I get rid of this whitespace in my QTableWi

2019-04-02 07:12发布

问题:

How do I get rid of the whitespace in my application:

I would like to get rid of the whitespace in my QTableWidget (blue arrow). How do I do it?

Here is the code for my application:

    gridLayout = QGridLayout()

    #add other widgets

    currentItemsTable = QTableWidget(4, 2)
    currentDeviceIconLabel = QLabel("Current Device Icon: ")
    self.currentDeviceIcon = QLabel()
    currentAppIconLabel = QLabel("Current App Icon: ")
    self.currentAppIcon = QLabel()
    currentTitleLabel = QLabel("Current Title: ")
    self.currentTitle = QLabel(self.getCurrentTitle())
    currentSubtitleLabel = QLabel("Current Subtitle: ")
    self.currentSubtitle = QLabel(self.getCurrentSubtitle())

    currentItemsTable.setCellWidget(0, 0, currentDeviceIconLabel)
    currentItemsTable.setCellWidget(0, 1, self.currentDeviceIcon)
    currentItemsTable.setCellWidget(1, 0, currentAppIconLabel)
    currentItemsTable.setCellWidget(1, 1, self.currentAppIcon)
    currentItemsTable.setCellWidget(2, 0, currentTitleLabel)
    currentItemsTable.setCellWidget(2, 1, self.currentTitle)
    currentItemsTable.setCellWidget(3, 0, currentSubtitleLabel)
    currentItemsTable.setCellWidget(3, 1, self.currentSubtitle)
    currentItemsTable.horizontalHeader().hide()
    currentItemsTable.resizeColumnsToContents()
    currentItemsTable.resizeRowsToContents()
    currentItemsTable.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
    gridLayout.addWidget(currentItemsTable, 1, 4, 8, 2)

Additionally, how do I get the QTableWidget to resize with the row and column contents? I don't want any scroll bars.

回答1:

I think you should be using this:

currentItemsTable.horizontalHeader().setStretchLastSection(True)
currentItemsTable.verticalHeader().setStretchLastSection(True)

You should call this after resize....toContents() and any time you are going to resize for contents you should first setStretchLastSection(False), something like:

currentItemsTable.verticalHeader().setStretchLastSection(False)
currentItemsTable.resizeRowsToContents()
currentItemsTable.verticalHeader().setStretchLastSection(True)

Or you will get strange effects on resize.