I have a lot of buttons, lineedits and other widgets in a window. Because they are too much for one window, I like to wrap them in a QScrollArea. All that should be in a 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:
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. I also saw Qscrollbar in PyQt5 nothing is shown but I do have a size in my code for the 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_())