PYQT QSplitter Problem

I use QSplitter and I found out that the minimum widget width in the splitter is 32 pixels (and a height of 23 pixels). Does anyone know how to change this default value. In other words, you cannot drag the splitter so that one of the widgets (suppose there are 2 widgets in the spllitter) in the spllitter is less than 32 pixels wide.

The code:

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):

        self.resize(400,400)

        m = QtGui.QSplitter(self)
        m.resize(200, 100)

        x = QtGui.QPushButton(m)
        x.setGeometry(0, 0, 100, 100)

        y = QtGui.QPushButton(m)
        y.setGeometry(0, 100, 100, 100)


        m.setSizes([20, 180])
        # this will show you that the width of x is 32 (it should be 20!)
        print x.width()
+3
source share
1 answer

Note. I use Python 3.6.2 and PyQt5, although the logic in the example remains the same and can be understood even if you use other versions of Python and PyQt.

See what is said here:

0, . . , , , .

- x.setMinimumWidth() , :

x.setMinimumWidth(1)

, , ,

  • , ,
  • , - "32" "20".

x.setMinimumWidth(0)

: ( , ), 32 , .

,

m.setCollapsible(0, False)
m.setCollapsible(1, False)

, . .

, , - sizeHint() , , ( ButtonWrapper , , ).

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Python 3.6.2 and PyQt5 are used in this example


from PyQt5.QtWidgets import (
  QPushButton,
  QSplitter,
  QWidget,
  QApplication,
)

import sys


class ButtonWrapper(QPushButton):

    def sizeHint(self):

        return self.minimumSize()


class Example(QWidget):

    def __init__(self):

        super().__init__()
        self.initUI()

    def initUI(self):

        self.resize(400, 400)

        m = QSplitter(self)
        m.resize(200, 100)

        x = ButtonWrapper(self)
        x.setGeometry(0, 0, 100, 100)

        y = QPushButton(self)
        y.setGeometry(0, 100, 100, 100)

        m.addWidget(x)
        m.addWidget(y)

        m.setSizes([20, 180])

        #Now it really shows "20" as expected
        print(x.width())

        #minimumWidth() is zero by default for empty QPushButton
        print(x.minimumWidth())

        #Result of our overloaded sizeHint() method
        print(x.sizeHint().width())
        print(x.minimumSizeHint().width())

        self.setWindowTitle('Example')
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

, -, , , , . , -

0

All Articles