如果要设置QGraphicsView为圆角边框后,圆角部分不透明,您可以使用以下方法:
- 设置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);
- 如果您希望在圆角处使用其他颜色或纹理来填充,可以通过重写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);
}
};
以上是两种实现方法,根据您的具体需求选择即可。希望能对您有所帮助!