python - 自动将 `stackedWidget` 页面的高度调整为放置在其上的小部件的高度

标签 python pyqt qt-designer

我想调整 stackedWidget 的高度这样QTextEdit的高度它下面的小部件与 stackedWidget 的事件页面上的按钮高度相匹配。 。而4 QPushButton在页上extended占据stackedWidget的整个空间, 2 QPushButton在页上normal垂直出现在中间,并且上方和下方仍然有自由空间。

但是 2 QPushButton在页上normal应该在顶部,QTextEdit小部件应该向上放大。如何做到这一点?

<小时/>

main.py
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以及上方和下方未使用的空间:

Screenshot of page <code>normal</code>

<小时/>

页面截图extended与 4 QPushButton以及可用空间的最佳利用:

Screenshot of page <code>extended</code>

最佳答案

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 - 自动将 `stackedWidget` 页面的高度调整为放置在其上的小部件的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59922115/

相关文章:

python - 如何在 Python 中用 0 替换文本表中的空白条目?

python - 在 pandas 数据帧上运行套索和岭回归

python - qt设计器如何创建彩色面板?

c++ - 如何将焦点返回到窗口?

qt - 如何找出自定义小部件未显示在 Qt Designer 中的原因?

python - python中的分组列表

python - Django - 将 form.errors 作为字典而不是 HTML 代码获取

python - 寻找 PyQt4 嵌入式终端小部件

python - 如何更改 QMainWindow 中央小部件的背景图像?

python - PyQt 代码拆分——设计与功能