[img]https:// i.sstatic.net/O7RYNq18.png[/img]

Я не уверен, связано ли это с моими расчетами в коде или с внутренним ограничением способа отрисовки окна?
import functools as _functools
import typing as _t
from Qt import (
QtCore as _QtCore,
QtGui as _QtGui,
QtWidgets as _QtWidgets,
)
class MyWindow(_QtWidgets.QWidget):
_MAX_WIDTH = 600
_CLOSE_BUTTON_SIZE: _t.ClassVar[_t.Tuple[int, int]] = (15, 15)
_CLOSE_BUTTON_TOOLTIP: _t.ClassVar[str] = "Close"
_BORDER_WIDTH = 1.0
def __init__(self):
super().__init__(parent=None)
self._tip_palette = _QtWidgets.QToolTip.palette()
self._setup_ui()
self._setup_connections()
# TODO: Remove after testing.
self._setup_pdb()
self.setAttribute(_QtCore.Qt.WA_TranslucentBackground)
self.setWindowFlag(_QtCore.Qt.FramelessWindowHint)
self.allow_close(True)
self._window_shape_path = self._get_window_shape_path()
def resizeEvent(self, event):
self._window_shape_path = self._get_window_shape_path()
return super().resizeEvent(event)
def set_message(self, text: str) -> None:
self._message_label.setText(text)
def allow_close(self, allow: bool):
self._close_t_button.setHidden(not allow)
def _get_window_shape_path(self) -> _QtGui.QPainterPath:
print(self.size())
shape_size = _QtCore.QSizeF(self.size())
print(shape_size)
origin = _QtCore.QPointF(0.0, 0.0)
bottom_left = _QtCore.QPointF(origin.x(), shape_size.height())
bottom_right = _QtCore.QPointF(
shape_size.width(), bottom_left.y()
)
top_right = _QtCore.QPointF(bottom_right.x(), origin.y())
shape_path = _QtGui.QPainterPath(origin)
shape_path.lineTo(bottom_left)
shape_path.lineTo(bottom_right)
shape_path.lineTo(top_right)
shape_path.lineTo(origin)
return shape_path
def paintEvent(self, event: _QtGui.QPaintEvent) -> None:
painter = _QtGui.QPainter(self)
brush = self._tip_palette.background()
pen = _QtGui.QPen()
pen.setColor(_QtCore.Qt.red)
pen.setWidthF(self._BORDER_WIDTH)
painter.setBrush(brush)
painter.setPen(pen)
painter.drawPath(self._window_shape_path)
def _setup_ui(self):
self._message_label = _QtWidgets.QLabel()
self._message_label.setWordWrap(True)
self._message_label.setForegroundRole(self._tip_palette.ToolTipText)
self._button = _QtWidgets.QPushButton()
self._close_t_button = _QtWidgets.QToolButton()
self._close_t_button.setIcon(
_QtWidgets.QApplication.style().standardIcon(
_QtWidgets.QStyle.SP_TitleBarCloseButton
)
)
self._close_t_button.setToolTip(self._CLOSE_BUTTON_TOOLTIP)
self._close_t_button.setFixedSize(*self._CLOSE_BUTTON_SIZE)
self._main_layout = _QtWidgets.QVBoxLayout()
self._main_layout.addWidget(
self._close_t_button, alignment=_QtCore.Qt.AlignRight
)
self._main_layout.addWidget(self._message_label)
self._main_layout.addWidget(self._button)
self.setLayout(self._main_layout)
self.setMaximumWidth(self._MAX_WIDTH)
self._message_label.setMaximumWidth(
self.maximumWidth() - self._main_layout.contentsMargins().right()*2
)
def _setup_connections(self):
self._button.clicked.connect(_functools.partial(self.close))
def _setup_pdb(self):
self._pdb_shortcut = _QtWidgets.QShortcut(_QtGui.QKeySequence("Ctrl+p"), self)
self._pdb_shortcut.activated.connect(self._launch_pdb)
def _launch_pdb(self):
import pdb
pdb.set_trace()
if __name__ == "__main__":
import sys as _sys
app = _QtWidgets.QApplication([])
widget = MyWindow()
widget.set_message("This is quite a long message that I do not care about!")
widget.allow_close(False)
widget.resize(400, 400)
widget.show()
_sys.exit(app.exec_())
Подробнее здесь: https://stackoverflow.com/questions/792 ... -my-window