SBGit-2019 / Pyside-QCP

Pyside2 bindings for QCustomplot
Other
16 stars 1 forks source link

The same code, using pyqt5 is normal, pyside cannot set the coordinate axis to the time axis normally #4

Closed cd-86 closed 2 years ago

cd-86 commented 2 years ago

The same code, using pyqt5 is normal, pyside cannot set the coordinate axis to the time axis normally

# import time
# from PySide2.QtWidgets import QApplication
# from qcustomplot_pyside2 import QCustomPlot, QCP, QCPAxisTickerDateTime, QCPScatterStyle

# import time
# from PyQt5.QtWidgets import QApplication
# from QCustomPlot2 import QCustomPlot, QCP, QCPAxisTickerDateTime, QCPScatterStyle

app = QApplication([])
customPlot = QCustomPlot()
customPlot.resize(800, 600)
customPlot.setInteractions(QCP.iRangeZoom)
customPlot.addGraph()
tm = time.time()
data = [tm + i *1000 for i in range(100)], [0 for i in range(100)]
customPlot.graph(0).setData(*data)
customPlot.graph(0).setScatterStyle(QCPScatterStyle(QCPScatterStyle.ssCircle, 5))
dateTimeTicker = QCPAxisTickerDateTime()
dateTimeTicker.setDateTimeFormat("dd hh:mm:ss")
customPlot.xAxis.setTicker(dateTimeTicker)
customPlot.legend.setVisible(True)
customPlot.xAxis.setLabel("Time")
customPlot.yAxis.setLabel("Value")
customPlot.rescaleAxes()
customPlot.show()
app.exec_()
SBGit-2019 commented 2 years ago

Thank you for the good bug report. You did find a bug in the axis ticker. And thanks to the easy to follow example it was easy to fix. Please try version 2.1.3 on pypi or from here. It should work now.

cd-86 commented 2 years ago

Version 2.1.3 When deleting controls, it will cause a crash

import time

from PySide2.QtCore import Qt
from PySide2.QtGui import QPen, QBrush
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QLabel
from qcustomplot_pyside2 import QCustomPlot, QCP, QCPAxisTickerDateTime, QCPScatterStyle,QCPGraph

class CustomPlot(QCustomPlot):
    def __init__(self):
        super(CustomPlot, self).__init__()
        tm = time.time()
        data = [int(tm + i * 100) for i in range(100)], [i for i in range(100)]
        self.resize(800, 600)
        self.setInteractions(QCP.iRangeDrag | QCP.iRangeZoom | QCP.iSelectAxes)
        self.addGraph()
        self.graph(0).setData(data[0], data[1])
        self.graph(0).setLineStyle(QCPGraph.lsNone)
        self.graph(0).setScatterStyle(
            QCPScatterStyle(QCPScatterStyle.ssCircle, QPen(Qt.red, 1), QBrush(Qt.yellow), 6))
        #############################################
        # self must be used, and will cause a crash when removing the control
        self.dateTimeTicker = QCPAxisTickerDateTime()
        self.dateTimeTicker.setDateTimeFormat("dd hh:mm:ss")
        self.xAxis.setTicker(self.dateTimeTicker)
        #############################################
        self.legend.setVisible(True)
        self.xAxis.setLabel("Time")
        self.yAxis.setLabel("Value")
        self.rescaleAxes()

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.resize(800, 600)
        self.setLayout(QVBoxLayout())
        self.layout().setAlignment(Qt.AlignTop)
        self.button = QPushButton("add", self)
        self.button2 = QPushButton("remove", self)
        hBoxLayout = QHBoxLayout()
        hBoxLayout.addWidget(self.button)
        hBoxLayout.addWidget(self.button2)
        self.vBoxLayout = QVBoxLayout()
        self.layout().addLayout(hBoxLayout)
        self.layout().addLayout(self.vBoxLayout)

        self.button.clicked.connect(self.add)
        self.button2.clicked.connect(self.remove)

    def add(self):
        self.vBoxLayout.addWidget(CustomPlot())
        # With normal Qt controls it doesn't crash
        # self.vBoxLayout.addWidget(QLabel("label"))

    def remove(self):
        if self.vBoxLayout.count() > 0:
            widget = self.vBoxLayout.itemAt(0).widget()
            self.vBoxLayout.removeWidget(widget)
            widget.deleteLater()

if __name__ == '__main__':
    app = QApplication()
    window = Window()
    window.show()
    app.exec_()
SBGit-2019 commented 2 years ago

You are right again. Thank you! I will make a new release shortly.

SBGit-2019 commented 2 years ago

2.1.4 fixes the "remove" bug of your example. Please try it.