如何将颜色填充到QVBoxLayout上剩余的剩余空间

时间:2019-08-13 11:51:31

标签: python pyside2

我正在尝试使用pyqt制作侧面板。我能够制作漂亮的按钮,但无法用相同的颜色填充其余空间。

我的目标是向按钮下方的空间添加相同的蓝色,一直到底部。现在,我的按钮位于小部件内部的框布局内(只是为了设置样式),该布局位于小部件内部的网格单元内。

我尝试将另一个小部件添加到框布局中,但是它并没有延伸以填充空间。我可以设置最小高度,但是如果调整窗口大小,则蓝色区域不会调整大小。

import sys
import PySide2.QtWidgets as qt 
from PySide2 import QtCore, QtGui
import os

class main_page:
    def create(self):

        # Create main window
        main_window = qt.QMainWindow()

        # Set some parameters for main window
        main_window.setGeometry(50,50,700,400)
        main_window.setWindowTitle("Here will be name of the program")

        # Create central widget that will contain layout to contain widgets. Eh...
        central_widget = qt.QWidget()
        central_widget.setStyleSheet("background: white;")

        # Add central widget to main window
        main_window.setCentralWidget(central_widget)

        # Create main grid layout that will organize everything
        main_grid = qt.QGridLayout(central_widget)

        main_grid.setSpacing(0)
        main_grid.setMargin(0)

        # Create widget that will contain layout and now we can set style for the widget.

        widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid = qt.QWidget()

        widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid.setStyleSheet(
            '''
            QPushButton {
                background: #FF005AA1;
                color: white;
                text-align: center;
                border: none;
                font-weight: 500;
                font-size: 15px;
                height: 48px;
                width: 120px;
            }
            QPushButton:hover {
               background-color: #FF014a82;
            }
            QPushButton:checked {
            background: #FF01508c;
            }
            QPushButton:pressed {
                color: #FF005AA1;
                background: white;
            }
            ''')

        # On left side we will have some (or at least one) button
        left_side_buttons = qt.QVBoxLayout(widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid)

        left_side_buttons.setSpacing(0)
        left_side_buttons.setMargin(0)

        # And add it to main box the left most cell
        main_grid.addWidget(widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid, 0, 0, alignment = QtCore.Qt.AlignTop)

        main_grid.setRowStretch(0,1)

        # Column 0 must not stretch
        main_grid.setColumnStretch(0,0)

        # Column 1 must stretch
        main_grid.setColumnStretch(1,1)

        # Add widgets to container
        left_side_buttons.addWidget(qt.QPushButton("First button"))
        left_side_buttons.addWidget(qt.QPushButton("Second button"))

        # Add separator line
        line = qt.QFrame()
        line.setFrameShape(qt.QFrame.HLine)
        line.setFrameShadow(qt.QFrame.Sunken)

        left_side_buttons.addWidget(line)

        # Add color for rest of the space
        color_filler = qt.QWidget()

        # I can use this to hack to make colored area bigger but it won't stretch
        color_filler.setMinimumHeight(100)

        left_side_buttons.addWidget(color_filler)

        main_grid.addWidget(qt.QListView(), 0, 1)

        main_window.show()

        # Add list view to right side
        main_grid.addWidget(qt.QListView(), 0, 1)

        main_window.show()

        return main_window


def main():

    app = qt.QApplication(sys.argv)
    my_main_page = main_page().create()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

这就是我得到的以及我想要的: Imgur

1 个答案:

答案 0 :(得分:1)

获得所需的最简单的方法可能是执行以下操作

  1. QObject { background: #FF005AA1; }添加到widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid的样式表中。

  2. 在将alignment添加到widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid(或使用main_grid)时,删除alignment = QtCore.Qt.AlignJustify参数,

  3. color_filler底部的可伸缩空间替换left_side_buttons(可通过left_side_buttons.addStretch()添加可伸缩空间)。

屏幕截图:

Screenshot