QSplitter, hide sub elements one by one

43 views Asked by At

I have a QSplitter that divides my main window in two parts, in the upper part I have a QTabWidget and in the lower I have a QVBoxLayout.

What I want to do is to have only a single movable QSplitter, but now I can hide the entire QVBoxLayout in one shot while I would like to hide it element by element.

Is it possible to do that with QT Creator?

An example: lowering the QSplitter i jump between the two images. I want to see at first Button1, TextLabel, Button2 then Button1, TextLabel, then only Button1 minimum size All gone

In the code I added my programmatic solution

main.py

from PyQt6 import QtWidgets, uic
import sys

class mw(QtWidgets.QMainWindow):
    def __init__(self, *args):
        super(mw, self).__init__(*args)
        uic.loadUi("mainwindow.ui", self)
        self.splitter.splitterMoved.connect(self.splitterMoved)

    def splitterMoved(self, pos: int, index: int):
        delta = self.splitter.height() - pos - self.splitter.handleWidth()
        items = [self.pb1, self.label, self.pb2] #first is last disappearing
        used = items[0].height()
        for f in items[1:]:
            used += f.height() + self.innerLay.spacing()
            f.setVisible(used <= delta) # use < instead of <= to hide before

app = QtWidgets.QApplication(sys.argv)
mw = mw()
mw.show()
app.exec()

mainwindow.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>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout_2">
    <item>
     <widget class="QSplitter" name="splitter">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <property name="handleWidth">
       <number>5</number>
      </property>
      <widget class="QTableView" name="tableView"/>
      <widget class="QWidget" name="verticalLayoutWidget">
       <layout class="QVBoxLayout" name="innerLay">
        <property name="spacing">
         <number>6</number>
        </property>
        <item>
         <spacer name="verticalSpacer">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>40</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
         <widget class="QPushButton" name="pb1">
          <property name="text">
           <string>PushButton1</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QLabel" name="label">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="text">
           <string>TextLabel</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QPushButton" name="pb2">
          <property name="text">
           <string>PushButton2</string>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
0

There are 0 answers