def paintEvent(self, event): QFrame.paintEvent(self, event) text = QDateTime.currentDateTime().toString(Qt.SystemLocaleLongDate) logicalRect = QRectF(QPointF(0, 0), QSizeF(QFontMetrics(self.font()).size(Qt.TextSingleLine, text))) physicalRect, frameWidth = QRectF(self.rect()), self.frameWidth() physicalRect.adjust(frameWidth, frameWidth, -frameWidth, -frameWidth) scaleForWidth = physicalRect.width() / logicalRect.width() scaleForHeight = physicalRect.height() / logicalRect.height() logicalRect.moveTo(frameWidth / scaleForWidth , frameWidth / scaleForHeight) painter = QStylePainter(self) painter.scale(scaleForWidth, scaleForHeight) painter.drawText(logicalRect, Qt.AlignCenter, text)
def paintEvent(self, event): QFrame.paintEvent(self, event) text = QDateTime.currentDateTime().toString(Qt.SystemLocaleLongDate) logicalRect = QRectF( QPointF(0, 0), QSizeF(QFontMetrics(self.font()).size(Qt.TextSingleLine, text))) physicalRect, frameWidth = QRectF(self.rect()), self.frameWidth() physicalRect.adjust(frameWidth, frameWidth, -frameWidth, -frameWidth) scaleForWidth = physicalRect.width() / logicalRect.width() scaleForHeight = physicalRect.height() / logicalRect.height() logicalRect.moveTo(frameWidth / scaleForWidth, frameWidth / scaleForHeight) painter = QStylePainter(self) painter.scale(scaleForWidth, scaleForHeight) painter.drawText(logicalRect, Qt.AlignCenter, text)
class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() # na pozici [0, 0] se šířkou 50px a výškou 50px # musíme ho vykreslit my, protože je to pouze abstraktní objekt (abstrakní ve smyslu, že nám pouze uchovává data a poskytuje potřebné výpočty) self.__drag_and_drop_rect = QRectF(QPointF(), QSizeF(50, 50)) self.__rect_color = QColor("red") self.__mouse_pressed = False def paintEvent(self, _): """ :type _: QPaintEvent """ painter = QPainter(self) painter.setBrush(QBrush(self.__rect_color)) painter.setPen(QPen(self.__rect_color)) painter.drawRect(self.__drag_and_drop_rect) def mousePressEvent(self, mouse_event): """ :type mouse_event: QMouseEvent """ # je v bod kliknutí v obdelníku? self.__mouse_pressed = self.__drag_and_drop_rect.contains(mouse_event.pos()) def mouseReleaseEvent(self, _): """ :type _: QMouseEvent """ self.__mouse_pressed = False def mouseMoveEvent(self, mouse_event): """ :type mouse_event: QMouseEvent """ if not self.__mouse_pressed: return # přesuň obdelník na pozici kurzoru self.__drag_and_drop_rect.moveTo(mouse_event.pos()) # obnov okno(zavolá se metoda paintEvent) self.update()
def paintEvent(self, event): # TODO: Use QPainter.drawPixmapFragments on Qt 4.7 opt = QStyleOption() opt.initFrom(self) pixmap = self.__shadowPixmap shadow_rect = QRectF(opt.rect) widget_rect = QRectF(self.widget().geometry()) widget_rect.moveTo(self.radius_, self.radius_) left = top = right = bottom = self.radius_ pixmap_rect = QRectF(QPointF(0, 0), QSizeF(pixmap.size())) # Shadow casting rectangle in the source pixmap. pixmap_shadow_rect = pixmap_rect.adjusted(left, top, -right, -bottom) source_rects = self.__shadowPixmapFragments(pixmap_rect, pixmap_shadow_rect) target_rects = self.__shadowPixmapFragments(shadow_rect, widget_rect) painter = QPainter(self) for source, target in zip(source_rects, target_rects): painter.drawPixmap(target, pixmap, source) painter.end()