Home>

Hello! I rewrote an example of BarChart from Qt on PyQT. Everything works great. Here is the code (maybe someone will be interested)

# -* -Coding: UTF-8 -* -
from pyqt5.qtchart import qchart, qchartview, qbarseries, qbarset, qglegend, qbarcategoryaxis
from pyqt5.qtwidgets import qmainwindow
Import sys
from pyqt5.qtwidgets import qapplication
from pyqt5.Qtcore Import Qt
from pyqt5.Qtgui import qpainter
Class Testwindow (QMainWindow):
    Def __init __ (Self, Parent= None):
        Super (TestWindow, Self) .__ init __ (Parent= Parent)
        SET0= QBARSET ("Jane")
        SET1= QBARSET ("John")
        SET2= QBARSET ("Alexander")
        SET3= QBARSET ("Maria")
        SET4= Qbarset ("Samantha")
        For a in [1,2,3,4,5,6]: This is data for Jane. In January 1, in February 2, ...
            SET0.APPEND (A)
        For a in [5,2,3,4,5,6]: This is data for John. In January 5, in February 2, ...
            SET1.APPEND (A)
        For a in [1,5,3,4,5,6]:
            SET2.APPEND (A)
        For a in [1,2,5,4,5,6]:
            SET3.APPEND (A)
        For a in [1,2,3,5,5,6]:
            SET4.APPEND (A)
        Series= Qbarseries ()
        Series.APPEND (SET0)
        Series.APPEND (SET1)
        Series.APPEND (SET2)
        Series.APPEND (SET3)
        Series.APPEND (SET4)
        Chart= Qchart ()
        CHART.ADDSERIES (SERIES)
        CHART.SETTITLE ("Example of a column chart")
        # Chart.SetanimationOptions (Qchart.SeriesAnimations)
        CHART.SETANIMATIONOPTIONS (qchart.allanimations)
        Categories= ["January", "February", "March", "April", "May", "June"]
        axis= qbarcategoryaxis ()
        Axis.APPEND (Categories)
        Chart.CreateDefaultAxes ()
        CHART.SETAXISX (AXIS, SERIES)
        Chart.legend (). SetVisible (True)
        Chart.legend (). SetAlignment (Qt.alignBottom)
        CHARTVIEW= QChartView (Chart)
        Chartview.setRenderhint (Qpainter.antialiaSing)
        Self.setCentralWidget (Chartview
if __name__== '__main__':
    App= Qapplication (SYS.Argv)
    Window= TestWindow ()
    Window.setWindowTitle ("Star Chart")
    Window.Resize (500, 400)
    Window.Show ()
    sys.exit (app.exec_ ())

Next rewrote an example of PieChart. But it does not work. Falls out with a mistake: Segmentation Fault (Core Dumped) Program Exited With Code: 139) It seems that it falls out on the chart= Qchart () line Here is the code of the example (do not pay attention to the supervisor).

Import Sys
Import Time.
from pyqt5.Qtcore import (Qt, QSIZE, QPOINT, QRECT)
from pyqt5.qtwidgets import (qtyleditemdelegate, qtylication, qmainwindow, qsplashscreen,
                             qapp, qtabwidget, qmessagebox, qwidget, qvboxlayout, qhboxlayout,
                             QTableView, QPushButton, QradioButton, QButtongroup, QDateEdit,
                             Qcalendarwidget, QLabel, QComboBox)
from pyqt5.qtsql import (qsqldatabase, qsqlrelationaltablemodel, qsqlrelation, qsqlrelationaldelegate,
                             QSQLTableModel, QSQLQuery)
from pyqt5.Qtgui import Qpainter, Qpixmap, Qicon, QPEN
from pyqt5.QTChart Import (Qchart, Qchartview, Qbarseries, Qpieseries, Qpieslice,
                             Qbarset, Qlegend, QbarcategoryAxis)
Class TestWindow (QWidget):
    Def __init __ (Self, Parent= None):
        Super (TestWindow, Self) .__ init __ (Parent= Parent)
    Series= Qpieseries ()
    Series.APPEND ("Jane", 1)
    Series.APPEND ("Joe", 2)
    series.append ("Andy", 3)
    Series.APPEND ("Barbara", 4)
    series.append ("Axel", 5)
    Slice= Series.slices () [1]
    Slice.seTexploded ()
    Slice.Setlabelvisible ()
    Slice.Setpen (QT.DarkReen, 2))
    Slice.Setbrush (Qt.Green)
    Chart= Qchart () !!!!!! It seems here to fall out
    CHART.ADDSERIES (SERIES)Chart.Settitle ("Simple Piechart Example") Chart.legend (). Hide ()
    CHARTVIEW= QChartView (Chart)
    Chartview.setRenderhint (Qpainter.antialiaSing)
    Self.setCentralWidget (Chartview
if __name__== '__main__':
    App= Qapplication (SYS.Argv)
    Window= TestWindow ()
    # Window.setwindowtitle ("Pie")
    Window.Resize (500, 400)
    Window.Show ()
    sys.exit (app.exec_ ())
  • Answer # 1

    Something corrected in the code:

    Import Sys
    from pyqt5.Qtcore Import Qt
    from pyqt5.qtwidgets import qapplication, qmainwindow
    from pyqt5.Qtgui import qpainter, qpen
    from pyqt5.QTChart Import (Qchart, Qchartview, Qbarseries, Qpieseries, Qpieslice,
                               Qbarset, Qlegend, QbarcategoryAxis)
    Class Testwindow (QMainWindow):
        Def __init __ (Self, Parent= None):
            Super () .__ INIT __ (Parent= Parent)
            Series= Qpieseries ()
            Series.APPEND ("Jane", 1)
            Series.APPEND ("Joe", 2)
            series.append ("Andy", 3)
            Series.APPEND ("Barbara", 4)
            series.append ("Axel", 5)
            Slice= Series.slices () [1]
            Slice.seTexploded ()
            Slice.Setlabelvisible ()
            Slice.Setpen (QT.DarkReen, 2))
            Slice.Setbrush (Qt.Green)
            Chart= Qchart ()
            CHART.ADDSERIES (SERIES)
            Chart.Settitle ("Simple Piechart Example")
            Chart.legend (). Hide ()
            CHARTVIEW= QChartView (Chart)
            Chartview.setRenderhint (Qpainter.antialiaSing)
            Self.setCentralWidget (Chartview
    if __name__== '__main__':
        App= Qapplication (SYS.Argv)
        Window= TestWindow ()
        Window.Resize (500, 400)
        Window.Show ()
        sys.exit (app.exec_ ())
    
  • Answer # 2

    Please note that the entire initialization code Qchart-A is not located in the designer, but in the class.

    it is executed accordingly before initializing the QApplication, which leads to a segfolit.

    It is unlikely that this is the case, because Qchart initialization occurs in the widget class designer, which is initialized after creating Qapplicaton

    gil9red2021-04-09 08:37:34

    Look carefully on the code. Quick settings retreats are the same as an indent of the designer signature.

    tonal2021-04-09 08:37:34

    Perhaps you are right (now I can not check his code). Then, if you checked on a mistake by making the right indents, I propose a corrected code in response to put (I would simit him along the way, and then so much excess ... :))

    gil9red2021-04-09 08:37:34

    You are right, the case was in the returns and the error occurred on the chart= Qchart () line

    gil9red2021-04-09 08:37:34

    Thank you very much! The point is indeed in the departures (children's mistake)

    Александр Остыловский2021-04-09 08:37:34