4

I'm attempting to scale up a QIcon, but its not working.

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

When I run the application it doesn't seem to work. I've tried loading the icon both with QPixmap and directly with QIcon, but it's the same tiny size regardless.

What am I doing wrong here?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Enrico Tuvera Jr
  • 2,739
  • 6
  • 35
  • 51

1 Answers1

6

You have to change the iconSize property of QToolBar:

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Ah, so no need to do it through QPixmap or QIcon? – Enrico Tuvera Jr Apr 25 '19 at 06:17
  • @EnricoTuveraJr No, QToolBar uses the iconSize to do the painting, QIcon does not have size for it, even though the QPixmap was huge, the QToolBar will only be reduced using the iconSize – eyllanesc Apr 25 '19 at 06:20
  • 1
    BONUS (for posterity) : turns out using QPixmap to load the svg and then scaling it up with `self.toolbar.setIconSize()` will cause the end result to be pixelated. passing it in directly as `QIcon('./icons/something.svg')` will scale it properly. – Enrico Tuvera Jr Apr 25 '19 at 06:22