python - 使内容和选项卡在pyqt5中可扩展

标签 python pyqt pyqt5 qt-designer qtabwidget

这是我试图在 QT Creator 中解决的问题。我想让选项卡小部件的内容以及选项卡小部件本身可根据屏幕尺寸进行拉伸(stretch)。到目前为止,我一直在 StackOverflow 上寻找解决方案,但没有找到任何解决方案。

一个小例子:

enter image description here

正如其他答案中所建议的,我将所有内容组织在具有这些属性的网格布局中。

enter image description here

事实上,我什至向列表中的所有对象添加了相同的 Expanding 值。但尽管如此,当我以全屏尺寸打开它时,我仍然得到一个固定的选项卡小部件。

enter image description here

有什么建议如何使选项卡小部件填充所有周围的空间吗?提前致谢!

更新1: 也分享我的.ui https://gist.github.com/Ren22/41ca0dc0333a360775aec530d6f38a62

最佳答案

大小策略仅在小部件几何形状由布局处理时才有效,因此解决方案是将 QTabWidget 设置为centralwidget,右键单击 QTabWidget 外部但 QMainWindow 内部的区域,然后单击图标enter image description hereenter image description here Qt Designer 顶部的图标生成以下内容:

<?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>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QTabWidget" name="tabWidget">
      <widget class="QWidget" name="tab">
       <attribute name="title">
        <string>Tab 1</string>
       </attribute>
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <item>
         <widget class="QListWidget" name="listWidget"/>
        </item>
        <item>
         <widget class="QPushButton" name="pushButton">
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QWidget" name="tab_2">
       <attribute name="title">
        <string>Tab 2</string>
       </attribute>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>30</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

关于python - 使内容和选项卡在pyqt5中可扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55185611/

相关文章:

python - Google App Engine 教程中的这段代码发生了什么

python - Python中的并行递归函数

QtGui 标签和框架在不同的机器上有不同的大小

Python在pyqt5中创建一个真正的圆形按钮

python - 如何使用 blit 有效地重绘多个 matplotlib 图

python - 如何在 QTableWidget 中选择多行?

需要python基本解释

python - 自动检测 Rohde and Schwarz 示波器的 IP 地址

python - 用钢笔在 Canvas 上删除

python - 如果大小写与现有项目不同,QComboBox 会替换编辑文本