我正在用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'自定义样式?
这些样式都没有对HiDpi显示器(4k等)的适当支持。例如,滚动条太小(参见本文:How to resize the scrollbar from a QTextEdit in PyQt?)。我甚至没有提到视力不佳的人的问题。
你知道一种风格(最好是开源的),它能为4k显示器或视力有问题的人提供良好的支持吗?
如果是这样的话,如何下载并安装这种样式?
非常感谢。
发布于 2017-05-02 05:40:57
我通过另一个问题得到了答案(或者说解决办法):How to make Icon in QMenu larger (PyQt)?
创建新QStyle最简单的方法是从现有QStyle中派生出来。PyQt为此提供了QProxyStyle类。下面是我在How to make Icon in QMenu larger (PyQt)?问题中也给出的例子。在本例中,创建了一个自定义QStyle (源自“融合”样式),并且自定义样式为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找到图标绘图。
尝试一下,您应该得到以下窗口:

https://stackoverflow.com/questions/38154702
复制相似问题