在 expanding/collapsing 附近调整 QWidget 的大小

Rezising QWidget afer expanding/collapsing it

我正在尝试根据它是否 expanded/collapsed 自动调整 QWidget 的大小。我尝试了几种发布方式 here and here.

我没有设法采用这些,因此它按我想要的方式工作:我希望 QWidget 在展开时调整自身大小(现在可以工作)但在折叠它(取消选中 QCheckBox)后它的大小保持不变太大,不会变回原来的状态。

这是我的代码:

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class CheckableComboBox(QComboBox):
    def __init__(self, parent = None):
        super(CheckableComboBox, self).__init__(parent)
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QStandardItemModel(self))

        self.setStyleSheet("QComboBox{"
                           "font-size:11px;"
                           "color:black;"
                           "background-color:white;"
                           "border:1px solid black;"
                           "padding:1px;""}")

        self.setEditable(True)
        self.lineEdit().setAlignment(Qt.AlignCenter)
        self.lineEdit().setReadOnly(True)
        self.setMinimumSize(150, 40)

    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.text() != ' ' and item.checkState() == Qt.Checked:
            item.setCheckState(Qt.Unchecked)
        if item.text() != ' ' and item.checkState() != Qt.Checked:
            item.setCheckState(Qt.Checked)

class Example(QWidget):

    def __init__(self):
        super().__init__()

##        self.setFixedSize(350, 280)
        self.init_UI()

    def init_UI(self):
        self.years = [' ', '2017', '2018', '2019', '2020']
        self.months = [' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
        self.metrics = [' ', 'Seller', 'Section', 'Store']
        self.units = [' ', 'Number of Pieces Sold', 'Total Value']

        self.setWindowTitle('Dialog')

        self.sMetric = QLabel('Select metric(s):')
        self.sUnit = QLabel('Select unit(s):')
        self.okButton = QPushButton('Ok')
        self.cancelButton = QPushButton('Cancel')

        self.okButton.clicked.connect(self.okClicked)
        self.cancelButton.clicked.connect(self.cancelClicked)

        for i in (self.sMetric, self.sUnit, self.okButton, self.cancelButton):
            i.setFixedHeight(40)
            i.setFixedWidth(150)

        self.timeWise = QCheckBox('Time-wise View')
        self.timeWise.stateChanged.connect(self.on_checked)

        self.metricCombo = CheckableComboBox()
        for index, element in enumerate(self.metrics):
            self.metricCombo.addItem(element)
            if index > 0:
                item = self.metricCombo.model().item(index, 0)
                item.setCheckState(Qt.Unchecked)

        self.unitCombo = CheckableComboBox()
        for index, element in enumerate(self.units):
            self.unitCombo.addItem(element)
            if index > 0:
                item = self.unitCombo.model().item(index, 0)
                item.setCheckState(Qt.Unchecked)

        self.grid = QGridLayout()
        self.grid.setSpacing(10)

        self.grid.addWidget(self.sMetric, 1, 0)
        self.grid.addWidget(self.metricCombo, 1, 1)
        self.grid.addWidget(self.sUnit, 2, 0)
        self.grid.addWidget(self.unitCombo, 2, 1)
        self.grid.addWidget(self.timeWise, 3, 0)
        self.grid.addWidget(self.okButton, 6, 0)
        self.grid.addWidget(self.cancelButton, 6, 1)

        self.setLayout(self.grid)

    def on_checked(self, state):
        if state == Qt.Checked:
            print('Checked')

            self.sYear = QLabel('Select year(s):')
            self.sMonth = QLabel('Select month(s):')

            for i in (self.sYear, self.sMonth):
                i.setFixedHeight(40)
                i.setFixedWidth(150)

            self.monthCombo = CheckableComboBox()
            for index, element in enumerate(self.months):
                self.monthCombo.addItem(element)
                if index > 0:
                    item = self.monthCombo.model().item(index, 0)
                    item.setCheckState(Qt.Unchecked)

            self.yearCombo = CheckableComboBox()
            for index, element in enumerate(self.years):
                self.yearCombo.addItem(element)
                if index > 0:
                    item = self.yearCombo.model().item(index, 0)
                    item.setCheckState(Qt.Unchecked)

            self.grid.addWidget(self.sYear, 4, 0)
            self.grid.addWidget(self.yearCombo, 4, 1)
            self.grid.addWidget(self.sMonth, 5, 0)
            self.grid.addWidget(self.monthCombo, 5, 1)

        else:
            print('Unchecked')

            self.sYear.deleteLater()
            self.sMonth.deleteLater()
            self.monthCombo.deleteLater()
            self.yearCombo.deleteLater()

    def okClicked(self):
        self.close()

    def cancelClicked(self):
        self.close()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    main = Example()
    main.show()
    sys.exit(app.exec_())

对如何进行有什么建议吗?

您必须设置包含 QLayout::SetFixedSize 中所有小部件的设计的大小限制:

QLayout::SetFixedSize 3 The main widget's size is set to sizeHint(); it cannot be resized at all.

除此之外,对代码的改进不会创建然后删除小部件,但最好隐藏它们或根据需要使其可见。

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class CheckableComboBox(QComboBox):
    def __init__(self, parent = None):
        super(CheckableComboBox, self).__init__(parent)
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QStandardItemModel(self))

        self.setStyleSheet("QComboBox{"
                           "font-size:11px;"
                           "color:black;"
                           "background-color:white;"
                           "border:1px solid black;"
                           "padding:1px;""}")

        self.setEditable(True)
        self.lineEdit().setAlignment(Qt.AlignCenter)
        self.lineEdit().setReadOnly(True)
        self.setMinimumSize(150, 40)

    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.text() != ' ' and item.checkState() == Qt.Checked:
            item.setCheckState(Qt.Unchecked)
        if item.text() != ' ' and item.checkState() != Qt.Checked:
            item.setCheckState(Qt.Checked)

class Example(QWidget):

    def __init__(self):
        super().__init__()

##        self.setFixedSize(350, 280)
        self.init_UI()

    def init_UI(self):
        self.years = [' ', '2017', '2018', '2019', '2020']
        self.months = [' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
        self.metrics = [' ', 'Seller', 'Section', 'Store']
        self.units = [' ', 'Number of Pieces Sold', 'Total Value']

        self.setWindowTitle('Dialog')

        self.sMetric = QLabel('Select metric(s):')
        self.sUnit = QLabel('Select unit(s):')
        self.okButton = QPushButton('Ok')
        self.cancelButton = QPushButton('Cancel')

        self.okButton.clicked.connect(self.okClicked)
        self.cancelButton.clicked.connect(self.cancelClicked)

        for i in (self.sMetric, self.sUnit, self.okButton, self.cancelButton):
            i.setFixedHeight(40)
            i.setFixedWidth(150)

        self.timeWise = QCheckBox('Time-wise View')
        self.timeWise.toggled.connect(self.on_checked)

        self.metricCombo = CheckableComboBox()
        for index, element in enumerate(self.metrics):
            self.metricCombo.addItem(element)
            if index > 0:
                item = self.metricCombo.model().item(index, 0)
                item.setCheckState(Qt.Unchecked)

        self.unitCombo = CheckableComboBox()
        for index, element in enumerate(self.units):
            self.unitCombo.addItem(element)
            if index > 0:
                item = self.unitCombo.model().item(index, 0)
                item.setCheckState(Qt.Unchecked)

        self.grid = QGridLayout()
        self.grid.setSpacing(10)

        self.grid.addWidget(self.sMetric, 1, 0)
        self.grid.addWidget(self.metricCombo, 1, 1)
        self.grid.addWidget(self.sUnit, 2, 0)
        self.grid.addWidget(self.unitCombo, 2, 1)
        self.grid.addWidget(self.timeWise, 3, 0)
        self.grid.addWidget(self.okButton, 6, 0)
        self.grid.addWidget(self.cancelButton, 6, 1)

        self.setLayout(self.grid)


        self.sYear = QLabel('Select year(s):')
        self.sMonth = QLabel('Select month(s):')

        for i in (self.sYear, self.sMonth):
            i.setFixedHeight(40)
            i.setFixedWidth(150)

        self.monthCombo = CheckableComboBox()
        for index, element in enumerate(self.months):
            self.monthCombo.addItem(element)
            if index > 0:
                item = self.monthCombo.model().item(index, 0)
                item.setCheckState(Qt.Unchecked)

        self.yearCombo = CheckableComboBox()
        for index, element in enumerate(self.years):
            self.yearCombo.addItem(element)
            if index > 0:
                item = self.yearCombo.model().item(index, 0)
                item.setCheckState(Qt.Unchecked)

        self.grid.addWidget(self.sYear, 4, 0)
        self.grid.addWidget(self.yearCombo, 4, 1)
        self.grid.addWidget(self.sMonth, 5, 0)
        self.grid.addWidget(self.monthCombo, 5, 1)
        self.grid.setSizeConstraint(QLayout.SetFixedSize)

        self.on_checked(False)

    def on_checked(self, checked):
        self.sYear.setVisible(checked)
        self.sMonth.setVisible(checked)
        self.monthCombo.setVisible(checked)
        self.yearCombo.setVisible(checked)

    def okClicked(self):
        self.close()

    def cancelClicked(self):
        self.close()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    main = Example()
    main.show()
    sys.exit(app.exec_())