【问题标题】:How to install new QStyle for PyQt?如何为 PyQt 安装新的 QStyle?
【发布时间】:2016-11-04 09:56:28
【问题描述】:

我正在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 显示器或有视力问题的人吗?

如果是这样,如何下载并安装此样式?

非常感谢。

【问题讨论】:

  • 你看过Qt的High DPI documentation吗?
  • 谢谢@TheCompiler。我如何在 python 中实际实现这一点?
  • 我自己没有 HighDPI 屏幕,但类似 ​​app.setAttribute(Qt.AA_EnableHighDpiScaling, True)(假设 app 是您的 QApplication 实例)可能已经有所帮助。
  • 也许题外话:使用 Fusion 样式 QGroupBox 的默认显示是错误的。请改用 WindowsVista 样式。

标签: python python-3.x pyqt pyqt4 pyqt5


【解决方案1】:

我通过另一个问题得到了答案(或者说是一种解决方法): How to make Icon in QMenu larger (PyQt)?

创建一个新的QStyle 最直接的方法是从现有的QStyle 派生它。 PyQt 为此提供了QProxyStyle 类。下面是我在How to make Icon in QMenu larger (PyQt)? 问题中给出的示例。在此示例中,创建了一个自定义 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_())

只需复制粘贴代码 sn-p,然后将其粘贴到 *.py 文件中。当然,您应该将图标的路径替换为本地计算机上的有效路径。只需提供完整的路径(“C:..”)即可 100% 确定 Qt 找到图标绘图。

试一试,你应该会得到以下窗口:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多