[PyQt] QScrollArea does not work - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] QScrollArea does not work (/thread-39275.html) |
QScrollArea does not work - HeinKurz - Jan-24-2023 Hi! What's wrong with my code? QScrollArea is shown but scrolling doesn't work. import sys import datetime import xml.etree.ElementTree as ET from PyQt5.QtWidgets import * #QApplication, QDialog, QMainWindow, QMessageBox, QPushButton from PyQt5 import QtWidgets from PyQt5.QtCore import * from PyQt5 import QtCore from PyQt5.QtGui import * from PyQt5 import QtGui #from PyQt5 import uic class MainWindow(QMainWindow): def __init__(self): super().__init__() self.resize(900, 600) self.setMinimumSize(900, 600) self.setWindowTitle("Gewicht") self.setWindowIcon(QIcon("Gewicht.png")) # Tab tabWidget = QTabWidget(self) tabWidget.setContextMenuPolicy(Qt.DefaultContextMenu) self.setCentralWidget(tabWidget) # Tab Daten tabDaten = QWidget() # ... # Tab Grafik tabGrafik = QWidget() tabWidget.addTab(tabGrafik, "Grafik") scrollArea = QScrollArea(tabGrafik) scrollArea.setGeometry(10, 100, 870, 430) sp2 = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) sp2.setHorizontalStretch(0) sp2.setVerticalStretch(0) sp2.setHeightForWidth(scrollArea.sizePolicy().hasHeightForWidth()) # ??? scrollArea.setSizePolicy(sp2) scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn) scrollArea.setWidgetResizable(False) # !?!?!?! scrollArea.setAutoFillBackground(False) sawc = QWidget(scrollArea) # scrollAreaWidgetContents sawc.setGeometry(QRect(1, 1, 1200, 405)) sp3 = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) # Preferred !?!?!? sp3.setHorizontalStretch(0) sp3.setVerticalStretch(0) sp3.setHeightForWidth(sawc.sizePolicy().hasHeightForWidth()) # ??? sawc.setSizePolicy(sp3) sawc.setMinimumSize(QSize(1200, 405)) # !?!?!? sawc.setStyleSheet("background-color:aquamarine;") pushButtonNeu = QPushButton(sawc) pushButtonNeu.setText("Neu") pushButtonNeu.setGeometry(QRect(10, 20, 1040, 90)) pushButtonNeu.setStyleSheet("background-color:gray;") # Tab Infos tabInfos = QWidget() tabWidget.addTab(tabInfos, "Infos, Statistik") def tabChange(seltab): if seltab == 1: # ... if seltab == 2: # ... # Actions tabWidget.currentChanged.connect(tabChange) app = QApplication(sys.argv) window = MainWindow() window.show() app.exec() RE: QScrollArea does not work - Axel_Erfurt - Jan-24-2023 There is nothing to scroll and your imports are a disaster. Don't use import *. Don't use setGeometry for widgets. Use a layout. RE: QScrollArea does not work - deanhystad - Jan-24-2023 You added a widget to the scroll widget window, but not in the correct way. Use scroll_area.setWidget(scrolled_widget). You should also use layout managers instead of setGeometry. Run the attached program and see how easily it adapts to changes in window size. This would be very difficult if you placed and resized all the widgets yourself, but it happens automatically when you use a layout manager. import sys from PySide6.QtWidgets import ( QApplication, QWidget, QScrollArea, QLabel, QVBoxLayout, QPushButton, ) text = ( "The scroll bars appearance depends on the currently set scroll bar policies. " "You can control the appearance of the scroll bars using the inherited functionality " "from QAbstractScrollArea.\n" "\n" "For example, you can set the horizontalScrollBarPolicy and verticalScrollBarPolicyproperties. " "Or if you want the scroll bars to adjust dynamically when the contents of the scroll area " "changes, you can use the horizontalScrollBar() and verticalScrollBar() functions " "(which enable you to access the scroll bars) and set the scroll bars’ values whenever the " "scroll area’s contents change, using the setValue() function.\n" "\n" "You can retrieve the child widget using the widget() function. The view can be made to be " "resizable with the setWidgetResizable() function. The alignment of the widget can be " "specified with setAlignment().\n" ) class MainWindow(QWidget): def __init__(self): super().__init__() self.pane = QWidget() self.view = QScrollArea() self.view.setWidget(self.pane) self.view.setWidgetResizable(True) layout = QVBoxLayout(self) layout.addWidget(self.view) self.text = QLabel(text) self.text.setWordWrap(True) self.text.setMinimumWidth(300) self.button = QPushButton(text="Push Me") layout = QVBoxLayout(self.pane) layout.addWidget(self.text) layout.addWidget(self.button) app = QApplication(sys.argv) window = MainWindow() window.show() app.exec() RE: QScrollArea does not work - HeinKurz - Jan-24-2023 Thanks deanhystad, that's great. |