如何在 pyqt 中向 Qtablewidget 添加布局? [英] How do I add a layout to a Qtablewidget in pyqt?

查看:57
本文介绍了如何在 pyqt 中向 Qtablewidget 添加布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 qtablewidget 定义如下:

I have my qtablewidget defined like this:

def __init__(self, parent = None):
        super(Window, self).__init__(parent)
        QtGui.QWidget.__init__(self)
        QtGui.QTableWidget.setMinimumSize(self, 500, 500)
        QtGui.QTableWidget.setWindowTitle(self, "Custom table widget")
        self.table = QtGui.QTableWidget()
        rowf = 3
        self.table.setColumnCount(3)
        self.table.setRowCount(rowf)
        self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
        self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
        self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
        self.table.verticalHeader().hide()

        header = self.table.horizontalHeader()
        header.setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
        header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
        header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)

        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.verticalLayout.addWidget(self.table)
        self.verticalLayout.addWidget(self.buttonBox)

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

我希望我的最终结果看起来类似于下图,但现在,我尝试添加的布局并没有像我希望的那样安静地工作.我是pyqt的初学者.我之前在 qlistview 上尝试过这种布局,效果很好.

I would like my end result to look something similar to the pic below but right now, the layout that I'm trying to add doesn't quiet work the way I'd like it to. I'm a beginner at pyqt. I've tried this layout before on a qlistview and it worked well.

推荐答案

add {your table}.table.horizo​​ntalHeader().setStretchLastSection(True) 和/或 {your table}.verticalHeader().setStretchLastSection(True)

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.table = QtGui.QTableWidget()
        rowf = 3
        self.table.setColumnCount(3)
        self.table.setRowCount(rowf)
        self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
        self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
        self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
        self.table.horizontalHeader().setStretchLastSection(True)
        # self.table.verticalHeader().setStretchLastSection(True)

        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.verticalLayout.addWidget(self.table)
        self.verticalLayout.addWidget(self.buttonBox)

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

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

仅水平:

仅垂直:

垂直和水平:

这篇关于如何在 pyqt 中向 Qtablewidget 添加布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆