I am currently developing a small programm in Qt. To show a plot you can use qwt or qcustomplot or the qpainterevent or QChart. But I am interessted in a solution for a dynamic plot which is writen with the QGraphicsView.
My preferences -width of my chart should be constant -realtime plotting -the first sample should be deleted or overwriten if the end of the chart is reached, so it is a dynamic and fluent chart
My example beneath is able to be dynamic and fluent... but just for the number, which is in my if clause. I do not get why.
The idea is to delete the first lineitem, so I have constantly 99 items. If I delete a item I want to give the next item the position from the item before. So x=99 will be x=98 ......x=1 will be x=0;
Do I have a mistake in my idea? I also have had several ideas, but this is probably the best.
Thanks in advance Konrad
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
vectorPoint = new QVector<QPoint>;
line = new QVector<QGraphicsLineItem*>;
yDatai = 0;
xDatai = 0;
Grenzenlaufvariable = 0;
timer = new QTimer(this);
timer->start(10);
connect (timer, SIGNAL(timeout()),this,SLOT(newData()));
connect(this,SIGNAL(newPaint()),this,SLOT(paint()));
}
MainWindow::~MainWindow()
{
delete ui;
delete scene;
delete vectorPoint;
delete line;
}
void MainWindow::newData()
{
if (yDatai == 100 || yDatai == -100)
{
Grenzenlaufvariable++;
}
if (Grenzenlaufvariable%2==0)
{
yDatai+=1;
}
else
{
yDatai-=1;
}
xDatai++;
point = {xDatai,yDatai};
vectorPoint->append(point);
if(vectorPoint->size()>1)
{
item = scene->addLine(QLineF(vectorPoint->at(ix-1),vectorPoint->at(ix)));
line->append(item);
}
ix++;
emit newPaint();
}
void MainWindow::paint()
{
if(line->size()==99)
{
scene->removeItem(line->at(0));
line->removeAt(0);
qDebug()<<line->size();
for (int ip= 0;ip <line->size();ip++)
{
oldx = line->at(ip)->x();
line->at(ip)->setX(oldx-1);
qDebug()<<ip;
}
}
}