如何为 PyQt 安装新的 QStyle?
How to install new QStyle for PyQt?
我正在 PyQt4
中编写 GUI(并迁移到 PyQt5
)。这就是我启动 GUI 的方式:
if __name__== '__main__':
app = QApplication(sys.argv)
QApplication.setStyle(QStyleFactory.create('Fusion')) # <- Choose the style
myGUI = MyMainWindow("First GUI")
app.exec_()
PyQt4 中的默认样式:
显然,PyQt4
具有以下样式:
'Windows'
'WindowsXP'
'WindowsVista'
'Motif'
'CDE'
'Plastique'
'Cleanlooks'
PyQt5 中的默认样式:
PyQt5
具有以下样式:
'Windows'
'WindowsXP'
'WindowsVista'
'Fusion'
自定义样式?
None 这些样式对 HiDpi 显示器(4k 等)有适当的支持。例如,滚动条太小(参见 post: )。我什至没有提到那些视力不佳的人的问题..
您知道一种风格(最好是开源的)可以很好地支持 4k 显示器或有视力问题的人吗?
如果是这样,如何下载并安装这种风格?
非常感谢。
我通过另一个问题得到了答案(或者说是解决方法):
创建新 QStyle
最直接的方法是从现有的派生它。 PyQt 为此目的提供了 QProxyStyle
class。下面是我在 问题中也给出的示例。在此示例中,创建了自定义 QStyle
(派生自 "Fusion" 样式),自定义样式为 QMenu
.
提供了非常大的图标
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
# Create a custom "QProxyStyle" to enlarge the QMenu icons
#-----------------------------------------------------------
class MyProxyStyle(QProxyStyle):
pass
def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None):
if QStyle_PixelMetric == QStyle.PM_SmallIconSize:
return 40
else:
return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)
# This is the main window class (with a simple QMenu implemented)
# ------------------------------------------------------------------
class TestWindow(QMainWindow):
def __init__(self):
super(TestWindow, self).__init__()
# 1. Set basic geometry and color.
self.setGeometry(100, 100, 400, 400)
self.setWindowTitle('Hello World')
palette = QPalette()
palette.setColor(QPalette.Window, QColor(200, 200, 200))
self.setPalette(palette)
# 2. Create the central frame.
self.centralFrame = QFrame()
self.centralFrame.setFrameShape(QFrame.NoFrame)
self.setCentralWidget(self.centralFrame)
# 3. Create a menu bar.
myMenuBar = self.menuBar()
fileMenu = myMenuBar.addMenu("&File")
testMenuItem = QAction(QIcon("C:\my\path\myFig.png"), "&Test", self)
testMenuItem.setStatusTip("Test for icon size")
testMenuItem.triggered.connect(lambda: print("Menu item has been clicked!"))
fileMenu.addAction(testMenuItem)
# 4. Show the window.
self.show()
# Start your Qt application based on the new style
#---------------------------------------------------
if __name__== '__main__':
app = QApplication(sys.argv)
myStyle = MyProxyStyle('Fusion') # The proxy style should be based on an existing style,
# like 'Windows', 'Motif', 'Plastique', 'Fusion', ...
app.setStyle(myStyle)
myGUI = TestWindow()
sys.exit(app.exec_())
只需复制粘贴代码片段,然后将其粘贴到 *.py 文件中。当然,您应该将图标的路径替换为本地计算机上的有效路径。只需提供完整路径 ("C:..") 即可 100% 确保 Qt 找到图标绘图。
尝试一下,你应该得到以下结果window:
我正在 PyQt4
中编写 GUI(并迁移到 PyQt5
)。这就是我启动 GUI 的方式:
if __name__== '__main__':
app = QApplication(sys.argv)
QApplication.setStyle(QStyleFactory.create('Fusion')) # <- Choose the style
myGUI = MyMainWindow("First GUI")
app.exec_()
PyQt4 中的默认样式:
显然,PyQt4
具有以下样式:
'Windows'
'WindowsXP'
'WindowsVista'
'Motif'
'CDE'
'Plastique'
'Cleanlooks'
PyQt5 中的默认样式:
PyQt5
具有以下样式:
'Windows'
'WindowsXP'
'WindowsVista'
'Fusion'
自定义样式?
None 这些样式对 HiDpi 显示器(4k 等)有适当的支持。例如,滚动条太小(参见 post:
您知道一种风格(最好是开源的)可以很好地支持 4k 显示器或有视力问题的人吗?
如果是这样,如何下载并安装这种风格?
非常感谢。
我通过另一个问题得到了答案(或者说是解决方法):
创建新 QStyle
最直接的方法是从现有的派生它。 PyQt 为此目的提供了 QProxyStyle
class。下面是我在 QStyle
(派生自 "Fusion" 样式),自定义样式为 QMenu
.
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
# Create a custom "QProxyStyle" to enlarge the QMenu icons
#-----------------------------------------------------------
class MyProxyStyle(QProxyStyle):
pass
def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None):
if QStyle_PixelMetric == QStyle.PM_SmallIconSize:
return 40
else:
return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)
# This is the main window class (with a simple QMenu implemented)
# ------------------------------------------------------------------
class TestWindow(QMainWindow):
def __init__(self):
super(TestWindow, self).__init__()
# 1. Set basic geometry and color.
self.setGeometry(100, 100, 400, 400)
self.setWindowTitle('Hello World')
palette = QPalette()
palette.setColor(QPalette.Window, QColor(200, 200, 200))
self.setPalette(palette)
# 2. Create the central frame.
self.centralFrame = QFrame()
self.centralFrame.setFrameShape(QFrame.NoFrame)
self.setCentralWidget(self.centralFrame)
# 3. Create a menu bar.
myMenuBar = self.menuBar()
fileMenu = myMenuBar.addMenu("&File")
testMenuItem = QAction(QIcon("C:\my\path\myFig.png"), "&Test", self)
testMenuItem.setStatusTip("Test for icon size")
testMenuItem.triggered.connect(lambda: print("Menu item has been clicked!"))
fileMenu.addAction(testMenuItem)
# 4. Show the window.
self.show()
# Start your Qt application based on the new style
#---------------------------------------------------
if __name__== '__main__':
app = QApplication(sys.argv)
myStyle = MyProxyStyle('Fusion') # The proxy style should be based on an existing style,
# like 'Windows', 'Motif', 'Plastique', 'Fusion', ...
app.setStyle(myStyle)
myGUI = TestWindow()
sys.exit(app.exec_())
只需复制粘贴代码片段,然后将其粘贴到 *.py 文件中。当然,您应该将图标的路径替换为本地计算机上的有效路径。只需提供完整路径 ("C:..") 即可 100% 确保 Qt 找到图标绘图。
尝试一下,你应该得到以下结果window: