无法动态重置 QSplitter 中小部件的最大高度 [英] Unable to dynamically reset the maximum height of widgets in a QSplitter
本文介绍了无法动态重置 QSplitter 中小部件的最大高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个 QSplitterHandle
调整仅上面的小部件的大小,通过:
I'd like to have a QSplitterHandle
resize only the widget above it, by:
- 首先固定
QSplitter
中所有小部件(实际上是QFrame
)的最大和最小高度. - 在
QSplitterHandle::mousePressEvent
上增加"最大高度,在QSplitterHandle::mouseReleaseEvent
上将最大高度重置为当前.
- Initially fixing the maximum and minimum height of all the widgets (
QFrame
's actually) in theQSplitter
. - On
QSplitterHandle::mousePressEvent
"increase the" the maximum height, and onQSplitterHandle::mouseReleaseEvent
reset the maximum height to the current.
代码
#! /usr/bin/python
import sys
from PySide import QtGui
from PySide import QtCore
# Custom Splitter Handle
class SplitterHandle(QtGui.QSplitterHandle):
QWIDGET_MAX_HEIGHT = 1000
def __init__(self, index , orientation, parent):
super(SplitterHandle , self).__init__(orientation, parent)
self.index = index
def getWidget( self ):
return self.splitter().widget( self.index )
# Remove height restriction on corresponding widget when dragging starts
def mousePressEvent(self ,event ):
widget = self.getWidget()
widget.setMinimumHeight( 100 ) # FIX
widget.setMaximumHeight( self.QWIDGET_MAX_HEIGHT )
return QtGui.QSplitterHandle.mousePressEvent(self,event)
# Freeze height of corresponding widget after dragging has ends
def mouseReleaseEvent(self ,event ):
widget = self.getWidget()
widget.setMinimumHeight( widget.height() ) # FIX
widget.setMaximumHeight( widget.height() )
return QtGui.QSplitterHandle.mousePressEvent(self,event)
# Custom Splitter
class Splitter(QtGui.QSplitter):
def __init__( self , orientation , parent = None):
super(Splitter , self).__init__(orientation,parent)
self.orientation = orientation
def createHandle( self):
return SplitterHandle( self.count() , self.orientation , self )
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
hbox = QtGui.QVBoxLayout(self)
frame1 = QtGui.QFrame()
frame1.setFrameShape(QtGui.QFrame.StyledPanel)
frame1.setMinimumHeight( 100 )
frame1.setMaximumHeight( 100 )
frame1.setStyleSheet("background-color: red;");
frame2 = QtGui.QFrame()
frame2.setFrameShape(QtGui.QFrame.StyledPanel)
frame2.setMinimumHeight( 100 )
frame2.setMaximumHeight( 100 )
frame2.setStyleSheet("background-color: green;");
frame3 = QtGui.QFrame()
frame3.setFrameShape(QtGui.QFrame.StyledPanel)
frame3.setMinimumHeight( 100 )
frame3.setMaximumHeight( 100 )
frame3.setStyleSheet("background-color: blue;");
frame4 = QtGui.QFrame()
frame4.setFrameShape(QtGui.QFrame.StyledPanel)
frame4.setMinimumHeight( 100 )
frame4.setMaximumHeight( 100 )
frame4.setStyleSheet("background-color: yellow;");
# The "absorber"
frame5 = QtGui.QFrame()
frame5.setStyleSheet("background-color: white;");
########################
splitter = Splitter(QtCore.Qt.Vertical)
splitter.addWidget(frame1)
splitter.addWidget(frame2)
splitter.addWidget(frame3)
splitter.addWidget(frame4)
splitter.addWidget(frame5)
splitter.setChildrenCollapsible( False )
hbox.addWidget(splitter)
self.setGeometry(300, 300, 300, 600)
self.setWindowTitle('--Splitter--')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
查看应用截图.问题是动态重置小部件高度的尝试失败了.
See application screenshot. The problem is that the attempts to dynamically reset the widget heights fail.
推荐答案
问题在于,由于传递了不适当的索引,您将最大大小应用于不正确的小部件.解决办法是传它self.count()-1
:
The problem is that you are applying the maximum size to an incorrect widget caused because you are passing an inappropriate index. The solution is to pass it self.count()-1
:
def createHandle( self):
return SplitterHandle(self.count()-1, self.orientation , self)
这篇关于无法动态重置 QSplitter 中小部件的最大高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文