Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Error. I'm trying to set an icon.

Error. I'm trying to set an icon.

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 280 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Composirevontismo
    wrote on last edited by
    #1

    Hello! I have an issue:
    I'm creating an application and I'm trying to set an icon, but it doesn't show:
    Code:

    ...
    class initializeRemoteControl(QWidget):
        def __init__(self):
            super().__init__()
            self.resize(240, 360)
            self.setMinimumWidth(180)
            self.setMinimumHeight(300)
            self.setMaximumWidth(320)
            self.setMaximumHeight(360)
            self.setWindowTitle("Virtual Control")
            self.setWindowIcon(QIcon("application_internal_resources\remoteControlForApplicationIcon_16PX.png"))
            labelOfRemoteControl1 = QLabel("Remote Control", self)
            labelOfRemoteControl2 = QLabel("Remote Control", self)
            exitBtn = QPushButton(self)
            exitBtn.setText("EXIT")
            exitBtn.setStyleSheet("""font-family: "Inter", "Argentum Sans", "Montserrat", "Nunito Sans", "Frutiger", "Helvetica Neue", "Helvetica", "Arial", system-ui; font-size: 14px; background-color: firebrick; color: white;""")
            remoteControlGrid = QGridLayout()
            self.setLayout(remoteControlGrid)
            remoteControlGrid.addWidget(labelOfRemoteControl1, 0, 0)
            remoteControlGrid.setColumnStretch(0, 12)
            remoteControlGrid.addWidget(exitBtn, 2, 1)
            remoteControlGrid.setColumnStretch(2, 10)
            remoteControlGrid.addWidget(labelOfRemoteControl2, 12, 0)
            remoteControlGrid.setColumnStretch(0, 12)
    ...
    

    Result:
    25d99e33-3239-4176-84cb-7153f0916db3-image.png
    I need a solution, please. I'll thank you for your answer.
    Have a nice night!

    sierdzioS JonBJ 2 Replies Last reply
    0
    • C Composirevontismo

      Hello! I have an issue:
      I'm creating an application and I'm trying to set an icon, but it doesn't show:
      Code:

      ...
      class initializeRemoteControl(QWidget):
          def __init__(self):
              super().__init__()
              self.resize(240, 360)
              self.setMinimumWidth(180)
              self.setMinimumHeight(300)
              self.setMaximumWidth(320)
              self.setMaximumHeight(360)
              self.setWindowTitle("Virtual Control")
              self.setWindowIcon(QIcon("application_internal_resources\remoteControlForApplicationIcon_16PX.png"))
              labelOfRemoteControl1 = QLabel("Remote Control", self)
              labelOfRemoteControl2 = QLabel("Remote Control", self)
              exitBtn = QPushButton(self)
              exitBtn.setText("EXIT")
              exitBtn.setStyleSheet("""font-family: "Inter", "Argentum Sans", "Montserrat", "Nunito Sans", "Frutiger", "Helvetica Neue", "Helvetica", "Arial", system-ui; font-size: 14px; background-color: firebrick; color: white;""")
              remoteControlGrid = QGridLayout()
              self.setLayout(remoteControlGrid)
              remoteControlGrid.addWidget(labelOfRemoteControl1, 0, 0)
              remoteControlGrid.setColumnStretch(0, 12)
              remoteControlGrid.addWidget(exitBtn, 2, 1)
              remoteControlGrid.setColumnStretch(2, 10)
              remoteControlGrid.addWidget(labelOfRemoteControl2, 12, 0)
              remoteControlGrid.setColumnStretch(0, 12)
      ...
      

      Result:
      25d99e33-3239-4176-84cb-7153f0916db3-image.png
      I need a solution, please. I'll thank you for your answer.
      Have a nice night!

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Composirevontismo said in Error. I'm trying to set an icon.:

      "application_internal_resources\remoteControlForApplicationIcon_16PX.png"

      Use forward slash for paths in Qt: "application_internal_resources/remoteControlForApplicationIcon_16PX.png".

      The issue is probably with the path. You have provided a relative path which means Qt will try to open a file at: <working directory>/"application_internal_resources/remoteControlForApplicationIcon_16PX.png". Does your icon exist there? Are you sure what your working dir is or will be?

      In general it's good to anchor the path to icon to the executable (something like: QCoreApplication::applicationDirPath() + "/application_internal_resources/remoteControlForApplicationIcon_16PX.png") or use Qt Resources (QRC).

      (Z(:^

      1 Reply Last reply
      2
      • C Composirevontismo

        Hello! I have an issue:
        I'm creating an application and I'm trying to set an icon, but it doesn't show:
        Code:

        ...
        class initializeRemoteControl(QWidget):
            def __init__(self):
                super().__init__()
                self.resize(240, 360)
                self.setMinimumWidth(180)
                self.setMinimumHeight(300)
                self.setMaximumWidth(320)
                self.setMaximumHeight(360)
                self.setWindowTitle("Virtual Control")
                self.setWindowIcon(QIcon("application_internal_resources\remoteControlForApplicationIcon_16PX.png"))
                labelOfRemoteControl1 = QLabel("Remote Control", self)
                labelOfRemoteControl2 = QLabel("Remote Control", self)
                exitBtn = QPushButton(self)
                exitBtn.setText("EXIT")
                exitBtn.setStyleSheet("""font-family: "Inter", "Argentum Sans", "Montserrat", "Nunito Sans", "Frutiger", "Helvetica Neue", "Helvetica", "Arial", system-ui; font-size: 14px; background-color: firebrick; color: white;""")
                remoteControlGrid = QGridLayout()
                self.setLayout(remoteControlGrid)
                remoteControlGrid.addWidget(labelOfRemoteControl1, 0, 0)
                remoteControlGrid.setColumnStretch(0, 12)
                remoteControlGrid.addWidget(exitBtn, 2, 1)
                remoteControlGrid.setColumnStretch(2, 10)
                remoteControlGrid.addWidget(labelOfRemoteControl2, 12, 0)
                remoteControlGrid.setColumnStretch(0, 12)
        ...
        

        Result:
        25d99e33-3239-4176-84cb-7153f0916db3-image.png
        I need a solution, please. I'll thank you for your answer.
        Have a nice night!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Composirevontismo
        In addition to what @sierdzio has correctly said.

        self.setWindowIcon(QIcon("application_internal_resources\remoteControlForApplicationIcon_16PX.png"))
        

        In Python, just as in C++, a \ inside "..."s is an *escape character". The \r you have in the middle is replaced by the carriage-return character, so the path being passed is not what you think it is anyway. In Python you would need either "...\\r..." or r"...\r..." to get literal 2 characters \r into a string.

        In this case, as @sierdzio says, you should in any case use / instead of \ in your path, and that avoids this issue. And you should make it a full path as he suggests to avoid the dependency on what the current directory is when your script is run.

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved