Qiang Zhang
Qiang Zhang

Reputation: 952

how to let the layout showed in the bottom

The window would have two layouts: A, the main layout to show some images; B, the qlabel to show some status. When the window is started, A would be empty. A would show some images after some operations.

My question is: how to let B show in the bottom? The current code is:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys


class TimeLine(QMainWindow):

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

        widget = QWidget()
        self.setCentralWidget(widget)

        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        widget.setLayout(layout)

        statusLabel = QLabel('status bar')
        statusLabel.setStyleSheet("background-color:blue")
        statusLabel.setMaximumHeight(20)

        layout.addWidget(statusLabel)
        layout.setSpacing(0)

app = QApplication(sys.argv)
window = TimeLine()
window.show()
app.exec_()

And the result is:

enter image description here

The blue label shows in the middle, and I want it to be show in the bottom.

Then, I add a stretch. The layout B would show in the bottom, however, the layout A would also be squeezed. The code is:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys


class TimeLine(QMainWindow):

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

        widget = QWidget()
        self.setCentralWidget(widget)

        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        widget.setLayout(layout)

        mainLabel = QLabel()
        mainLabel.setStyleSheet("background-color:yellow")

        statusLabel = QLabel()
        statusLabel.setStyleSheet("background-color:blue")
        statusLabel.setMaximumHeight(20)

        layout.addWidget(mainLabel)
        layout.addStretch()
        layout.addWidget(statusLabel)
        layout.setSpacing(0)

app = QApplication(sys.argv)
window = TimeLine()
window.show()
app.exec_()

And the result is:

enter image description here

The yellow label is the main label, the gray color represents the back ground. I want the yellow label to occupy all the window except from the blue label.

Thus, what I want is: when there is no other label, I hope the blue label show in the bottom, like: enter image description here

where there is another label (yellow label), I hope the yellow label occupy all the space except from the blue label, like: enter image description here

Upvotes: 0

Views: 161

Answers (2)

S. Nick
S. Nick

Reputation: 13661

I saw your task like this:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class TimeLine(QMainWindow):
    def __init__(self):
        super(TimeLine, self).__init__()
        widget = QWidget()
        self.setCentralWidget(widget)

        mainLabel = QLabel("This is a label.", alignment = Qt.AlignCenter)
        mainLabel.setStyleSheet("background-color:yellow")

        layout = QVBoxLayout(widget)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(mainLabel)

        self.statusBar().setStyleSheet("background-color:blue; color: #fff")
        self.statusBar().showMessage('Hello {} !'.format('status bar'))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = TimeLine()
    window.show()
    sys.exit(app.exec_())

enter image description here

Upvotes: 2

Heike
Heike

Reputation: 24420

If I understand your question correctly, you want the blue label to be aligned with the bottom edge of the main window at all times. One way to get what you want to to set the alignment of the label to Qt.AlignBottom when adding it to the layout. You also need to adjust the stretch factors of the two labels to make sure the yellow label takes up all the space above the blue label when it's present, i.e.

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys


class TimeLine(QMainWindow):

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

        widget = QWidget()
        self.setCentralWidget(widget)

        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        widget.setLayout(layout)

        mainLabel = QLabel()
        mainLabel.setStyleSheet("background-color:yellow")

        statusLabel = QLabel('status bar')
        statusLabel.setStyleSheet("background-color:blue")
        statusLabel.setMaximumHeight(20)

        layout.addWidget(mainLabel, stretch = 1)
        layout.addWidget(statusLabel, stretch = 0, alignment = Qt.AlignBottom)
        layout.setSpacing(0)

app = QApplication(sys.argv)
window = TimeLine()
window.show()
app.exec_()

Both labels:

Both labels

Blue label only:

Blue label only

Upvotes: 1

Related Questions