无法完全去除PyQt QGraphicsView的边框

7 投票
2 回答
9243 浏览
提问于 2025-04-17 01:37

我尝试在一个 QGraphicsView 上调用 self.setStyleSheet("background: transparent; border: transparent;"),但是顶部边缘还是留了一个 1 像素的边框。我还试过把 border: transparent; 换成 border-style: none;,但也没有效果。

这里有个问题的截图:

问题的截图

有什么命令可以完全去掉 QGraphicsView 的边框吗?

2 个回答

0

如果QGraphicsView是最上层的窗口,可以试试:

self.setContentsMargins(QMargins())

如果不是,你需要在QGraphicsView和最上层窗口之间的每个布局和小部件上调用同样的函数(这个函数在两个类中都有定义)。

另外,QMargins()是QtCore的一部分,当它的构造函数没有参数时,四个边距都会被设置为0。

10

你可以使用以下其中一个CSS规则:

graphicsView.setStyleSheet("border-width: 0px; border-style: solid")

或者

graphicsView.setStyleSheet("border: 0px")

这样你的边框就会消失了。

import sys
from PyQt4.QtGui import *

class Ui(QWidget):

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        graphicsView = QGraphicsView()
        graphicsView.setStyleSheet("border: 0px")

        grid = QGridLayout()
        grid.addWidget(graphicsView)

        self.setLayout(grid)

app = QApplication(sys.argv)
ui = Ui()
ui.show()
sys.exit(app.exec_())

这是一个默认样式的小部件:

在这里输入图片描述

现在这是应用了样式的小部件:

在这里输入图片描述

撰写回答