2016-03-11 85 views
0

我需要创建一个包含许多按钮的工具栏(例如左侧)。默认情况下,如果所有按钮的总高度大于工具栏的高度,则这些剩余按钮将被隐藏。我想让这个工具栏显示所有按钮,并让我向下滚动查看其余部分。到目前为止,我在网上找不到任何有用的东西。有任何想法吗?带滚动条的PyQt QToolBar

回答

-1

任何有兴趣在这里是解决方案:

由于@Brendan亚伯的回答我一个想法走了过来。我所做的是我创建了我的工具栏,就像以前一样。然后我将所有我的小部件(之前在此工具栏中)添加到带有QVBoxLayout的新QWidget中。然后,我创建了一个QScrollArea,并将我最​​近创建的小部件设置为此滚动区域的子部件。最后,我使用addWidget()将我的ScrollArea添加到工具栏。

class LeftToolbar(QtGui.QToolBar): 
    def __init__(self, *args): 
     QToolBar.__init__(self, *args) 
     self.setFloatable(False) 
     self.setMovable(False) 

     self.scroll_widget = QtGui.QWidget(self) 
     self.scroll_layout = QtGui.QVBoxLayout() 
     self.scroll_widget.setLayout(self.scroll_layout) 

     # Add your toolbar widgets here 
     self.ExampleWidget1 = QtGui.QLabel(self) 
     self.ExampleWidget1.setText("Example Text1") 
     self.scroll_layout.addWidget(self.ExampleWidget1) 

     self.ExampleWidget2 = QtGui.QLabel(self) 
     self.ExampleWidget2.setText("Example Text2") 
     self.scroll_layout.addWidget(self.ExampleWidget2) 

     # Create QScrollArea 
     self.scroll_area = QtGui.QScrollArea() 
     self.scroll_area.setWidget(self.scroll_widget) 
     self.addWidget(self.scroll_area) 

# Create object LeftToolbar in your main window 
self.LeftToolbar = LeftToolbar() 
self.addToolBar(Qt.LeftToolBarArea, self.LeftToolbar) 
1

您应该可以将QToolBar贴在QScrollArea的内部。

toolbar = QtGui.QToolBar() 
toolbar.setOrientation(QtCore.Qt.Vertical) 
for i in range(20): 
    toolbar.addAction('Action{0}'.format(i)) 
scroll_area = QtGui.QScrollArea() 
scroll_area.setWidget(toolbar)