3

我正在尝试扩大 QIcon,但它不起作用。

class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        exitIcon = QPixmap('./icons/outline-exit_to_app-24px.svg')
        scaledExitIcon = exitIcon.scaled(QSize(1024, 1024))
        exitActIcon = QIcon(scaledExitIcon)
        exitAct = QAction(exitActIcon, 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.triggered.connect(qApp.quit)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAct)

        self.setWindowTitle('Toolbar')
        self.show()

当我运行应用程序时,它似乎不起作用。我已经尝试使用 QPixmap 和直接使用 QIcon 加载图标,但无论如何它都是相同的小尺寸。

我在这里做错了什么?

4

1 回答 1

6

您必须更改 QToolBar 的iconSize属性:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets 


class Example(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        exitActIcon = QtGui.QIcon("./icons/outline-exit_to_app-24px.svg")
        exitAct = QtWidgets.QAction(exitActIcon, "Exit", self)
        exitAct.setShortcut("Ctrl+Q")
        exitAct.triggered.connect(QtWidgets.qApp.quit)
        self.toolbar = self.addToolBar("Exit")
        self.toolbar.addAction(exitAct)
        self.toolbar.setIconSize(QtCore.QSize(128, 128)) # <---

        self.setWindowTitle("Toolbar")
        self.show()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Example()
    w.show()
    sys.exit(app.exec_())
于 2019-04-25T05:28:49.783 回答