我使用QSplitter,发现拆分器中小部件的最小宽度为32像素(高度为23像素)。有没有人知道如何改变这个预设。换句话说,您不能拖动拆分器,以便其中一个小部件(假设拆分器中有2个小部件)宽度小于32个像素。
守则:
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()发布于 2017-10-18 11:46:29
注意:我使用的是Python3.6.2和PyQt5,尽管示例中的逻辑保持不变,即使使用其他版本的Python和PyQt也可以理解。
看看这里:是怎么说的
如果指定大小为0,则小部件将不可见。保持小部件的大小策略。也就是说,小于各个小部件的最小大小提示的值将被提示的值替换为.。
解决问题的一个选项是用一个小值调用x.setMinimumWidth(),如下所示:
x.setMinimumWidth(1)然而,如果你亲自尝试,你就会发现
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_())我不确定这是否是正确的方法,但我花了很多时间来解决我自己与此相关的问题,到目前为止还没有看到任何令人满意的事情。如果有人知道一个更好的确实在工作&清除解决方案,我会非常感激的。
https://stackoverflow.com/questions/7167929
复制相似问题