首页 > 解决方案 > 如何去除 Q-Chart 背景颜色并使其透明

问题描述

在此处输入图像描述

如何去除 Q-Chart 的背景颜色(白色)并使其透明。??这样我就可以在不覆盖应用程序中的其他标签的情况下最大化图表的大小。:)

我尝试了以下方法:

chart.setBackgroundBrush(QColor(Qt.transparent))
chart.setBackgroundBrush(QColor(255, 255, 255, 0))
chartView.setBackgroundBrush(QColor(Qt.transparent))
chartView.setBackgroundBrush(QColor(255, 255, 255, 0))

请参阅下面我使用的完整代码示例。我想在图表之外强调一个标签,但图表的背景覆盖了它。

# ==== Below is the functional Code (Start) ==== #

from PyQt5.QtWidgets import QApplication, QMainWindow, QFrame, QLabel
import sys
from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice
from PyQt5.QtGui import QPainter, QPen, QFont
from PyQt5.QtCore import Qt


class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("PyQtChart Pie Chart")
        self.setGeometry(100,100, 500,500)
        self.show()
        #self.create_piechart()
        self.sample_Frame()

    def sample_Frame(self):
        self.Frame1 = QFrame(self)
        self.Frame1.setFrameShape(QFrame.Box)
        self.Frame1.setFrameShadow(QFrame.Raised)
        self.Frame1.setGeometry(50, 50, 400, 400)
        self.Frame1.setStyleSheet('QFrame {background-color: gray}')

        labelFont = QFont('Calibri', 18)
        labelFont.setBold(True)
        self.Label1 = QLabel(self.Frame1)
        self.Label1.setText('The Quick Brown Fox \nJumps Over the Lazy Dog \nNear the River Bank')
        self.Label1.setFont(labelFont)
        self.Label1.move(10, 10)

        self.create_piechart()
        self.Frame1.show()

    def create_piechart(self):
        series = QPieSeries()
        series.append("Python", 80)
        series.append("C++", 70)
        series.append("Java", 50)
        series.append("C#", 40)
        series.append("PHP", 30)

        #adding slice
        slice = QPieSlice()
        slice = series.slices()[2]
        slice.setExploded(True)
        slice.setLabelVisible(True)
        slice.setPen(QPen(Qt.darkGreen, 2))
        slice.setBrush(Qt.green)

        chart = QChart()
        chart.legend().hide()
        chart.addSeries(series)
        chart.createDefaultAxes()
        chart.setAnimationOptions(QChart.SeriesAnimations)
        chart.legend().setVisible(True)
        chart.legend().setAlignment(Qt.AlignBottom)
        chartview = QChartView(chart)
        chartview.setRenderHint(QPainter.Antialiasing)
        chartview.setParent(self.Frame1)
        chartview.setGeometry(5, 50, 350, 350)
        chartview.show()


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())

标签: pythonpython-3.xpyqtpyqt5qchart

解决方案


当我尝试做同样的事情时,我也遇到了这个问题。我的解决方案如下(借用您的代码示例):

    chart = QChart()
    chart.legend().hide()
    chart.addSeries(series)
    chart.createDefaultAxes()
    chart.setAnimationOptions(QChart.SeriesAnimations)

    chart.setBackgroundBrush(QBrush(QColor("transparent")))

    chart.legend().setVisible(True)
    chart.legend().setAlignment(Qt.AlignBottom)
    chartview = QChartView(chart)
    chartview.setRenderHint(QPainter.Antialiasing)
    chartview.setParent(self.Frame1)
    chartview.setGeometry(5, 50, 350, 350)
    chartview.show()


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())

至少在我的使用和测试期间,这条线使背景透明而不是白色。看来您只是缺少 QBrush(..) 部分。

希望有帮助!
最好的,

凯文


推荐阅读