以 Nicolas 的回答为出发点,我创建了自己的 CSS 样式表:
QPushButton,QToolButton {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fdfbf7, stop: 1 #cfccc7);
border-width: 1px;
border-color: #8f8f91;
border-style: solid;
border-radius: 3px;
padding: 4px;
padding-left: 5px;
padding-right: 5px;
}
QToolButton[popupMode="1"] {
padding-right: 18px;
}
QToolButton::menu-button {
border-width: 1px;
border-color: #8f8f91;
border-style: solid;
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
/* 16px width + 2 * 1px for border = 18px allocated above */
width: 16px;
}
QPushButton:hover,QToolButton:hover {
border-top-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #b2afaa, stop: 0.5 #4847a1, stop: 1 #7e7cb6);
border-radius: 1px;
border-top-width: 3px;
border-bottom-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #7e7cb6, stop: 0.5 #4847a1, stop: 1 #b2afaa);
border-bottom-width: 3px;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e4e0e1, stop: 1 #cfcbcd);
}
QPushButton:pressed,QToolButton:pressed {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #cfcbcd, stop: 1 #e4e0e1);
border-width: 1px;
border-color: #8f8f91;
}
QPushButton:focus,QToolButton:focus {
outline: none;
}
为了应用这种风格,我使用了以下 PyQT 代码片段:
cssFile = os.path.join(self.installDir, "etc", "Buttons.css")
with open(cssFile, "r") as fh:
cssStyleSheet = "".join(fh.readlines())
for i in range(self.verticalLayout.count()):
widget = self.verticalLayout.itemAt(i).widget()
if isinstance(widget, QtGui.QPushButton) or isinstance(widget, QtGui.QToolButton):
widget.setStyleSheet(cssStyleSheet)
# Make Tool- and PushButton same height...
if isinstance(widget, QtGui.QToolButton):
widget.setMaximumHeight(self.firstPushButton.sizeHint().height())
如果应用程序背景颜色设置为“#ede9e3”,则生成的外观几乎与默认的“plastique”样式相同。
起初,我将样式表应用于所有工具按钮和按钮按钮(即系统默认设置)。但在此之后,对话框中的所有按钮都失去了默认宽度。所以我决定只将样式应用于垂直布局内的按钮,其中包含所有涉及的按钮。