Skip to content

Set macOS icon when using Qt backend #21784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import functools
import operator
import os
import signal
import sys
import traceback

Expand Down Expand Up @@ -121,6 +120,10 @@ def _create_qApp():
except AttributeError: # Only for Qt>=5.14.
pass
qApp = QtWidgets.QApplication(["matplotlib"])
if sys.platform == "darwin":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a harm in setting this regardless of platform? I just tested on a Linux box and it didn't seem to have an impact.

Ditto to the block below, where even if we are on mac, I don't think it hurts anything to call setWindowIcon on the windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a problem removing this one.

With the other one, theoretically window icons on macOS are supposed to only be set along with a file path when the window represents a file. However, if you don't set the file path, the icon won't show. So while it's an unnecessary call that's semantically incorrect, it's effectively a noop.

So I guess it's really up to your priorities: semantics? Performance? Less code? Up to you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the documentation from Qt re: QWidget::setWindowIcon():

https://doc.qt.io/qt-5/qwidget.html#windowIcon-prop

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to keep Darwin for now, and if someone wants this on another system they can add it?

image = str(cbook._get_data_path('images/matplotlib.svg'))
icon = QtGui.QIcon(image)
qApp.setWindowIcon(icon)
qApp.lastWindowClosed.connect(qApp.quit)
cbook._setup_new_guiapp()
else:
Expand Down Expand Up @@ -519,8 +522,10 @@ def __init__(self, canvas, num):
self.window.closing.connect(canvas.close_event)
self.window.closing.connect(self._widgetclosed)

image = str(cbook._get_data_path('images/matplotlib.svg'))
self.window.setWindowIcon(QtGui.QIcon(image))
if sys.platform != "darwin":
image = str(cbook._get_data_path('images/matplotlib.svg'))
icon = QtGui.QIcon(image)
self.window.setWindowIcon(icon)

self.window._destroying = False

Expand Down