Взяв ответ Николаса за отправную точку, я создал собственную таблицу стилей 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».
Сначала я применил таблицу стилей ко всем инструментам и кнопкам (то есть по умолчанию). Но после этого все кнопки в диалогах потеряли свою ширину по умолчанию. Поэтому я решил применить стиль только к кнопкам внутри вертикального макета, который содержит все задействованные кнопки.
person
Stefan Bohlein
schedule
28.07.2015