#ifndef CUSTOMGRAPHICSITEMGROUP_H #define CUSTOMGRAPHICSITEMGROUP_H #include #include class RoundedGraphicsItemGroup : public QGraphicsItemGroup { public: RoundedGraphicsItemGroup(QGraphicsItem *parent = nullptr) : QGraphicsItemGroup(parent), selected(false) { //setFlag(QGraphicsItem::ItemIsSelectable, true); // 使该项可选中 } // 设置圆角半径 void setCornerRadius(qreal radius) { cornerRadius = radius; prepareGeometryChange(); } QRectF boundingRect() const override { return QGraphicsItemGroup::boundingRect(); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override { Q_UNUSED(option) Q_UNUSED(widget) // 启用抗锯齿 painter->setRenderHint(QPainter::Antialiasing, true); // 绘制圆角矩形背景 painter->setBrush(QBrush(QColor(255, 255, 255))); painter->setPen(Qt::NoPen); painter->drawRoundedRect(boundingRect(), cornerRadius, cornerRadius); // 绘制选中效果 if (isSelected()) { QPen pen(QColor(36,93,155), 3); // 蓝色边框 painter->setPen(pen); painter->setBrush(Qt::NoBrush); painter->drawRoundedRect(boundingRect().adjusted(1.5, 1.5, -1.5, -1.5), cornerRadius, cornerRadius); } } protected: void mousePressEvent(QGraphicsSceneMouseEvent *event) override { QGraphicsItemGroup::mousePressEvent(event); update(); // 更新选中效果 } void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override { QGraphicsItemGroup::mouseReleaseEvent(event); update(); // 更新选中效果 } private: qreal cornerRadius = 10.0; // 默认圆角半径 bool selected; // 是否被选中 }; #endif // CUSTOMGRAPHICSITEMGROUP_H