[英]PyQT5 QScrollBar within QDockWidget
I have a lot of buttons, lineedits and other widgets in a window.我在一个窗口中有很多按钮、lineedits 和其他小部件。 Because they are too much for one window, I like to wrap them in a QScrollArea.因为它们对于一个窗口来说太多了,所以我喜欢将它们包装在 QScrollArea 中。 All that should be in a QDockWindow.所有这些都应该在 QDockWindow 中。 My problem: the docker window is visible and works, but none of the buttons and no scroll bar is visible.我的问题:泊坞窗窗口可见且有效,但没有任何按钮和滚动条可见。 I'm using python 3.6 / PyQT5 My code is:我正在使用 python 3.6 / PyQT5 我的代码是:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from pprint import pprint
class DockWindow(QMainWindow):
def __init__(self,parent=None):
super(DockWindow, self).__init__(parent)
layout=QHBoxLayout()
self.docker = QDockWidget()
self.dockerWidget = QWidget()
self.scroll = QScrollArea()
self.widget = QWidget()
self.grid = QGridLayout()
for i in range(1,50):
for j in range(1,5):
object = QPushButton("btn" + str(i) + ";" + str(j))
self.grid.addWidget(object,i,j)
self.widget.setLayout(self.grid)
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.widget)
self.scrollLayout = QHBoxLayout(self.scroll)
self.dockerWidget.setLayout(self.scrollLayout)
self.scroll.setGeometry(10, 10, 100, 100)
self.docker.setWidget(self.dockerWidget)
self.setCentralWidget(QTextEdit())
self.addDockWidget(Qt.RightDockWidgetArea,self.docker)
self.setGeometry(600, 100, 1000, 900)
self.setWindowTitle('Scroll Area Demonstration')
self.show()
if __name__ == '__main__':
app=QApplication(sys.argv)
demo=DockWindow()
demo.show()
sys.exit(app.exec_())
I saw PyQt QScrollArea within QScrollArea but this is in PyQt4 and when I tried to convert it to PyQt5, nothing was visible again.我在 QScrollArea 中看到了PyQt QScrollArea,但这是在 PyQt4 中,当我尝试将其转换为 PyQt5 时,再也看不到任何东西了。 I also saw Qscrollbar in PyQt5 nothing is shown but I do have a size in my code for the QScrollArea.我还在PyQt5 中看到了 Qscrollbar,没有显示任何内容,但我的代码中确实有 QScrollArea 的大小。
So how can I make the buttons visible?那么如何使按钮可见?
Yep, that works.是的,这行得通。 Code is now:代码现在是:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from pprint import pprint
class DockWindow(QMainWindow):
def __init__(self,parent=None):
super(DockWindow, self).__init__(parent)
layout=QHBoxLayout()
self.docker = QDockWidget()
self.scroll = QScrollArea()
self.widget = QWidget()
self.grid = QGridLayout()
for i in range(1,50):
for j in range(1,5):
object = QPushButton("btn" + str(i) + ";" + str(j))
self.grid.addWidget(object,i,j)
self.widget.setLayout(self.grid)
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.widget)
self.docker.setWidget(self.scroll)
self.scroll.setGeometry(10, 10, 100, 100)
self.setCentralWidget(QTextEdit())
self.addDockWidget(Qt.RightDockWidgetArea,self.docker)
self.setGeometry(600, 100, 1000, 900)
self.setWindowTitle('Scroll Area Demonstration')
self.show()
if __name__ == '__main__':
app=QApplication(sys.argv)
demo=DockWindow()
demo.show()
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.