如何为 QVBoxLayout 上剩余的 space 填充颜色
How to fill color to rest of the space left on QVBoxLayout
我正在尝试用 pyqt 制作侧面板。我可以制作漂亮的按钮,但无法用相同的颜色填充 space 的其余部分。
我的目标是在按钮下方的 space 中添加相同的蓝色,一直到底部。现在我的按钮位于小部件内部的框布局内(只是为了能够设置样式),小部件位于小部件内部的网格单元格内。
我尝试在框布局中再添加一个小部件,但它无法拉伸以填充 space。我可以设置最小高度,但是如果调整 window 的大小,蓝色区域将不会调整大小。
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()
这是我得到的和我想要的:
获得所需内容的最简单方法可能是执行以下操作
将QObject { background: #FF005AA1; }
添加到widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid
的样式sheet中。
将widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid
添加到main_grid
(或使用alignment = QtCore.Qt.AlignJustify
)时删除alignment
参数,
将color_filler
替换为left_side_buttons
底部的可伸缩space(可以通过left_side_buttons.addStretch()
添加可伸缩space ).
截图:
我正在尝试用 pyqt 制作侧面板。我可以制作漂亮的按钮,但无法用相同的颜色填充 space 的其余部分。
我的目标是在按钮下方的 space 中添加相同的蓝色,一直到底部。现在我的按钮位于小部件内部的框布局内(只是为了能够设置样式),小部件位于小部件内部的网格单元格内。
我尝试在框布局中再添加一个小部件,但它无法拉伸以填充 space。我可以设置最小高度,但是如果调整 window 的大小,蓝色区域将不会调整大小。
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()
这是我得到的和我想要的:
获得所需内容的最简单方法可能是执行以下操作
将
QObject { background: #FF005AA1; }
添加到widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid
的样式sheet中。将
widget_that_would_not_be_needed_if_qt_would_not_be_so_stupid
添加到main_grid
(或使用alignment = QtCore.Qt.AlignJustify
)时删除alignment
参数,将
color_filler
替换为left_side_buttons
底部的可伸缩space(可以通过left_side_buttons.addStretch()
添加可伸缩space ).
截图: