Error. I'm trying to set an icon.
-
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:
I need a solution, please. I'll thank you for your answer.
Have a nice night! -
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:
I need a solution, please. I'll thank you for your answer.
Have a nice night!@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). -
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:
I need a solution, please. I'll thank you for your answer.
Have a nice night!@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..."
orr"...\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.