|
16 | 16 | QKeyEvent, |
17 | 17 | QKeySequence, |
18 | 18 | QMouseEvent, |
| 19 | + QPaintDevice, |
19 | 20 | QPainter, |
20 | 21 | QPaintEvent, |
21 | 22 | QPalette, |
@@ -924,6 +925,13 @@ def _create_action(self, name: str, workspace: Workspace): |
924 | 925 | return action |
925 | 926 |
|
926 | 927 |
|
| 928 | +def _get_width_dip(s: QPaintDevice): |
| 929 | + # get device-indpendent width for things like QPixmap, which report their size in physical pixels |
| 930 | + if ratio := s.devicePixelRatioF(): |
| 931 | + return int(s.width() / ratio) |
| 932 | + return s.width() |
| 933 | + |
| 934 | + |
927 | 935 | class GenerateButton(QPushButton): |
928 | 936 | ctrl_clicked = pyqtSignal() |
929 | 937 |
|
@@ -984,7 +992,7 @@ def paintEvent(self, a0: QPaintEvent | None) -> None: |
984 | 992 | ) |
985 | 993 | rect = self.rect() |
986 | 994 | pixmap = self.icon().pixmap(int(fm.height() * 1.3)) |
987 | | - pixmap_width = int(pixmap.width() / pixmap.devicePixelRatioF()) |
| 995 | + pixmap_width = _get_width_dip(pixmap) |
988 | 996 | is_hover = int(opt.state) & QStyle.StateFlag.State_MouseOver |
989 | 997 | element = QStyle.PrimitiveElement.PE_PanelButtonCommand |
990 | 998 | content_width = fm.width(self._operation) + 5 + pixmap_width |
@@ -1173,14 +1181,13 @@ def _paint_tool_drop_down(widget: QToolButton, text: str | None = None): |
1173 | 1181 | ) |
1174 | 1182 | rect = widget.rect() |
1175 | 1183 | pixmap = widget.icon().pixmap(int(rect.height() * 0.75)) |
1176 | | - pixmap_width = int(pixmap.width() / pixmap.devicePixelRatioF()) |
1177 | 1184 | element = QStyle.PrimitiveElement.PE_Widget |
1178 | 1185 | if int(opt.state) & QStyle.StateFlag.State_MouseOver: |
1179 | 1186 | element = QStyle.PrimitiveElement.PE_PanelButtonCommand |
1180 | 1187 | style.drawPrimitive(element, opt, painter, widget) |
1181 | 1188 | style.drawItemPixmap(painter, rect.adjusted(4, 0, 0, 0), align, pixmap) |
1182 | 1189 | if text: |
1183 | | - text_rect = rect.adjusted(pixmap_width + 4, 0, 0, 0) |
| 1190 | + text_rect = rect.adjusted(_get_width_dip(pixmap) + 4, 0, 0, 0) |
1184 | 1191 | style.drawItemText(painter, text_rect, align, widget.palette(), True, text) |
1185 | 1192 | painter.translate(int(0.5 * rect.width() - 10), 0) |
1186 | 1193 | style.drawPrimitive(QStyle.PrimitiveElement.PE_IndicatorArrowDown, opt, painter) |
0 commit comments