pyqt QChart 不显示任何结果

pyqt QChart dose not show any result

我正在使用 pyqt GUI 应用程序在来自 postgres 数据库的图表中显示一些结果,但它不起作用

这是我使用的代码

这就是我创建 table

的方式
create_table_transaction = ''' CREATE TABLE IF NOT EXISTS transactions (
            id SERIAL PRIMARY KEY  UNIQUE NOT NULL,
            montant DECIMAL(100,2), 
            medecin VARCHAR,
            date_d DATE, 
            time_d TIME,
            users_id INTEGER, 
            FOREIGN KEY(users_id) REFERENCES users(id)) '''

这是在Qchart widget中绘制图表的函数

from PyQt5 import *
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
from PyQt5.QtSql import *
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog
import sys, sqlite3
import psycopg2
import datetime
from datetime import timedelta
from PyQt5.QtCore import QDate

import sys

from PyQt5.QtChart import QChart, QLineSeries
from PyQt5.QtChart import *

from PyQt5.uic import loadUiType
from admin import Ui_MainWindow as ui

class MainApp(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.Handel_Buttons()


    def Handel_Buttons(self):
        self.pushButton_113.clicked.connect(self.draw_chart01)


    def draw_chart01(self): #pushButton_113
        
            self.connection = psycopg2.connect(user="postgres",
                                            password="password",
                                            host="localhost",
                                            database="database")
            self.cur = self.connection.cursor()

            date = str(self.dateEdit_19.text()) 

            self.cur.execute( '''SELECT medecin, montant FROM transactions WHERE date_d = %s ''',(date,))
            rows = self.cur.fetchall()

            rightseries = QPieSeries()

            for entry in rows:
                print(entry)
                rightseries.append(entry[0], entry[1])

            rightchart = QChart()
            rightchart.addSeries(rightseries)
            rightchart.setTitle("title")
            rightchart.setAnimationOptions(QChart.SeriesAnimations)

            self.graphicsView = QChartView(rightchart)
            self.graphicsView.setRenderHint(QPainter.Antialiasing)


if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    window = MainApp()
    window.show()
    sys.exit(app.exec_())

这是我的ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QChartView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>150</y>
      <width>531</width>
      <height>361</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_113">
    <property name="geometry">
     <rect>
      <x>550</x>
      <y>72</y>
      <width>121</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>Recherche</string>
    </property>
   </widget>
   <widget class="QDateEdit" name="dateEdit_19">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>70</y>
      <width>471</width>
      <height>41</height>
     </rect>
    </property>
    <property name="dateTime">
     <datetime>
      <hour>0</hour>
      <minute>0</minute>
      <second>0</second>
      <year>2020</year>
      <month>1</month>
      <day>1</day>
     </datetime>
    </property>
    <property name="calendarPopup">
     <bool>true</bool>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QChartView</class>
   <extends>QGraphicsView</extends>
   <header location="global">PyQt5.QtChart</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

当我 运行 这个函数 (draw_chart01) 时,它没有显示错误,在终端中它只显示打印(输入)的结果,如下所示:

('doc01', Decimal('1400.00'))
('doc01', Decimal('14000.00'))
('doc01', Decimal('1500.00'))

在我的 ui 文件中,我像这个问题中的答案一样提升了 graphicsView

self.graphicsView = QChartView(rightchart) 不会替换 QChartView,但“graphicsView”变量现在指向新的 QChartView,因此您会收到错误消息。解决方法是在已有的QChartView中设置QChart:

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtChart import QPieSeries, QChart

import psycopg2

from admin import Ui_MainWindow as ui


class MainApp(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.Handel_Buttons()

    def Handel_Buttons(self):
        self.pushButton_113.clicked.connect(self.draw_chart01)

    def draw_chart01(self):

        connection = psycopg2.connect(
            user="postgres", password="password", host="localhost", database="database"
        )
        cur = connection.cursor()

        date = str(self.dateEdit_19.text())

        cur.execute(
            """SELECT medecin, montant FROM transactions WHERE date_d = %s """, (date,)
        )

        rows = cur.fetchall()
        rightseries = QPieSeries()

        for medecin, montant in rows:
            rightseries.append(medecin, montant)

        rightchart = QChart()
        rightchart.addSeries(rightseries)
        rightchart.setTitle("title")
        rightchart.setAnimationOptions(QChart.SeriesAnimations)

        <b>self.graphicsView.setChart(rightchart)</b>


if __name__ == "__main__":

    app = QApplication(sys.argv)
    app.setStyle("Fusion")
    window = MainApp()
    window.show()
    sys.exit(app.exec_())