如何使 QMenu 中的图标更大(PyQt)? [英] How to make Icon in QMenu larger (PyQt)?

查看:164
本文介绍了如何使 QMenu 中的图标更大(PyQt)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到如何使 QMenu 中的图标变大.我试图定义一个图标大小被放大的样式表.但它不起作用.这是我的代码:

menuStyleSheet = ("""Q菜单{字体大小:18px;颜色:黑色;边框:2px纯黑色;左:20px;背景色:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #cccccc, stop: 1 #ffffff);}QMenu::item {填充:2px 20px 2px 30px;边框:1px 实心透明;/* 为选择边框保留空间 */间距:20px;高度:60px;}QMenu::icon {padding-left: 20px;宽度:50px;/* <- 不幸的是,它不起作用 */高度:50px;/* <- 不幸的是,它不起作用 */}""")################################################### PYQT 应用程序 ###################################################类 GMainWindow(QMainWindow):def __init__(self, title):super(GMainWindow, self).__init__()...def setCustomMenuBar(self):myMenuBar = self.menuBar()全局菜单样式表myMenuBar.setStyleSheet(menuStyleSheet)# 现在将 Menus 和 QActions 添加到 myMenuBar..

这段代码的结果如下:

我知道有一个关于类似主题的旧 StackOverflow 问题,但它假设有人正在用 C++ 编写 Qt 应用程序.所以情况是不同的.这是链接:

那我是怎么解决的呢?好吧,显然您不能以通常的方式增加 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

I did not yet find out how to make the icons in my QMenu larger. I have tried to define a stylesheet in which the icon size is enlarged. But it doesn't work. Here is my code:

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..

The result of this code is as follows:

I know that there is an old StackOverflow question about a similar topic, but it assumes that one is coding the Qt application in C++. So the situation is different. Here is the link: How to make Qt icon (in menu bar and tool bar) larger?

Any help is greatly appreciated :-)

EDIT :

Here are some details about my machine:

  • OS: Windows 10
  • Python: v3 (Anaconda package)
  • Qt: PyQt5

解决方案

After a long search, I finally found the solution. Just copy-paste the code below, and paste it in a *.py file. Of course, you should replace the path to the icon with a valid path on your local computer. Just provide a complete path ("C:\..") to be 100% sure that Qt finds the icon drawing.

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

You will see a window like this, with a huge icon:

So how did I solve it? Well, apparently you cannot increase the icon size of a QMenu item in the usual way - defining the size in the stylesheet. The only way to do it is to provide a new QStyle, preferably derived from an existing QStyle. I found sources on how to do that in C++ (see http://www.qtcentre.org/threads/21187-QMenu-always-displays-icons-aty-16x16-px), but no explanation for PyQt.
After long trials and errors, I got it working :-)

Apparently more people struggle with this situation: http://www.qtcentre.org/threads/61860-QMenu-Icon-Scale-in-PyQt

这篇关于如何使 QMenu 中的图标更大(PyQt)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆