| 29 | |
| 30 | void ElaImageCard::setCardImage(const QImage& cardImage) |
| 31 | { |
| 32 | Q_D(ElaImageCard); |
| 33 | d->_pCardImage = cardImage; |
| 34 | update(); |
| 35 | Q_EMIT pCardImageChanged(); |
| 36 | } |
| 37 | |
| 38 | const QImage& ElaImageCard::getCardImage() const |
| 39 | { |
| 40 | Q_D(const ElaImageCard); |
| 41 | return d->_pCardImage; |
| 42 | } |
| 43 | |
| 44 | void ElaImageCard::paintEvent(QPaintEvent* event) |
| 45 | { |
| 46 | Q_D(ElaImageCard); |
| 47 | if (d->_pCardImage.isNull() || rect().isEmpty()) |
| 48 | { |
| 49 | return; |
| 50 | } |
| 51 | QPainter painter(this); |
| 52 | painter.save(); |
| 53 | painter.setPen(Qt::NoPen); |
| 54 | painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing); |
| 55 | QPainterPath path; |
| 56 | path.addRoundedRect(rect(), d->_pBorderRadius, d->_pBorderRadius); |
| 57 | painter.setClipPath(path); |
| 58 | // 图片绘制 |
| 59 | if (d->_pIsPreserveAspectCrop) |
| 60 | { |
| 61 | qreal windowAspectRatio = static_cast<qreal>(rect().width()) / rect().height(); |
| 62 | qreal pixAspectRatio = static_cast<qreal>(d->_pCardImage.width()) / d->_pCardImage.height(); |
| 63 | int targetPixWidth, targetPixHeight; |
| 64 | if (windowAspectRatio < pixAspectRatio) |
| 65 | { |
| 66 | targetPixWidth = qRound(d->_pCardImage.width() * windowAspectRatio / pixAspectRatio); |
| 67 | targetPixHeight = d->_pCardImage.height(); |
nothing calls this directly
no outgoing calls
no test coverage detected