Unable to fully remove border of PyQt QGraphicsView

喜你入骨 提交于 2021-02-07 19:11:43

问题


I have tried calling self.setStyleSheet("background: transparent; border: transparent;") on a QGraphicsView, but it still leaves a 1 pixel border on the top edge. I have also tried replacing border: transparent; with border-style: none;, but it didn't work either.

Here is a screenshot of the problem:

Problematic line

What command will fully remove the border from the QGraphicsView?


回答1:


You can use one of the following css rule:

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

or

graphicsView.setStyleSheet("border: 0px")

Your border should disappear.

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_())

Here is the widget with the default style:

And now the widget with the style applied:




回答2:


If the QGraphicsView is the top level window, try:

self.setContentsMargins(QMargins())

If not, you call the same function on every layouts and widgets (the function is defined in both classes) between the QGraphicsView and the top level window.

PS: QMargins() is part of QtCore, and when its constructor is called without any parameters, the four margins are set to 0.



来源:https://stackoverflow.com/questions/7325911/unable-to-fully-remove-border-of-pyqt-qgraphicsview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!