I have seen this question already being asked:
in Qt forum topic
in another Qt forum topic
in yet another Qt forum topic
and even in SO
Surprisingly I found no solutions (well, qcustomplot has this feature, but we are not allowed to use it in my company). My task is to represent some scientific data as a line plot with error bars representing the data standard deviation. I have tried some hacks to solve it, but the results are not sufficient for our application:
creating one QLineSeries for data mean values, and then add N QLineSeries to represent error bars (see approachWithMultipleQLineSeries in the example below, ); this is clearly an overkill and my plot is glitching on the slower machines (with Windows :/)
creating one QLineSeries for data mean values and then add QBoxPlotSeries to represent error bars (see approachWithQBoxSet in the example below, ); this does not glitch anymore, but the error bars do not correspond well to the line plot (e.g. look at the values close to zero), probably because Boxes are designed for slightly different X axes (with categories, not floating point numbers);
Could you please suggest some better approach where error bar values will actually be associated with X value and mean Y value (maybe subclass QXYSeries, or sth similar, I do not have any good ideas how to do it exactly)?
MCVE if someone would like to try:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.11.0)
project(QtChartsErrBars CXX)
set(CMAKE_CXX_STANDARD 17)
find_package(Qt5 COMPONENTS Charts Core Widgets REQUIRED)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
add_executable(QtChartsErrBars
main.cpp
Demo.cpp
Demo.hpp
)
target_link_libraries(QtChartsErrBars
Qt5::Charts
Qt5::Core
Qt5::Widgets
)
target_compile_options(QtChartsErrBars PUBLIC -g)
Demo.hpp:
#pragma once
/**
* /author Pawel Ptasznik
*/
#include <QMainWindow>
#include <QWidget>
#include <memory>
class Demo : public QMainWindow
{
Q_OBJECT
public:
explicit Demo(QWidget* parent = nullptr);
~Demo() override = default;
};
Demo.cpp:
#include "Demo.hpp"
#include <QDebug>
#include <QVector>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QAreaSeries>
#include <QtCharts/QLegendMarker>
#include <QtCharts/QBoxSet>
#include <QtCharts/QBoxPlotSeries>
#include <algorithm>
#include <random>
static std::random_device rd{};
static std::mt19937 gen{ rd() };
// for each input generate n samples using normal distribution with expected value == x^2
QVector<QVector<double> > generateData(QVector<double> in, int nSamples)
{
QVector<QVector<double> > results(in.size());
for (int i = 0; i < in.size(); i++)
{
results[i].resize(nSamples);
std::normal_distribution<double> d{ in[i]*in[i], 1 };
for (auto& sample : results[i])
{
sample = d(gen);
}
}
return results;
}
/************* HERE the solutions I have tried and I do not like ************/
namespace
{
class VerticalLine : public QtCharts::QLineSeries
{
public:
VerticalLine(double x, double yMin, double yMax)
{
replace(QVector<QPointF>{ { x, yMin }, { x, yMax } });
}
};
}
void approachWithMultipleQLineSeries(QtCharts::QChart *chart,
const QVector<double>& x, const QVector<double>& yMean, const QVector<double>& yStdDev)
{
for (int i = 0; i < x.size(); i++)
{
auto series = new VerticalLine(x[i], yMean[i]-yStdDev[i], yMean[i]+yStdDev[i]);
chart->addSeries(series);
chart->legend()->markers(series)[0]->setVisible(false);
}
}
void approachWithQBoxSet(QtCharts::QChart *chart,
const QVector<double>& x, const QVector<double>& yMean, const QVector<double>& yStdDev)
{
QList<QtCharts::QBoxSet*> boxList;
for (int i = 0; i < x.size(); i++)
{
boxList.push_back(new QtCharts::QBoxSet(yMean[i]-yStdDev[i], yMean[i]-yStdDev[i]/2,
yMean[i], yMean[i]+yStdDev[i]/2, yMean[i]+yStdDev[i]));
}
auto series = new QtCharts::QBoxPlotSeries();
series->append(boxList);
series->setBoxWidth(0.1);
series->attachAxis(chart->axes(Qt::Horizontal)[0]);
chart->addSeries(series);
chart->legend()->markers(series)[0]->setVisible(false);
}
/************* HERE quick example that plots quadratic function over 200 points ************/
Demo::Demo(QWidget* parent): QMainWindow(parent)
{
QVector<double> xAxisValues(200); // lets have 200 points between -10 and 10
std::generate(xAxisValues.begin(), xAxisValues.end(), [x = -10.0]() mutable { return x += 0.1; });
QVector<double> yAxisMeanValues(xAxisValues.size());
QVector<double> yAxisStdDev(xAxisValues.size());
// lets generate 50 samples for each x value
auto data = generateData(xAxisValues, 50);
// calculating mean values
std::transform(data.begin(), data.end(), yAxisMeanValues.begin(), [](const auto& samples)
{
double sum = 0.0;
for (auto sample : samples) sum += sample;
return sum / samples.size();
});
// calculating standard deviation
std::transform(data.begin(), data.end(), yAxisMeanValues.begin(), yAxisStdDev.begin(), [](const auto& samples, auto mean)
{
double var = 0.0;
for (auto sample : samples) var += (sample - mean)*(sample - mean);
var /= samples.size();
return std::sqrt(var);
});
QtCharts::QLineSeries *series = new QtCharts::QLineSeries();
for (int i = 0; i < xAxisValues.size(); i++)
{
series->append(xAxisValues[i], yAxisMeanValues[i]);
}
QtCharts::QChart *chart = new QtCharts::QChart();
chart->addSeries(series);
// OK, now how do I add the error bars basing on x, yMean, yStdDev?
// approachWithMultipleQLineSeries(chart, xAxisValues, yAxisMeanValues, yAxisStdDev);
chart->createDefaultAxes();
approachWithQBoxSet(chart, xAxisValues, yAxisMeanValues, yAxisStdDev);
chart->setTitle("Demo chart");
QtCharts::QChartView *chartView = new QtCharts::QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
setCentralWidget(chartView);
resize(400, 300);
}
main.cpp:
#include "Demo.hpp"
#include <QApplication>
int main(int argc, char** argv)
{
QApplication a(argc, argv);
Demo mainWindow;
mainWindow.show();
return a.exec();
}
Related
I have a QT chart with regular value axes. When I toggle a checkbox, I want to change the X Axis from a QValueAxis to a QLogValueAxis. The problem is, when I do this, my data no longer plots to the correct point.
I've tried two approaches (and a bunch of variations on them) to get the log scale to work, but no what I've tried it seems like the data scales itself to fit in the window linearly and ignores the log axis completely.
Approach 1 - Replace the old axis:
QLogValueAxis* xLogAxis = new QLogValueAxis();
xLogAxis->setBase(10);
xLogAxis->setMinorTickCount(10);
dataSeries->attachedAxes()[0] = xLogAxis; //Replace the old X Axis on the series
chart->setAxisX(xLogAxis);
Approach 2 - Make a completely new Chart:
chart->removeSeries(data); //release the data so you don't destroy it
QChart* newChart = new QChart();
ui->graphView->setChart(newChart); //Swap the old chart out then delete it
delete chart;
chart = newChart;
//get rid of the old axes on the data
for(QAbstractAxis* axis : data->attachedAxes()){
data->detachAxis(axis);
}
QValueAxis* yAxis = new QValueAxis();
data->attachAxis(xLogAxis);
data->attachAxis(yAxis);
chart->addAxis(xLogAxis, Qt::AlignBottom);
chart->addAxis(yAxis, Qt::AlignLeft);
chart->addSeries(data);
chart->legend()->setVisible(false);
Any ideas on how to hotswap to a log axis at runtime? Thanks in advance!
In the following example how to exchange axis types:
#include <QtWidgets>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
typedef std::function<qreal (const qreal &)> function;
static std::vector<std::pair<function, std::string>> functions{
{[](const qreal & v){ return v;}, "linear"},
{[](const qreal & v){ return v*v; }, "quadratic"},
{[](const qreal & v){ return std::exp(0.01*v);}, "exponential"},
{[](const qreal & v){ return std::sqrt(1 + std::abs(v));}, "square root"}
};
class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent=nullptr):
QWidget(parent),
xLogAxis(new QLogValueAxis),
xLinAxis(new QValueAxis),
yAxis(new QValueAxis)
{
xLogAxis->setBase(10);
xLogAxis->setMinorTickCount(10);
view.setChart(&chart);
checkbox.setText("Log Axis");
connect(&checkbox, &QCheckBox::stateChanged, this, &Widget::onStateChanged);
QVBoxLayout *lay = new QVBoxLayout(this);
lay->addWidget(&checkbox);
lay->addWidget(&view);
chart.addAxis(yAxis, Qt::AlignLeft);
// chart.legend()->hide();
// create series
for(const std::pair<function, std::string> & func: functions){
QLineSeries *serie = new QLineSeries;
serie->setName(QString::fromStdString(func.second));
for(int i=0; i< 1000; ++i){
*serie << QPointF(i+1, func.first(i));
}
chart.addSeries(serie);
serie->attachAxis(yAxis);
}
onStateChanged(checkbox.checkState());
}
private slots:
void onStateChanged(int state){
QAbstractAxis *removeaxis, *insertaxis;
if(state == Qt::Checked){
removeaxis = xLinAxis;
insertaxis = xLogAxis;;
}
else{
removeaxis = xLogAxis;
insertaxis = xLinAxis;
}
if(chart.axes(Qt::Horizontal).contains(removeaxis))
chart.removeAxis(removeaxis);
chart.addAxis(insertaxis, Qt::AlignBottom);
for(auto serie: chart.series()){
if(serie->attachedAxes().contains(removeaxis))
serie->detachAxis(removeaxis);
serie->attachAxis(insertaxis);
}
}
private:
QCheckBox checkbox;
QChartView view;
QChart chart;
QLogValueAxis *xLogAxis;
QValueAxis *xLinAxis;
QValueAxis *yAxis;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(640, 480);
w.show();
return a.exec();
}
#include "main.moc"
I'm trying to adapt the Qt5.9 QML Oscilloscope example to have the graph data pushed from c++ rather than requested from QML. Below are the pertinent sections from the QML Oscilloscope example.
datasource.h:
#ifndef DATASOURCE_H
#define DATASOURCE_H
#include <QtCore/QObject>
#include <QtCharts/QAbstractSeries>
QT_BEGIN_NAMESPACE
class QQuickView;
QT_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
class DataSource : public QObject
{
Q_OBJECT
public:
explicit DataSource(QQuickView *appViewer, QObject *parent = 0);
Q_SIGNALS:
public slots:
void generateData(int type, int rowCount, int colCount);
void update(QAbstractSeries *series);
private:
QQuickView *m_appViewer;
QList<QVector<QPointF> > m_data;
int m_index;
};
#endif // DATASOURCE_H
datasource.cpp:
#include "datasource.h"
#include <QtCharts/QXYSeries>
#include <QtCharts/QAreaSeries>
#include <QtQuick/QQuickView>
#include <QtQuick/QQuickItem>
#include <QtCore/QDebug>
#include <QtCore/QtMath>
QT_CHARTS_USE_NAMESPACE
Q_DECLARE_METATYPE(QAbstractSeries *)
Q_DECLARE_METATYPE(QAbstractAxis *)
DataSource::DataSource(QQuickView *appViewer, QObject *parent) :
QObject(parent),
m_appViewer(appViewer),
m_index(-1)
{
qRegisterMetaType<QAbstractSeries*>();
qRegisterMetaType<QAbstractAxis*>();
generateData(0, 5, 1024);
}
void DataSource::update(QAbstractSeries *series)
{
if (series) {
QXYSeries *xySeries = static_cast<QXYSeries *>(series);
m_index++;
if (m_index > m_data.count() - 1)
m_index = 0;
QVector<QPointF> points = m_data.at(m_index);
// Use replace instead of clear + append, it's optimized for performance
xySeries->replace(points);
}
}
void DataSource::generateData(int type, int rowCount, int colCount)
{
// Remove previous data
m_data.clear();
// Append the new data depending on the type
for (int i(0); i < rowCount; i++) {
QVector<QPointF> points;
points.reserve(colCount);
for (int j(0); j < colCount; j++) {
qreal x(0);
qreal y(0);
switch (type) {
case 0:
// data with sin + random component
y = qSin(3.14159265358979 / 50 * j) + 0.5 + (qreal) rand() / (qreal) RAND_MAX;
x = j;
break;
case 1:
// linear data
x = j;
y = (qreal) i / 10;
break;
default:
// unknown, do nothing
break;
}
points.append(QPointF(x, y));
}
m_data.append(points);
}
}
main.cpp:
#include <QtWidgets/QApplication>
#include <QtQml/QQmlContext>
#include <QtQuick/QQuickView>
#include <QtQml/QQmlEngine>
#include <QtCore/QDir>
#include "datasource.h"
int main(int argc, char *argv[])
{
// Qt Charts uses Qt Graphics View Framework for drawing, therefore
QApplication must be used.
QApplication app(argc, argv);
QQuickView viewer;
// The following are needed to make examples run without having to install the module
// in desktop environments.
#ifdef Q_OS_WIN
QString extraImportPath(QStringLiteral("%1/../../../../%2"));
#else
QString extraImportPath(QStringLiteral("%1/../../../%2"));
#endif
viewer.engine()->addImportPath(extraImportPath.arg(QGuiApplication::applicationDirPath(),
QString::fromLatin1("qml")));
//QObject::connect(viewer.engine(), &QQmlEngine::quit, &viewer, &QWindow::close);
viewer.setTitle(QStringLiteral("QML Oscilloscope"));
DataSource dataSource(&viewer);
viewer.rootContext()->setContextProperty("dataSource", &dataSource);
viewer.setSource(QUrl("qrc:/qml/qmloscilloscope/main.qml"));
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
viewer.setColor(QColor("#404040"));
viewer.show();
return app.exec();
}
ScopeView.qml:
import QtQuick 2.0
import QtCharts 2.1
ChartView {
id: chartView
animationOptions: ChartView.NoAnimation
theme: ChartView.ChartThemeDark
property bool openGL: true
property bool openGLSupported: true
onOpenGLChanged: {
if (openGLSupported) {
series("signal 1").useOpenGL = openGL;
}
}
Component.onCompleted: {
if (!series("signal 1").useOpenGL) {
openGLSupported = false
openGL = false
}
}
ValueAxis {
id: axisY1
min: -1
max: 4
}
ValueAxis {
id: axisX
min: 0
max: 1024
}
LineSeries {
id: lineSeries1
name: "signal 1"
axisX: axisX
axisY: axisY1
useOpenGL: chartView.openGL
}
Timer {
id: refreshTimer
interval: 1 / 60 * 1000 // 60 Hz
running: true
repeat: true
onTriggered: {
dataSource.update(chartView.series(0));
}
}
}
Rather than using the Timer in QML, I'd like to use an existing Timeout in a c++ class to push new data to the QML ChartView. I have two questions:
How would I achieve this for the QML Oscilloscope example posted above?
What format would be most suitable for the c++ data to facilitate this? I'm thinking a QVector of some sort; the data will be an integer or float with a vector index.
As you say in a comment you need to pass a series, then we create a method that receives the series and saves it in a member of the C ++ class, We also create a QTimer, and we do the same to update the interval:
*.h
public:
Q_INVOKABLE void setSeries(QAbstractSeries *series);
Q_INVOKABLE void setInterval(int interval);
[...]
private:
QXYSeries *mSeries;
QTimer *timer;
[...]
*.cpp
void DataSource::setSeries(QAbstractSeries *series)
{
if (series) {
mSeries = static_cast<QXYSeries *>(series);
}
}
Then we remove the update argument and use mSeries:
DataSource::DataSource(QQuickView *appViewer, QObject *parent) :
QObject(parent),
m_appViewer(appViewer),
m_index(-1)
{
[...]
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &DataSource::update);
timer->start(1 / 60 * 1000 );
}
void DataSource::update()
{
if (mSeries) {
m_index++;
if (m_index > m_data.count() - 1)
m_index = 0;
QVector<QPointF> points = m_data.at(m_index);
// Use replace instead of clear + append, it's optimized for performance
mSeries->replace(points);
}
}
void DataSource::setInterval(int interval)
{
if(timer){
if(timer->isActive())
timer->stop();
timer->start(interval);
}
}
*.qml
Component.onCompleted: {
dataSource.setSeries(chartView.series(0));
if (!series("signal 1").useOpenGL) {
openGLSupported = false
openGL = false
}
}
[...]
function changeRefreshRate(rate) {
dataSource.setInterval(1 / Number(rate) * 1000);
//refreshTimer.interval = 1 / Number(rate) * 1000;
}
You can find the complete example in the following link.
I have a simple QTableView with a QSortFilterProxyModel and a source model of a custom TableModel subclass that inherits from QAbstractTableModel. The model is dynamically updated with additional rows.
My problem is this: If I sort the table on a column, then scroll to a specific row, and then more rows are added above this row it pushes the row down. Data is coming in fast enough that it makes it difficult to click on rows to edit them without the row changing underneath my cursor.
Is there a way to stop the table from scrolling and maintain the position of the table relative to say a selected row?
QTableView::rowViewportPosition() can be used to get the current view port position which has to be corrected if something is inserted before current index.
It can be retrieved before and after insertion of a row using signal handlers.
Thus, the scrolling can be adjusted accordingly in the signal handler after the insertion. This is done changing the value of the vertical scrollbar. (The vertical scroll mode is changed to QTableView::ScrollPerPixel to ensure correct vertical adjustment.)
A minimal code sample:
#include <iostream>
#include <QApplication>
#include <QMainWindow>
#include <QScrollBar>
#include <QStandardItemModel>
#include <QTableView>
#include <QTimer>
enum { NCols = 2 }; // number of columns
enum { Interval = 1000 }; // interval of auto action
enum { NRep = 5 }; // how often selected auto action is repeated
// fills a table model with sample data
void populate(QStandardItemModel &tblModel, bool prepend)
{
int row = tblModel.rowCount();
if (prepend) tblModel.insertRow(0);
for (int col = 0; col < NCols; ++col) {
QStandardItem *pItem = new QStandardItem(QString("row %0, col %1").arg(row).arg(col));
tblModel.setItem(prepend ? 0 : row, col, pItem);
}
}
// does some auto action
void timeout(QTimer &timer, QStandardItemModel &tblModel)
{
static int step = 0;
++step;
std::cout << "step: " << step << std::endl;
switch (step / NRep % 3) {
case 0: break; // pause
case 1: populate(tblModel, false); break; // append
case 2: populate(tblModel, true); break; // prepend
}
}
// managing the non-scrolling when something is inserted.
struct NoScrCtxt {
QTableView &tblView;
int y;
NoScrCtxt(QTableView &tblView_): tblView(tblView_) { }
void rowsAboutToBeInserted()
{
y = tblView.rowViewportPosition(tblView.currentIndex().row());
}
void rowsInserted()
{
int yNew = tblView.rowViewportPosition(tblView.currentIndex().row());
if (y != yNew) {
if (QScrollBar *pScrBar = tblView.verticalScrollBar()) {
pScrBar->setValue(pScrBar->value() + yNew - y);
}
}
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
// build some GUI
QMainWindow win;
QStandardItemModel tblModel(0, NCols);
for (int i = 0; i < 10; ++i) populate(tblModel, false);
QTableView tblView;
tblView.setVerticalScrollMode(QTableView::ScrollPerPixel);
tblView.setModel(&tblModel);
win.setCentralWidget(&tblView);
win.show();
// setup a "no-scroll manager"
NoScrCtxt ctxt(tblView);
QObject::connect(&tblModel, &QStandardItemModel::rowsAboutToBeInserted,
[&ctxt](const QModelIndex&, int, int) { ctxt.rowsAboutToBeInserted(); });
QObject::connect(&tblModel, &QStandardItemModel::rowsInserted,
[&ctxt](const QModelIndex&, int, int) { ctxt.rowsInserted(); });
// initiate some auto action
QTimer timer;
timer.setInterval(Interval); // ms
QObject::connect(&timer, &QTimer::timeout,
[&timer, &tblModel]() { timeout(timer, tblModel); });
timer.start();
// exec. application
return app.exec();
}
I compiled and tested this in Windows 10, VS2013, Qt 5.7:
Hey I have tried several times to complete this using usleep or the qt sleep when showing an image but sometimes (almost every time) it shows up white instead of the image basically i want it to accept any input to say im ready cin will do, then show a random image numbered 1-28 in a random time ranging 1.5-3secs then show it for 250milisecs then hide and wait 2secs the show the picture for 3secs then repeat. I am on debian, g++;
thanks in advance.
int getRandInt(int x){
return rand() % x;
}
class I : public QThread
{
public:
static void sleep(unsigned long secs) {
QThread::msleep(secs);
}
};
QApplication app(argc, argv);
std::ostringstream oss;
oss << "images/" << getRandInt(28) << ".jpg";
std::cout << oss.str();
QString qstr = QString::fromStdString(oss.str());
QPixmap pixmap(qstr);
QPixmap pixmap2(qstr);
QSplashScreen splash(pixmap);
QSplashScreen splash2(pixmap2);
QMainWindow mainWin;
while(1==1){
splash.show();
splash.showMessage(QObject::tr("test"),
Qt::AlignLeft | Qt::AlignTop, Qt::black);
app.processEvents();
I::sleep(250);
splash.finish(0);
splash.raise();
I::sleep(2*1000);
splash2.show();
splash2.showMessage(QObject::tr("test"),Qt::AlignLeft | Qt::AlignTop, Qt::black);
app.processEvents(); I::sleep(5000);splash2.finish(&mainWin);splash2.raise();
}
With an understanding that "show a random image numbered 1-28 in a random time ranging 1.5-3secs then show it for 250milisecs" means "show a random image numbered 1-28 in a random time between 1.75s and 3.25s", the below does it.
The code requires Qt 5.x and a C++11 compiler. I tried to highlight proper use of C++11 pseudorandom number facilities. I don't know the end-use of your code, but if you substituted std::mt19937_64 for random_engine, it'd be good enough for running psychological research as far as randomness goes.
The images shown are randomly drawn from a set of 28 images. Each of the source images are also randomly generated.
Notice the conspicuous absence of any form of sleep or busy-looping, and zealous use of lambdas. This code wouldn't be any shorter were it written in Python. That's what C++11 buys you: conciseness, among other things :)
#include <QApplication>
#include <QStateMachine>
#include <QTimer>
#include <QStackedWidget>
#include <QPainter>
#include <QLabel>
#include <QPushButton>
#include <random>
typedef std::default_random_engine random_engine;
QImage randomImage(int size, random_engine & rng) {
QImage img(size, size, QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::white);
QPainter p(&img);
p.setRenderHint(QPainter::Antialiasing);
int N = std::uniform_int_distribution<>(25, 200)(rng);
std::uniform_real_distribution<> dP(0, size);
std::uniform_int_distribution<> dC(0, 255);
QPointF pt1(dP(rng), dP(rng));
for (int i = 0; i < N; ++i) {
QColor c(dC(rng), dC(rng), dC(rng));
p.setPen(QPen(c, 3));
QPointF pt2(dP(rng), dP(rng));
p.drawLine(pt1, pt2);
pt1 = pt2;
}
return img;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::random_device rd;
random_engine gen(rd());
int imageSize = 300;
QList<QImage> images;
for (int n = 0; n < 28; ++n) images << randomImage(imageSize, gen);
std::uniform_int_distribution<> dImage(0, images.size()-1);
QStackedWidget display;
QPushButton ready("I'm Ready!");
QLabel label, labelHidden;
display.addWidget(&ready);
display.addWidget(&label);
display.addWidget(&labelHidden);
QTimer splashTimer;
QStateMachine machine;
QState s1(&machine), s2(&machine), s3(&machine), s4(&machine);
splashTimer.setSingleShot(true);
QObject::connect(&s1, &QState::entered, [&]{
display.setCurrentWidget(&ready);
ready.setDefault(true);
ready.setFocus();
});
s1.addTransition(&ready, "clicked()", &s2);
QObject::connect(&s2, &QState::entered, [&]{
label.setPixmap(QPixmap::fromImage(images.at(dImage(gen))));
display.setCurrentWidget(&label);
splashTimer.start(250 + std::uniform_int_distribution<>(1500, 3000)(gen));
});
s2.addTransition(&splashTimer, "timeout()", &s3);
QObject::connect(&s3, &QState::entered, [&]{
display.setCurrentWidget(&labelHidden);
splashTimer.start(2000);
});
s3.addTransition(&splashTimer, "timeout()", &s4);
QObject::connect(&s4, &QState::entered, [&]{
display.setCurrentWidget(&label);
splashTimer.start(3000);
});
s4.addTransition(&splashTimer, "timeout()", &s1);
machine.setInitialState(&s1);
machine.start();
display.show();
return a.exec();
}
This is my first question here and I believe that I havent seen anyone else asking for this specific problem but what I'm currently dealing with is a qwt plot that doesn't want to replot.
What I want to do: Call the replot() method to clear my current plot.
My problem: When calling replot() does not clear my current plot and my plots are drawn on top of eachother.
Here is a link to an image of my problem
As can be seen in the image, the new curves are drawn on top of the existing ones and this is what I want to solve.
Here is some of my code: (let me know if I missed some parts)
plot.cpp
#include "plot.h"
#include <qwt_plot.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_directpainter.h>
#include <qwt_curve_fitter.h>
#include <qwt_painter.h>
class CurveData: public QwtArraySeriesData<QPointF>
{
public:
CurveData()
{
}
virtual QRectF boundingRect() const
{
if ( d_boundingRect.width() < 0.0 )
d_boundingRect = qwtBoundingRect( *this );
return d_boundingRect;
}
inline void append( const QPointF &point )
{
d_samples += point;
}
void clear()
{
d_samples.clear();
d_samples.squeeze();
d_boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
}
};
Plot::Plot( QWidget *parent ):
QwtPlot( parent )
{
d_directPainter = new QwtPlotDirectPainter(this);
setAutoReplot(false);
setAxisScale(QwtPlot::yLeft, 0.014,0.016);
setAxisScale(QwtPlot::xBottom, 0, 1000);
d_curve = new QwtPlotCurve();
d_curve->setData(new CurveData());
d_curve->attach(this);
}
void Plot::AppendPoint(const QPointF &point)
{
CurveData *data = static_cast<CurveData *>(d_curve->data());
data->append(point);
}
void Plot::DrawCurveSegment()
{
CurveData *data = static_cast<CurveData *>(d_curve->data());
d_directPainter->drawSeries(d_curve, data->size()-11, data->size()-1);
}
void Plot::ClearPlot()
{
CurveData *data = static_cast<CurveData *>(d_curve->data());
data->clear();
QwtPlot::replot();
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent):
QWidget( parent )
{
d_plot = new Plot();
counter = 0;
loopCounter = 0;
// ... //
}
void MainWindow::timerEvent(QTimerEvent *) {
if (counter>0 && counter%1000==0)
{
d_plot->ClearPlot();
d_plot->replot();
loopCounter++;
qDebug()<<"clear!";
}
for (int ii=0; ii<10;ii++)
{
double y = someArray[counter];
double x = (double)counter-((double)loopCounter*1000);
counter++;
d_plot->AppendPoint(QPointF(x,y));
}
d_plot->DrawCurveSegment();
}
Would very much appreciate if someone can see what I'm doing wrong.
Best regards!
I solved the problem by changing the clearPlot method to the following:
void Plot::ClearPlot()
{
CurveData *data = static_cast<CurveData *>(d_curve->data());
data->clear();
QwtPlot::replot();
QwtPlot::repaint();
}
You can also do this:
QwtPlot::detachItems(QwtPlotItem::Rtti_PlotItem, true);