0

I'm trying to build a custom toolbar for my application. But the toolbar doesn't show when I put it in the QGridLayout of the application.

This is a test code that regenerates the issue.

from sys import argv
from sys import exit as ex
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QGridLayout, QTextEdit, QWidget, QSizePolicy


class Toolbar(QWidget):
    def __init__(
        self,
        parent: QWidget | None = None,
        flags: Qt.WindowType = Qt.WindowType.Widget,
    ) -> None:
        super().__init__(parent, flags)
        self.setMinimumHeight(50)


class MainWindow(QWidget):
    def __init__(
        self, parent: QWidget | None = None, flags: Qt.WindowType = Qt.WindowType.Window
    ) -> None:
        super().__init__(parent, flags)
        self.__setup_ui()

    def __setup_ui(self) -> None:
        self.grid_layout = QGridLayout()
        toolbar = Toolbar()
        self.grid_layout.addWidget(toolbar, 0, 0, 1, 4)
        text_edit = QTextEdit()
        self.grid_layout.addWidget(text_edit, 1, 0, 2, 1)
        text_edit = QTextEdit()
        self.grid_layout.addWidget(text_edit, 1, 1, 1, 1)
        text_edit = QTextEdit()
        self.grid_layout.addWidget(text_edit, 1, 2, 2, 1)
        text_edit = QTextEdit()
        self.grid_layout.addWidget(text_edit, 1, 3, 2, 1)
        text_edit = QTextEdit()
        self.grid_layout.addWidget(text_edit, 2, 1, 1, 1)
        self.setLayout(self.grid_layout)


if __name__ == "__main__":
    application = QApplication(argv)
    main_window = MainWindow()
    main_window.show()
    ex(application.exec())

This is what it looks like when we run it.

With custom QWidget

When I put a QTextEdit instead the toolbar.

toolbar = QTextEdit()

I get this.

With QTextEdit

I saw this.

https://forum.qt.io/topic/97929/custom-qwidget-doesn-t-show-up-in-qgridlayout-only-in-a-new-window/7

So I tried this.

self.grid_layout.addWidget(toolbar.setVisible(True), 0, 0, 1, 4)

I found this.

https://doc.qt.io/qtforpython-6/overviews/layout.html

That says:

Custom Widgets in Layouts When you make your own widget class, you should also communicate its layout properties. If the widget uses one of Qt’s layouts, this is already taken care of. If the widget does not have any child widgets, or uses a manual layout, you can change the behavior of the widget using any or all of the following mechanisms:

  • Reimplement sizeHint() to return the preferred size of the widget.
  • Reimplement minimumSizeHint() to return the smallest size the widget can have.
  • Call setSizePolicy() to specify the space requirements of the widget.

So I tried this.

class Toolbar(QWidget):
    def __init__(
        self,
        parent: QWidget | None = None,
        flags: Qt.WindowType = Qt.WindowType.Widget,
    ) -> None:
        ...
        self.setSizePolicy(
            QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
        )

But I got same result.

Could you help me figure out why this custom QWidget does not show, please.

15
  • I cannot reproduce this with Qt 6.6, and it should also not happen as soon as the widget has a minimum height set, which is what you're doing. Note that your attempt with setVisible(True) is wrong, other than pointless: 1. setVisible() returns None, so using addWidget() is no-op; 2. by default, child widgets added to a parent are automatically shown as soon as the parent is. Setting the size policy is also not intended for your purpose, and should be left as the default (Preferred). Please add relevant debugging details: PyQt, Qt and OS versions. Commented Jun 13 at 21:39
  • This is the output of pip freeze command PyQt6==6.7.0 PyQt6-Qt6==6.7.1 PyQt6-sip==13.6.0 six==1.16.0 I'm also on Python 3.12.2 and Windows 10 Commented Jun 13 at 22:19
  • Try to downgrade to Qt 6.7.0 (note that PyQt and Qt version are rarely the same: the PyQt version refers to the binding, while the Qt one refers to the actual Qt library). Eventually try downgrading to 6.6. Commented Jun 13 at 22:51
  • 1
    Thank you for the help. I updated Python to 3.12.4 and downgraded PyQt6 and PyQt6-Qt6 to version 6.6.0, and the problem seems to not exist with these versions. Please write an answer that I can take as a correct answer to the question for others. It seems that the issue lies with the new version of PyQt6, specifically version 6.7.1. Should I report this issue, and if so, where should I do that? Commented Jun 13 at 23:57
  • 1
    @musicamante Yeah - if it was affecting me, I would want to experiment quite a bit to see exactly what triggers the issue and maybe find a work-around (e.g. perhaps by reimplementing sizeHint). I have no idea what the underlying cause might be though - something compiler-related maybe? If the OP is using pip, it might also be worth testing with PySide6, just in case there's some PyQt6-specific build issues (unlikely, but you never know).
    – ekhumoro
    Commented Jun 14 at 16:17

0