Answer the question
In order to leave comments, you need to log in
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:
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:
But here's how it turns out for me:
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question