N
N
nvlveu2021-04-11 10:05:02
PyQt
nvlveu, 2021-04-11 10:05:02

How can I add a widget to a QBoxLayout so that it doesn't push other widgets away?

Hello. We need to add a widget to the Q(H \ V)BoxLayout so that it doesn't push other widgets away. For example, on a QVBoxLayout we have two widgets that completely occupy a layer:
60729e6fdfc62566896946.png

On top of them, on the same layer, we need to add a QWidget (it is a red translucent widget in the photo) with stretching to the entire QVBoxLayout, but so that the previous widgets do not change either in size or in location:
60729ebb568d4461455727.png

But here's how it turns out for me:
60729ef06a6d5676175483.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-04-11
@sanya84

Something like this.

import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QApplication


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):
        self.setGeometry(300, 200, 600, 400)
        self.setWindowTitle('Пример')
        
        hbox_layout = QVBoxLayout()
        
        red_widget = QWidget()
        red_widget.setStyleSheet("background: green;")
        
        green_widget = QWidget()
        green_widget.setStyleSheet("background: green;")
        
        hbox_layout.addWidget(red_widget)
        hbox_layout.addWidget(green_widget)
        
        self.setLayout(hbox_layout)
        
        transparent_widget = QWidget(self)
        transparent_widget.setGeometry(0, 0, 600, 400)
        transparent_widget.setStyleSheet("background-color: rgba(117, 190, 218, 0.4);")
        

if __name__ == '__main__':
    app = QApplication(sys.argv)
    example = Example()
    example.show()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question