如何使QMenu中的Icon更大(PyQt)?

时间:2016-09-08 17:10:37

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

我还没知道如何让QMenu中的图标更大。我试图定义一个样式表,其中图标大小被放大。但它不起作用。这是我的代码:

menuStyleSheet = ("""
        QMenu {
            font-size: 18px;
            color: black;
            border: 2px solid black;
            left: 20px;
            background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #cccccc, stop: 1 #ffffff);
        }

        QMenu::item {
            padding: 2px 20px 2px 30px;
            border: 1px solid transparent; /* reserve space for selection border */
            spacing: 20px;
            height: 60px;
        }

        QMenu::icon {
            padding-left: 20px;
            width: 50px;        /* <- unfortunately, doesn't work */
            height: 50px;       /* <- unfortunately, doesn't work */
        }
    """)

#####################################################
#               THE PYQT APPLICATION                #
#####################################################
class GMainWindow(QMainWindow):

    def __init__(self, title):
        super(GMainWindow, self).__init__()
            ...

    def setCustomMenuBar(self):
        myMenuBar = self.menuBar()
        global menuStyleSheet
        myMenuBar.setStyleSheet(menuStyleSheet)
        # Now add Menus and QActions to myMenuBar..

此代码的结果如下:

enter image description here

我知道有一个关于类似主题的旧StackOverflow问题,但它假设一个人用C ++编写Qt应用程序。所以情况就不同了。这是链接:How to make Qt icon (in menu bar and tool bar) larger?

非常感谢任何帮助: - )

编辑:

以下是我的机器的一些细节:

  • 操作系统:Windows 10
  • Python:v3(Anaconda包)
  • Qt:PyQt5

1 个答案:

答案 0 :(得分:3)

经过长时间的搜索,我终于找到了解决方案。 只需复制粘贴下面的代码,然后将其粘贴到*.py文件中即可。当然,您应该使用本地计算机上的有效路径替换图标的路径。只需提供一个完整的路径(“C:\ ..”)即可100%确定Qt找到图标图。

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_())

你会看到一个像这样的窗口,带有一个巨大的图标: enter image description here

那我是怎么解决的?好吧,显然你不能以通常的方式增加QMenu项目的图标大小 - 在样式表中定义大小。唯一的方法是提供一个新的QStyle,最好是从现有的QStyle派生。我在C ++中找到了如何做到这一点的资料(参见http://www.qtcentre.org/threads/21187-QMenu-always-displays-icons-aty-16x16-px),但没有解释PyQt 经过长时间的试验和错误,我得到了它的工作: - )

显然有更多人为这种情况而斗争:http://www.qtcentre.org/threads/61860-QMenu-Icon-Scale-in-PyQt

相关问题