QT开发中文网

如何设置QGraphicsView为圆角边框后,圆角部分不透明

如果要设置QGraphicsView为圆角边框后,圆角部分不透明,您可以使用以下方法:

  1. 设置QGraphicsView的背景色与父容器相同,并将视图的属性WA_TranslucentBackground设置为false。这样可以使视图背景色变得不透明。
// 创建 QFrame 并设置圆角
QFrame *frame = new QFrame();
frame->setStyleSheet("border-radius: 10px; background-color: white;");

// 将 QGraphicsView 放到 QFrame 中
QGraphicsView *view = new QGraphicsView(frame);

// 设置 QGraphicsView 的背景颜色为白色并且不透明
view->setBackgroundBrush(QBrush(Qt::white));
view->setAutoFillBackground(true);
view->setAttribute(Qt::WA_TranslucentBackground, false);
  1. 如果您希望在圆角处使用其他颜色或纹理来填充,可以通过重写QGraphicsView的paintEvent方法,在绘制时手动填充圆角区域。
class RoundRectGraphicsView : public QGraphicsView {
public:
    RoundRectGraphicsView(QWidget *parent = nullptr) : QGraphicsView(parent) {}

protected:
    void paintEvent(QPaintEvent *event) override {
        // 调用父类paintEvent方法进行默认绘制
        QGraphicsView::paintEvent(event);

        QPainter painter(viewport());

        // 填充圆角矩形区域
        QColor bgColor(0, 255, 0);  // 圆角区域背景颜色
        painter.setBrush(bgColor);
        painter.setPen(Qt::NoPen);

        QRectF rect = viewport()->rect();
        qreal radius = 20.0;  // 圆角半径
        QPainterPath path;
        path.addRoundedRect(rect, radius, radius);
        painter.drawPath(path);
    }
};

以上是两种实现方法,根据您的具体需求选择即可。希望能对您有所帮助!

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=1616

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?