微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使 PyQt 窗口或布局适应内容大小

如何解决如何使 PyQt 窗口或布局适应内容大小

在下面的代码中,我在 QTableView 内创建了一个 QMainWindow,但窗口显示为小于表格:

Window not showing entire table

我希望它自动调整大小以显示表格:

Window showing entire table

我该怎么做?我见过几个类似的问题,但答案似乎特定于给定的示例。我希望通过单一设置将窗口配置为自动调整其内容

import sys

from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QVBoxLayout,QWidget,QApplication,QMainWindow,QTableView


class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        super().__init__()

    def data(self,index,role=None):
        if role == Qt.displayRole:
            return 42

    def rowCount(self,index):
        return 3

    def columnCount(self,index):
        return 4


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        l = QVBoxLayout()

        self.table = QTableView()

        self.model = TableModel()
        self.table.setModel(self.model)

        l.addWidget(self.table)

        w = QWidget()
        w.setLayout(l)
        self.setCentralWidget(w)


app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。