python - 自动将 `stackedWidget` 页面的高度调整为放置在其上的小部件的高度
问题描述
我想调整 的高度,stackedWidget
使其QTextEdit
下方小部件的高度与stackedWidget
. 虽然QPushButton
页面上的 4extended
占据了 的整个空间,但页面上stackedWidget
的 2垂直显示在中间,并且上下仍有可用空间。QPushButton
normal
但是QPushButton
页面上的 2normal
应该在顶部,并且QTextEdit
小部件应该向上放大。如何才能做到这一点?
主文件
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
loadUi("mainwindow.ui", self)
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
主窗口.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>601</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="locale">
<locale language="English" country="UnitedKingdom"/>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QStackedWidget" name="stackedWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="normal" native="true">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="normal_start_button">
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="normal_stop_button">
<property name="text">
<string>Stop</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="extended">
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QPushButton" name="extended_quick_start_button">
<property name="text">
<string>Quick Start</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="extended_stop_button">
<property name="text">
<string>Stop</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="extended_slow_start_button">
<property name="text">
<string>Slow Start</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="extended_pause_button">
<property name="text">
<string>Pause</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item row="1" column="0">
<widget class="QTextEdit" name="output"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
页面截图,上面和下面normal
只有 2 个QPushButton
和未使用的空间:
页面截图extended
4QPushButton
和可用空间的最佳利用:
解决方案
QStackedWidget 将其包含的小部件的默认最大高度作为默认高度,因此在这种情况下,一种可能的解决方案是将高度设置为参考当前小部件的默认高度。
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
loadUi("mainwindow.ui", self)
self.stackedWidget.currentChanged.connect(self.adjustHeight)
self.adjustHeight()
def adjustHeight(self):
h = self.stackedWidget.currentWidget().sizeHint().height()
self.stackedWidget.setFixedHeight(h)
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
def onTimeout():
sw = main_window.stackedWidget
sw.setCurrentIndex((sw.currentIndex() + 1) % sw.count())
# test
from PyQt5.QtCore import QTimer
timer = QTimer(timeout=onTimeout)
timer.start(1000)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
推荐阅读
- python - Button'对象没有属性python回调
- java - 基本 Java 的输出不打印。不知道为什么
- ios - UICollectionViewCell 有 2 个单元格,每个单元格都有“wrap_content”高度
- node.js - 在 Node.js 中使用 Mongoose 从数据库中查找正确数据时遇到问题
- math - F# 用小数精确计算?
- javascript - 使用javascript显示时间表
- drake - 多体树中的循环关节
- javascript - NestJS 和 GraphQL - TypeError:graphql_1.parse 不是具有最少设置的函数
- python-3.x - 需要数据结构来查找句子中两个单词同时出现的概率?
- javascript - 具有相同类的多个 DIV,每个 DIV 在 2 个 DIV 之间独立切换