void GraphicsPixmapHoverItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { if (pixmap().isNull()) { return; } //此处不能使用static来定义tempPix,因为普通界面和全幅界面使用的图片尺寸不一样 QPixmap tempPix(pixmap().size()); tempPix.fill(Qt::transparent); QPainter tempPainter(&tempPix); tempPainter.setRenderHint(QPainter::SmoothPixmapTransform, (transformationMode() == Qt::SmoothTransformation)); if (m_val > 0) { tempPainter.drawPixmap(offset(), m_heroSkinPixmap); double percent = 1 - (double)m_val / (double)m_max; QRectF rect = QRectF(offset().x(), offset().y(), boundingRect().width(), percent * boundingRect().height()); tempPainter.setClipRect(rect); tempPainter.drawPixmap(offset(), pixmap()); tempPainter.setClipRect(boundingRect()); tempPainter.drawPixmap(rect.left() - 9, rect.bottom() - 25, m_skinChangingFrames[m_currentSkinChangingFrameIndex]); //由于可能需要在外部额外处理tempPix,因此必须调用tempPainter.end() //来释放tempPainter对tempPix的控制权,否则会出现异常 tempPainter.end(); if (isSecondaryAvartarItem()) { tempPix = m_playerCardContainer->paintByMask(tempPix); } } else { tempPainter.drawPixmap(offset(), pixmap()); } if (option->state & QStyle::State_Selected) { qt_graphicsItem_highlightSelected(this, &tempPainter, option); } painter->drawPixmap(0, 0, tempPix); }
void GraphicsPixmapHoverItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { if (pixmap().isNull()) { return; } QPixmap tempPix(pixmap().size()); tempPix.fill(Qt::transparent); QPainter tempPainter(&tempPix); tempPainter.setRenderHint(QPainter::SmoothPixmapTransform, (transformationMode() == Qt::SmoothTransformation)); if (m_val > 0) { tempPainter.drawPixmap(offset(), m_heroSkinPixmap); double percent = 1 - (double)m_val / (double)m_max; QRectF rect = QRectF(offset().x(), offset().y(), boundingRect().width(), percent * boundingRect().height()); tempPainter.setClipRect(rect); tempPainter.drawPixmap(offset(), pixmap()); tempPainter.setClipRect(boundingRect()); tempPainter.drawPixmap(rect.left() - 9, rect.bottom() - 25, m_skinChangingFrames[m_currentSkinChangingFrameIndex]); // For tempPix may be processed outside, we should call tempPainter.end() to // release its control of tempPix, or an exception will occur tempPainter.end(); if (!isAvatarOfDashboard() && isSecondaryAvartarItem()) { tempPix = m_playerCardContainer->paintByMask(tempPix); } } else { tempPainter.drawPixmap(offset(), pixmap()); } if (option->state & QStyle::State_Selected) { qt_graphicsItem_highlightSelected(this, &tempPainter, option); } painter->drawPixmap(0, 0, tempPix); }
void MyCustomWidget::paintEvent(QPaintEvent *event){ painter.begin(this); painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); //When the mouse is pressed if(mousePressed){ // we are taking QPixmap reference again and again //on mouse move and drawing a line again and again //hence the painter view has a feeling of dynamic drawing painter.drawPixmap(0,0,mPix); if(selectedTool == 1) painter.drawRect(mRect); else if(selectedTool == 2) painter.drawLine(mLine); else if(selectedTool ==3) painter.drawEllipse(mEllipse); drawStarted = true; } else if (drawStarted){ // It created a QPainter object by taking a reference // to the QPixmap object created earlier, then draws a line // using that object, then sets the earlier painter object // with the newly modified QPixmap object QPainter tempPainter(&mPix); tempPainter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); if(selectedTool == 1) tempPainter.drawRect(mRect); else if(selectedTool == 2) tempPainter.drawLine(mLine); else if(selectedTool == 3) tempPainter.drawEllipse(mEllipse); painter.drawPixmap(0,0,mPix); } painter.end(); }
void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/) { const ServerInfo_User *const info = owner->getUserInfo(); const qreal border = 2; QRectF avatarBoundingRect = boundingRect().adjusted(border, border, -border, -border); QRectF translatedRect = painter->combinedTransform().mapRect(avatarBoundingRect); QSize translatedSize = translatedRect.size().toSize(); QPixmap cachedPixmap; const QString cacheKey = "avatar" + QString::number(translatedSize.width()) + "_" + QString::number(info->user_level()) + "_" + QString::number(fullPixmap.cacheKey()); #if QT_VERSION >= 0x040600 if (!QPixmapCache::find(cacheKey, &cachedPixmap)) { #else if (!QPixmapCache::find(cacheKey, cachedPixmap)) { #endif cachedPixmap = QPixmap(translatedSize.width(), translatedSize.height()); QPainter tempPainter(&cachedPixmap); // pow(foo, 0.5) equals to sqrt(foo), but using sqrt(foo) in this context will produce a compile error with MSVC++ QRadialGradient grad(translatedRect.center(), pow(translatedSize.width() * translatedSize.width() + translatedSize.height() * translatedSize.height(), 0.5) / 2); grad.setColorAt(1, Qt::black); grad.setColorAt(0, QColor(180, 180, 180)); tempPainter.fillRect(QRectF(0, 0, translatedSize.width(), translatedSize.height()), grad); QPixmap tempPixmap; if (fullPixmap.isNull()) tempPixmap = UserLevelPixmapGenerator::generatePixmap(translatedSize.height(), UserLevelFlags(info->user_level()), false); else tempPixmap = fullPixmap.scaled(translatedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); tempPainter.drawPixmap((translatedSize.width() - tempPixmap.width()) / 2, (translatedSize.height() - tempPixmap.height()) / 2, tempPixmap); QPixmapCache::insert(cacheKey, cachedPixmap); } painter->save(); painter->resetTransform(); painter->translate((translatedSize.width() - cachedPixmap.width()) / 2.0, 0); painter->drawPixmap(translatedRect, cachedPixmap, cachedPixmap.rect()); painter->restore(); QRectF nameRect = QRectF(0, boundingRect().height() - 20, 110, 20); painter->fillRect(nameRect, QColor(0, 0, 0, 160)); QRectF translatedNameRect = painter->combinedTransform().mapRect(nameRect); painter->save(); painter->resetTransform(); QString name = QString::fromStdString(info->name()); if (name.size() > 13) name = name.mid(0, 10) + "..."; QFont font; font.setPixelSize(qMax((int) round(translatedNameRect.height() / 1.5), 9)); painter->setFont(font); painter->setPen(Qt::white); painter->drawText(translatedNameRect, Qt::AlignVCenter | Qt::AlignLeft, " " + name); painter->restore(); QPen pen(QColor(100, 100, 100)); pen.setWidth(border); pen.setJoinStyle(Qt::RoundJoin); painter->setPen(pen); painter->drawRect(boundingRect().adjusted(border / 2, border / 2, -border / 2, -border / 2)); if (getBeingPointedAt()) painter->fillRect(boundingRect(), QBrush(QColor(255, 0, 0, 100))); } AbstractCounter *PlayerTarget::addCounter(int _counterId, const QString &_name, int _value) { if (playerCounter) { disconnect(playerCounter, 0, this, 0); playerCounter->delCounter(); } playerCounter = new PlayerCounter(owner, _counterId, _name, _value, this); playerCounter->setPos(boundingRect().width() - playerCounter->boundingRect().width(), boundingRect().height() - playerCounter->boundingRect().height()); connect(playerCounter, SIGNAL(destroyed()), this, SLOT(counterDeleted())); return playerCounter; } void PlayerTarget::counterDeleted() { playerCounter = 0; }