|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +__author__ = "ipetrash" |
| 5 | + |
| 6 | + |
| 7 | +import sys |
| 8 | +import traceback |
| 9 | + |
| 10 | +from PyQt6.QtWidgets import ( |
| 11 | + QApplication, |
| 12 | + QMessageBox, |
| 13 | + QGridLayout, |
| 14 | + QPushButton, |
| 15 | + QStyle, |
| 16 | + QWidget, |
| 17 | + QToolTip, |
| 18 | +) |
| 19 | +from PyQt6.QtGui import QCursor |
| 20 | +from PyQt6.QtCore import QRect |
| 21 | + |
| 22 | + |
| 23 | +# Для отлова всех исключений, которые в слотах Qt могут "затеряться" и привести к тихому падению |
| 24 | +def log_uncaught_exceptions(ex_cls, ex, tb) -> None: |
| 25 | + text = f"{ex_cls.__name__}: {ex}:\n" |
| 26 | + text += "".join(traceback.format_tb(tb)) |
| 27 | + |
| 28 | + print("Error: ", text) |
| 29 | + QMessageBox.critical(None, "Error", text) |
| 30 | + sys.exit(1) |
| 31 | + |
| 32 | + |
| 33 | +sys.excepthook = log_uncaught_exceptions |
| 34 | + |
| 35 | + |
| 36 | +if __name__ == "__main__": |
| 37 | + app = QApplication([]) |
| 38 | + style = app.style() |
| 39 | + |
| 40 | + source = QStyle.StandardPixmap |
| 41 | + |
| 42 | + enum_items_standard_pixmap: list[str] = [x for x in dir(source) if x.startswith("SP_")] |
| 43 | + |
| 44 | + mw = QWidget() |
| 45 | + mw.setWindowTitle("show_all_standard_qt_icon__from_StandardPixmap__PyQt6") |
| 46 | + |
| 47 | + layout = QGridLayout() |
| 48 | + |
| 49 | + max_column: int = 4 |
| 50 | + for i, enum_name in enumerate(enum_items_standard_pixmap): |
| 51 | + enum_value = getattr(source, enum_name) |
| 52 | + icon = style.standardIcon(enum_value) |
| 53 | + |
| 54 | + button = QPushButton(icon, enum_name) |
| 55 | + button.clicked.connect( |
| 56 | + lambda checked, w=button: ( |
| 57 | + app.clipboard().setText(w.text()), |
| 58 | + QToolTip.showText( |
| 59 | + QCursor.pos(), |
| 60 | + "Saved to clipboard", |
| 61 | + w, |
| 62 | + QRect(), |
| 63 | + 2000, |
| 64 | + ), |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + row = i // max_column |
| 69 | + col = i % max_column |
| 70 | + layout.addWidget(button, row, col) |
| 71 | + |
| 72 | + mw.setLayout(layout) |
| 73 | + mw.show() |
| 74 | + |
| 75 | + app.exec() |
0 commit comments