我想删除我在版面上放置的空白区域:
我想要这样的东西:
这是我的代码:

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

我试着在我的桌子右边放一个按钮,这有助于我调整桌子本身的大小但是我不需要用按钮,所以我怎样才能固定桌子的宽度呢?

最佳答案

最好的方法是使用表格右侧的QSpacerItem

    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)

此外,您需要将小部件水平大小策略设置为最小,以消除表中剩余的空白。为了达到这个目的,根据你的需要,有不同的选择。您可以将大小拉伸到this之后的内容,或者设置一个固定大小,这可能是获得与图像最接近的结果的最佳选择:
    table.setSizePolicy(QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
    header = table.horizontalHeader()
    table.setMaximumWidth(header.length())
    table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

结果显示here

09-30 15:38
查看更多