pythonpyqtpyqt5qlineeditqmenubar

QLineEdit setCornerWidget width


I'm adding a QLineEdit to the menu bar with setCornerWidget. However, when setting a size with either setFixedWidth or setMinimumWidth, the QLineEdit is clipping through the window:

Clipping QLineEdit

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        menu = self.menuBar()

        search = QLineEdit(self)
        search.setFixedWidth(200)
        menu.setCornerWidget(search)

Solution

  • It seems to be a bug, a workaround is to use a QWidget as a container:

    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
    
            menu = self.menuBar()
    
            search = QLineEdit()
            search.setFixedWidth(200)
    
            container = QWidget()
            layout = QVBoxLayout(container)
            layout.setContentsMargins(0, 0, 0, 0)
            layout.addWidget(search)
    
            menu.setCornerWidget(container)