在pyqt中为qtablewidget添加布局的正确方法是什么?

问题描述 投票:0回答:1

我已经问了一个类似的问题here,但我想知道向qtablewigets添加布局的正确方法,以及如果他们两个只有3列,如何将2个表小部件并排放在同一个窗口中。

python pyqt pyqt4 qtablewidget
1个回答
0
投票

将表放在QHBoxLayout中。

码:

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore


class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent=parent)
        QtGui.QTableWidget.setMinimumSize(self, 500, 500)
        QtGui.QTableWidget.setWindowTitle(self, "Custom table widget")
        self.table1 = QtGui.QTableWidget()
        self.configureTable(self.table1)

        self.table2 = QtGui.QTableWidget()
        self.configureTable(self.table2)

        self.buttonBox = QtGui.QDialogButtonBox(self)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)

        self.verticalLayout = QtGui.QVBoxLayout(self)

        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.addWidget(self.table1)
        self.horizontalLayout.addWidget(self.table2)

        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addWidget(self.buttonBox)

        self.buttonBox.accepted.connect(self.close)
        self.buttonBox.rejected.connect(self.close)

    def configureTable(self, table):
        rowf = 3
        table.setColumnCount(3)
        table.setRowCount(rowf)
        table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
        table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
        table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
        table.horizontalHeader().setStretchLastSection(True)
        # table.verticalHeader().setStretchLastSection(True)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

图片:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.