我正在使用PyQt5實現一個python應用程序,並且在使用QScrollArea時遇到了一些問題。這是我的應用程序的佈局:Qt.ScrollBarAsNeeded在實際需要時不顯示滾動條
它是由2 QScrollArea
(左和右窗格)和QMdiArea
(中心部件)排列成QHBoxLayout
。當我通過單擊控件來展開左窗格上的小部件時,QScrollArea
的QWidget
的高度大於QScrollArea
本身的高度,滾動條顯示(如預期),但它與QScrollArea
的內容重疊。要解決這個問題,我重新實現了resizeEvent
添加必要的空間用於滾動條(直到此時一切正常。
現在,當我手動調整的主窗口,在左側窗格中得到更多的空間和滾動條應該消失(但事實並非如此),它重疊面板的部件:
我也試過手動切換滾動條的可見性(收到resizeEvent時):當我這樣做,我可以成功隱藏t他滾動條,但然後我不能再顯示它(無論我是否在滾動條上調用setVisible(True)
)。這導致滾動條的空間被添加,但滾動條缺失和麪板的內容是不可滾動:
這裏是面板控件的實現:
class Pane(QScrollArea):
MinWidth = 186
def __init__(self, alignment=0, parent=None):
super().__init__(parent)
self.mainWidget = QWidget(self)
self.mainLayout = QVBoxLayout(self.mainWidget)
self.mainLayout.setAlignment(alignment)
self.mainLayout.setContentsMargins(0, 0, 0, 0)
self.mainLayout.setSpacing(0)
self.setContentsMargins(0, 0, 0, 0)
self.setFrameStyle(QFrame.NoFrame)
self.setFixedWidth(Pane.MinWidth)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Ignored)
self.setWidgetResizable(True)
self.setWidget(self.mainWidget)
def resizeEvent(self, resizeEvent):
if self.viewport().height() < self.widget().height():
self.setFixedWidth(Pane.MinWidth + 18)
# THIS DOESN'T WORK
#self.verticalScrollBar().show()
else:
self.setFixedWidth(Pane.MinWidth)
#self.verticalScrollBar().hide()
def addWidget(self, widget):
self.mainLayout.addWidget(widget)
def removeWidget(self, widget):
self.mainLayout.removeWidget(widget)
def update(self, *__args):
for item in itemsInLayout(self.mainLayout):
item.widget().update()
super().update(*__args)
我想實現的非常簡單(但實際上看起來並不那麼簡單):我希望只在需要時纔在左/右窗格小部件上動態顯示垂直滾動條,併爲滾動條添加必要的空間,以便它不會覆蓋QScrollArea
中的小部件。
有人問之前,我已經嘗試做這樣的事情:
def resizeEvent(self, resizeEvent):
if self.viewport().height() < self.widget().height():
self.setFixedWidth(Pane.MinWidth + 18)
scrollbar = self.verticalScrollbar()
scrollbar.setVisible(True)
self.setVerticalScrollBar(scrollbar) ## APP CRASH
else:
self.setFixedWidth(Pane.MinWidth)
#self.verticalScrollBar().hide()
導致我的應用程序崩潰。 我希望有人已經面臨這個問題,並能夠幫助我。
編輯:我使用PyQt5.5編譯Qt5.5下OSX優勝美地10.10.4使用鐺。
首先嚐試設置'setWidgetResizable'真爲你scrollArea –
它已經被設置爲真,所以我想這個問題是其他地方 –