【问题标题】:Unable to fully remove border of PyQt QGraphicsView无法完全删除 PyQt QGraphicsView 的边框
【发布时间】:2011-11-11 15:49:14
【问题描述】:

我尝试在 QGraphicsView 上调用 self.setStyleSheet("background: transparent; border: transparent;"),但它仍然在顶部边缘留下 1 个像素的边框。我也试过用border-style: none;替换border: transparent;,但也没用。

这是问题的截图:

什么命令会从 QGraphicsView 中完全移除边框?

【问题讨论】:

    标签: python css qt pyqt qgraphicsview


    【解决方案1】:

    您可以使用以下 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_())
    

    这是具有默认样式的小部件:

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

    【讨论】:

    • 你的例子也有同样的问题。我已经用屏幕截图更新了问题。
    • 我提供的示例是在 Mac Os X 和 Windows 上运行,您使用的是什么版本的 Qt / PyQt?我在 PyQt Qt 4.7.3 和 4.8.3 上。从我在您的屏幕截图中看到的,您已经有一个样式接管了您的窗口,您确定它没有覆盖您的样式表吗?
    • 我在 Kubuntu 11.04 上使用 Qt 4.7.2。花哨的窗口颜色只是我的系统主题;即使使用默认主题也会出现问题。
    • graphicsView 是否显示任何边框样式覆盖?例如类似的东西:graphicsView.setStyleSheet("border: 8px solid red;")
    • 关于问题可能是什么的任何其他想法? (“完美运行”指的是 8 像素粗的红线)。
    【解决方案2】:

    如果QGraphicsView 是顶层窗口,请尝试:

    self.setContentsMargins(QMargins())
    

    如果没有,您在 QGraphicsView 和顶级窗口之间的每个布局和小部件(该函数在两个类中都定义)上调用相同的函数。

    PS:QMargins()是QtCore的一部分,在不带任何参数调用它的构造函数时,四个边距设置为0。

    【讨论】:

    • 有用的提示,但它不会删除线。
    猜你喜欢
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2015-10-20
    • 2020-02-25
    相关资源
    最近更新 更多