如何去除 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_())