【发布时间】:2021-08-03 12:58:18
【问题描述】:
我正在尝试使用特定于背景的设计构建一些 UI,我想知道您如何将 QLinearGradient 始终保持在中间? 如果您只设置像 QLinearGradient(0, 0, 700, 0) 这样的数字,即使调整窗口大小,它也会保留在同一个位置,这是不希望的......
这里是测试代码:
from PySide2.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, QApplication
from PySide2.QtGui import QLinearGradient, QPalette, QColor, QBrush
from PySide2.QtCore import Qt
class MainWindow(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent=parent)
self.setWindowFlags(Qt.Window)
self.setObjectName('Test')
self.setWindowTitle('Test')
self.mainLayout = QVBoxLayout()
self.find_line = QLineEdit()
self.replace_line = QLineEdit()
self.setLayout(self.mainLayout)
self.rename_button = QPushButton('test button')
self.h_layout_a = QHBoxLayout()
law_grad_col = self.lawrencium_gradiant_color()
self.setting_ui(set_colour=law_grad_col)
self.create()
def setting_ui(self, set_colour=None, **kwargs):
self.setPalette(set_colour)
self.resize(300, 50)
def lawrencium_gradiant_color(self, **kwargs):
p = QPalette()
gradient = QLinearGradient(0, 0, 700, 0)
gradient.setColorAt(0.0, QColor(15, 12, 41))
gradient.setColorAt(0.5, QColor(48, 43, 99))
gradient.setColorAt(1.0, QColor(36, 36, 62))
p.setBrush(QPalette.Window, QBrush(gradient))
return p
def create(self, **kwargs):
find = 'Find'
replace = 'Replace'
self.find_line.setPlaceholderText(find)
self.replace_line.setPlaceholderText(replace)
self.mainLayout.addLayout(self.h_layout_a)
self.h_layout_a.addWidget(self.find_line)
self.h_layout_a.addWidget(self.replace_line)
self.h_layout_a.addWidget(self.rename_button)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
【问题讨论】: