1

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?

houhou
  • 157
  • 1
  • 11
  • 1
    please provide a [mre] – eyllanesc Oct 12 '20 at 03:21
  • 1
    It looks like you haven't read the link (please take the time to do so). You are focusing on the "minimal" feature, which is the simplest since it only means deleting code and not on the most important feature: reproducibility, since it allows you to analyze the problem in detail. In simple words I need a code that can copy, paste, execute and thus reproduce your problem, and clearly your code is not. – eyllanesc Oct 12 '20 at 03:48
  • thank you for the explanation i added the main py file and the ui file – houhou Oct 12 '20 at 04:17

1 Answers1

2

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you it worked perfectly and thank you for the explanation of how I ask a question in stack overflow – houhou Oct 12 '20 at 04:40