Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize or scale a QIcon?

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?

like image 499
Enrico Tuvera Jr Avatar asked Oct 18 '25 05:10

Enrico Tuvera Jr


1 Answers

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_())
like image 102
eyllanesc Avatar answered Oct 20 '25 18:10

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!