gpt4 book ai didi

python - 无法动态重置 QSplitter 中小部件的最大高度

转载 作者:行者123 更新时间:2023-12-01 08:08:37 25 4
gpt4 key购买 nike

我想要一个QSplitterHandle调整其上方小部件的大小,方法是:

  1. 最初修复了 QSplitter 中所有小部件(实际上是 QFrame)的最大和最小高度。
  2. QSplitterHandle::mousePressEvent上“增加”最大高度,在QSplitterHandle::mouseReleaseEvent上将最大高度重置为当前高度。

代码

#! /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()

查看应用程序屏幕截图。问题是动态重置小部件高度的尝试失败。

enter image description here

最佳答案

问题在于您将最大大小应用于由于传递不适当的索引而导致的不正确的小部件。解决方案是传递它 self.count()-1:

def createHandle( self):
return SplitterHandle(self.count()-1, self.orientation , self)

关于python - 无法动态重置 QSplitter 中小部件的最大高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55405273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com