gpt4 book ai didi

python - 隐藏小部件后调用 size() 时应用程序大小不会改变

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:55 26 4
gpt4 key购买 nike

在隐藏小部件并将其 sizePolicy 设置为 Ignored 后,我正在尝试将我的应用调整为较小的尺寸。但是,应用程序不会调整到比以前更小,就好像隐藏的小部件仍然在那里占用空间一样。

在隐藏第二个小部件/将其 sizePolicy 设置为 Ignored 并在尝试调整应用程序大小之前,但它们似乎都没有任何效果。

class App(QWidget):
def __init__(self):
super().__init__()

# basic setup
self.move(100, 100)

# main widgets
self.advanced_metadata = QCheckBox("Advanced", self)
self.advanced_metadata.stateChanged.connect(self.displayAdvanced)
self.first_widget = QLabel('first_widget', self)
self.second_widget = QLabel('second_widget', self)
self.second_widget.setHidden(True)
self.second_widget.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

# main layout
self.setLayout(QVBoxLayout())
self.layout().addWidget(self.advanced_metadata)
self.layout().addWidget(self.first_widget)
self.layout().addWidget(self.second_widget)
self.layout().addStretch()

self.resizeApp(650, self.sizeHint().height())

self.show()

# toggles advanced metadata view
def displayAdvanced(self):
if self.advanced_metadata.isChecked():
self.second_widget.setVisible(True)
self.second_widget.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)

self.resizeApp(650, self.sizeHint().height())

else:
self.second_widget.setHidden(True)
self.second_widget.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

self.resizeApp(650, self.sizeHint().height())

# resizes main window
def resizeApp(self, width, height):
self.resize(width, height)
print(self.size())

if __name__ == '__main__':
# accounts for exiting errors in python
if not QApplication.instance():
app = QApplication(sys.argv)
else:
app = QApplication.instance()
ex = App()
app.exec_()

我希望窗口的大小初始化为650x112,然后选择高级选择复选框时,大小应更改为650x149,最后,当高级>复选框未选中,大小应恢复为 650x112。最后的期望没有发生;相反,窗口保持在 650x149。

最佳答案

在 Qt 中,许多任务都是异步完成的,几何就是这种情况,因此即使您更改了策略,它也不会立即应用,直到同步部分结束。因此,一种解决方法是使用 QTimer.singleShot(0, ...) 稍后应用调整大小。

from functools import partial
from PyQt5 import QtCore, QtWidgets


class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.move(100, 100)
# main widgets
self.advanced_metadata = QtWidgets.QCheckBox(
"Advanced", stateChanged=self.displayAdvanced
)
self.first_widget = QtWidgets.QLabel("first_widget")
self.second_widget = QtWidgets.QLabel("second_widget")
self.second_widget.setHidden(True)
self.second_widget.setSizePolicy(
QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored
)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.advanced_metadata)
lay.addWidget(self.first_widget)
lay.addWidget(self.second_widget)
lay.addStretch()

self.resizeApp(650, self.sizeHint().height())

# toggles advanced metadata view
@QtCore.pyqtSlot(int)
def displayAdvanced(self, state):
if state == QtCore.Qt.Checked:
self.second_widget.show()
self.second_widget.setSizePolicy(
QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred,
)

else:
self.second_widget.hide()
self.second_widget.setSizePolicy(
QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored
)
wrapper = partial(self.resizeApp, 650, self.sizeHint().height())
QtCore.QTimer.singleShot(0, wrapper)

@QtCore.pyqtSlot(int, int)
def resizeApp(self, width, height):
self.resize(width, height)


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())

关于python - 隐藏小部件后调用 size() 时应用程序大小不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56161081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com