Python Forum
[PyQt] Dinamically adding widgets to layout [PySide6]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Dinamically adding widgets to layout [PySide6]
#1
Bug 
Hello!

I'm following this thread on SO to dinamically create widgets. I managed to translate this example to PySide6 and it works.

But, for some (probably dumb) reason, it's not working on my code. The program starts with no errors, but the widgets are not showing... Any advice?

import sys
from PySide6 import QtWidgets, QtCore


def new_timer(description: str, hour: int, minutes: int) -> QtWidgets.QWidget:

    def remove_timer(layout: QtWidgets.QLayout, widget: QtWidgets.QWidget) -> None:
        layout.removeWidget(widget)
        widget.deleteLater()

    widget = QtWidgets.QWidget()
    layout = QtWidgets.QGridLayout(widget)

    label = QtWidgets.QLabel(description)
    edit_timer = QtWidgets.QTimeEdit(time=QtCore.QTime(hour, minutes))
    btn_remove = QtWidgets.QPushButton(text="Remove Timer")
    btn_remove.clicked.connect(lambda: remove_timer(layout, widget))

    layout.addWidget(label, 0, 0)
    layout.addWidget(edit_timer, 1, 0)
    layout.addWidget(btn_remove, 2, 0)

    return widget


class TimerWindow(QtWidgets.QMainWindow):
    def __init__(self) -> None:
        super().__init__()

        self.grid = QtWidgets.QGridLayout()
        self.grid.setSpacing(10)
        self.widget = QtWidgets.QWidget()
        self.layout = QtWidgets.QGridLayout(self.widget)

        # Mockup data
        self.timers = [
            new_timer("Alarme 1", 9, 15),
            new_timer("Alarme 2", 4, 15),
            new_timer("Alarme 3", 16, 20)
        ]
        for i, timer in enumerate(self.timers):
            self.layout.addWidget(timer, i, 0)

        self.scroll = QtWidgets.QScrollArea()
        self.scroll.setWidgetResizable(True)
        self.scroll.setWidget(self.widget)
        self.grid.addWidget(self.scroll, 3, 0)
        self.setLayout(self.grid)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    dialog = TimerWindow()
    dialog.show()
    sys.exit(app.exec())
Reply
#2
You are using the same layout widget to position the scrolled area and things inside the scrolled area. You need two layout widgets. One to put the timers inside the widget that gets scrolled, and another to place the scrolled area. widget in the window.

QMainWindow provides a layout widget for you. You get to add 1 widget to this layout.
class TimerWindow(QtWidgets.QMainWindow):
    def __init__(self) -> None:
        super().__init__()
 
        self.grid = QtWidgets.QGridLayout()
        self.grid.setSpacing(10)
        self.widget = QtWidgets.QWidget()
        self.layout = QtWidgets.QGridLayout(self.widget)
 
        # Mockup data
        self.timers = [
            new_timer("Alarme 1", 9, 15),
            new_timer("Alarme 2", 4, 15),
            new_timer("Alarme 3", 16, 20)
        ]
        for i, timer in enumerate(self.timers):
            self.layout.addWidget(timer, i, 0)
 
        self.scroll = QtWidgets.QScrollArea()
        self.scroll.setWidgetResizable(True)
        self.scroll.setWidget(self.widget)
        self.setCentralWidget(self.scroll)   # Add scroll area widget to the main window.
carecavoador likes this post
Reply
#3
Wonderful! That was precisely the solution to the problem. Thank you very much! Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PySide6 - Connect to DBus signal - Correct syntax Drexel 1 657 Dec-18-2023, 08:03 AM
Last Post: Drexel
  PySide6 Copy and Past from clipboard to QTableWedget zinho 6 1,292 Dec-07-2023, 10:10 PM
Last Post: zinho
  PySide6 QFontDialog - bug or just me? PatM 1 1,118 Jan-22-2023, 01:29 PM
Last Post: Axel_Erfurt
  Pyside6 Larz60+ 7 3,003 Nov-28-2022, 07:25 PM
Last Post: Larz60+
  [Tkinter] Frames layout adding widgets klatlap 7 2,062 Jan-29-2022, 10:17 PM
Last Post: klatlap
  [PySide6] Load ui with UiLoader catlessness 6 8,874 Nov-24-2021, 02:17 PM
Last Post: catlessness
  pyqt5 layout Nickd12 8 3,521 Jan-18-2021, 09:09 AM
Last Post: Axel_Erfurt
  [PyQt] remove widgets of a layout catlessness 2 12,054 Apr-02-2020, 07:25 PM
Last Post: catlessness
  Python GUI layout off between different OS shift838 5 3,732 Jan-02-2019, 02:53 AM
Last Post: shift838
  [Tkinter] grid layout neech 8 17,710 Oct-14-2016, 07:06 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020