Skip to content

Commit

Permalink
Optimize for pyqt5
Browse files Browse the repository at this point in the history
  • Loading branch information
5yutan5 committed Aug 29, 2021
1 parent b716db0 commit 12fe3d2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ app.exec()

```

> ⚠ The quality of image may be low on Qt5(PyQt5, PySide2) due to the use of svg.
You can add the following [attribute](https://doc.qt.io/qt-5/qt.html#ApplicationAttribute-enum) to improve the quality of images.
> ```Python
> app.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps)
> ```
### Light theme
```Python
Expand Down
4 changes: 2 additions & 2 deletions qdarktheme/Qt/qt_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _get_loaded_api() -> Optional[str]:
importing any other Qt-binding is unsafe.
Returns
-------
None, 'PySide6', 'PyQt6', 'PySide2', 'PyQt5'
None, 'PySide6', 'PyQt6', 'PyQt5', 'PySide2'
"""
for api in _API_LIST:
if sys.modules.get(f"{api}.QtCore"):
Expand All @@ -50,7 +50,7 @@ def _get_installed_api() -> Optional[str]:
Returns
-------
None, 'PySide6', 'PyQt6', 'PySide2', 'PyQt5'
None, 'PySide6', 'PyQt6', 'PyQt5', 'PySide2'
"""
# Fix [AttributeError: module 'importlib' has no attribute 'util']
# See https://stackoverflow.com/a/39661116/13452582
Expand Down
5 changes: 5 additions & 0 deletions qdarktheme/examples/sidebar/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import sys

import qdarktheme
from qdarktheme.Qt.qt_compat import QT_API
from qdarktheme.Qt.QtCore import Qt
from qdarktheme.Qt.QtGui import QAction, QActionGroup
from qdarktheme.Qt.QtWidgets import QApplication, QLabel, QMainWindow, QStackedWidget, QToolBar
from qdarktheme.resource_manager import get_icon

app = QApplication(sys.argv)
# Fix the svg icon display becoming low quality in Qt5.
# Exclude PyQt6 because it doesn't have this attribute.
if QT_API != "PyQt6":
app.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps)
main_win = QMainWindow()

# Create action
Expand Down
6 changes: 6 additions & 0 deletions qdarktheme/examples/widget_gallery/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import qdarktheme
from qdarktheme.examples.widget_gallery.mainwindow import WidgetGallery
from qdarktheme.Qt.qt_compat import QT_API
from qdarktheme.Qt.QtCore import Qt
from qdarktheme.Qt.QtWidgets import QApplication

app = QApplication(sys.argv)
# Fix the svg icon display becoming low quality in Qt5.
# Exclude PyQt6 because it doesn't have this attribute.
if QT_API != "PyQt6":
app.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps)
win = WidgetGallery()
win.menuBar().setNativeMenuBar(False)
app.setStyleSheet(qdarktheme.load_stylesheet())
Expand Down
55 changes: 30 additions & 25 deletions qdarktheme/template.qss
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,14 @@ QMenu::icon {
height: 14px;
}
QMenu::right-arrow {
image: url("qdarktheme:svg/${main_theme}/chevron_right_24dp.svg");
margin: 2px;
padding-left: 12px;
height: 9px;
width: 9px;
height: 20px;
width: 20px;
}
QMenu::right-arrow:disabled {
image: url("qdarktheme:svg/${main_theme}/chevron_right_disabled_24dp.svg");
}

/* QScrollBar -------------------------------------------------------------
Expand Down Expand Up @@ -468,23 +472,6 @@ QToolButton:checked {
QToolButton::checked:disabled {
background-color: $button-disabled;
}
QToolButton[popupMode="0"] {
padding-right: 2px;
}
QToolButton[popupMode="1"] {
padding-right: 20px;
}
QToolButton[popupMode="1"]::menu-button {
border: none;
}
QToolButton[popupMode="1"]::menu-button:hover {
border: none;
border-left: 1px solid $highlight;
border-radius: 0;
}
QToolButton[popupMode="2"] {
padding-right: 2px;
}
QToolButton::menu-button {
padding: 2px;
border-radius: 4px;
Expand All @@ -496,10 +483,14 @@ QToolButton::menu-button:hover, QToolButton::menu-button:checked:hover {
border: 1px solid $highlight;
}
QToolButton::menu-indicator {
height: 8px;
width: 8px;
top: 0;
left: -2px;
image: url("qdarktheme:svg/${main_theme}/expand_more_24dp.svg");
height: 18px;
width: 18px;
top: 6px;
left: 3px;
}
QToolButton::menu-indicator:disabled {
image: url("qdarktheme:svg/${main_theme}/expand_more_disabled_24dp.svg")
}
QToolButton::menu-arrow {
height: 8px;
Expand Down Expand Up @@ -532,7 +523,7 @@ QComboBox::down-arrow {
width: 23px;
}
QComboBox::down-arrow:disabled {
image: url("qdarktheme:svg/${main_theme}/expand_more_disabled_24dp.svg")
image: url("qdarktheme:svg/${main_theme}/expand_more_disabled_24dp.svg");
}

QComboBox QAbstractItemView {
Expand Down Expand Up @@ -940,6 +931,20 @@ QHeaderView::section:vertical {
QHeaderView::section::last, QHeaderView::section::only-one {
margin: 0;
}
QHeaderView::down-arrow {
image: url("qdarktheme:svg/${main_theme}/expand_more_24dp.svg");
width: 20px;
}
QHeaderView::down-arrow:disabled {
image: url("qdarktheme:svg/${main_theme}/expand_more_disabled_24dp.svg")
}
QHeaderView::up-arrow {
image: url("qdarktheme:svg/${main_theme}/expand_less_24dp.svg");
width: 20px;
}
QHeaderView::up-arrow:disabled {
image: url("qdarktheme:svg/${main_theme}/expand_less_disabled_24dp.svg")
}

/* QTableView -------------------------------------------------------------

Expand Down Expand Up @@ -1163,7 +1168,7 @@ QPushButton[type="contained"]:checked {
QPushButton[type="contained"]:disabled {
border-color: $contained-button-disabled;
background-color: $contained-button-disabled;
color: $text;
color: $text-disabled;
}

/* === Text button === */
Expand Down

0 comments on commit 12fe3d2

Please sign in to comment.