QPushButton设置图片居中显示

background-image: url(:/images/trail_not.png);outline:none;background-color:transparent;background-position:center;background-repeat:no-repeat;

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <QtWidgets>class MyItem : public QGraphicsObject{public: MyItem(int pinCount, QColor color, QSizeF size, QGraphicsItem* parent = nullptr) : QGraphicsObject(parent) , m_pinCount(pinCount) , m_color(color) , m_size(size) {} QRectF boundingRect() const override { return QRectF(QPointF(0, 0), m_size); } QPainterPath shape() const override { QPainterPath path; path.addRect(QRectF(QPointF(0, 0), m_size)); return path; } void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override { Q_UNUSED(option) Q_UNUSED(widget) painter->setPen(Qt::NoPen); painter->setBrush(m_color); painter->drawRect(boundingRect()); painter->setPen(Qt::black); painter->setFont(QFont("Arial", 10)); painter->drawText(boundingRect(), Qt::AlignCenter, QString("%1 pins").arg(m_pinCount)); } void mousePressEvent(QGraphicsSceneMouseEvent* event) override { if (event->button() == Qt::LeftButton) { setCursor(Qt::ClosedHandCursor); m_lastPos = event->scenePos(); } } void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override { if (QLineF(event->screenPos(), m_lastPos).length() < QApplication::startDragDistance()) return; QGraphicsObject::mouseMoveEvent(event); setPos(pos() + event->scenePos() - m_lastPos); m_lastPos = event->scenePos(); } void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override { if (event->button() == Qt::LeftButton) { setCursor(Qt::ArrowCursor); } }private: int m_pinCount; QColor m_color; QSizeF m_size; QPointF m_lastPos;};int main(int argc, char* argv[]){ QApplication app(argc, argv); QGraphicsScene scene; scene.setSceneRect(0, 0, 800, 600); MyItem* item1 = new MyItem(8, Qt::red, QSizeF(80, 60)); item1->setPos(100, 100); scene.addItem(item1); MyItem* item2 = new MyItem(16, Qt::green, QSizeF(120, 80)); item2->setPos(300, 200); scene.addItem(item2); QGraphicsView view(&scene); view.setRenderHint(QPainter::Antialiasing); view.setDragMode(QGraphicsView::RubberBandDrag); view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate); view.show(); return app.exec();}在这个代码中,如何使用TextOut去给矩形框中添加文本
05-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值