pyqt QChart dose not show any result

341 views Asked by At

I am using a pyqt GUI application to show some result in charts from postgres database, but it did not work

here is my code that i use

this is how i creat my 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)) '''

and this is the fucntion that draw the chart in the 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_())

this is my 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>

whene i run this fucntion (draw_chart01) it show me no error , in the terminal it shwo me only the result of printing (entry) like this :

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

in my ui file i promote the graphicsView like the answer in this question How to insert QChartView in form with Qt Designer?

1

There are 1 answers

1
eyllanesc On BEST ANSWER

Doing self.graphicsView = QChartView(rightchart) does not replace the QChartView but the "graphicsView" variable now directs the new QChartView so you get the error. The solution is to set the QChart in the existing QChartView:

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)

        self.graphicsView.setChart(rightchart)


if __name__ == "__main__":

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