我正在使用Qt5.9.6,并且在更改QChart轴范围时不会更新背景。选择其他应用程序作为活动背景后,背景就会立即更新。
更改范围后
有什么主意吗?谢谢
我试图为我的应用程序创建一个具有代表性的示例,但是该示例(不幸的是)有效。
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QScatterSeries>
#include <QtCharts/QChart>
#include <QtCharts/QValueAxis>
#include <QDebug>
#include <qslider>
#include <QLayout>
#include <qwidget>
#include <QtMath>
#include <QApplication>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a {argc, argv};
struct {
const double nbr_of_per {5.5};
// number of samples
const int nos {(int)(nbr_of_per*200*M_PI)};
} sine_params;
const int sld_max {sine_params.nos-1};
const double div {100.0};
QSlider * sld {new QSlider{Qt::Horizontal}};
QChartView * view {new QtCharts::QChartView{}};
QChart * chart {new QtCharts::QChart{}};
QLineSeries sine {};
QScatterSeries point {};
sld->setRange(0, sld_max);
chart->addSeries(&sine);
chart->addSeries(&point);
// for(auto e: chart->series())
// e->setUseOpenGL(true);
for(double i{}; i< sine_params.nos;i++)
sine.append(QPointF{i/div, sin(i/div)});
point.append(QPointF{});
point.setMarkerSize(20);
chart->legend()->hide();
QValueAxis x_ax;
x_ax.setRange(0, sine.pointsVector().last().x());
x_ax.setTickCount(5);
x_ax.setLabelFormat(QString{"%."+QString::number((int)log10(div))+"f"});
chart->setAxisX(&x_ax, &sine);
chart->setAxisX(&x_ax, &point);
QValueAxis y_ax;
y_ax.setRange(-1, 1);
y_ax.setTickCount(5);
y_ax.setLabelFormat("%.3f");
chart->setAxisY(&y_ax, &sine);
chart->setAxisY(&y_ax, &point);
QFont font{chart->axisX()->labelsFont()};
font.setPixelSize(20);
chart->axisX()->setLabelsFont(font);
chart->axisY()->setLabelsFont(font);
view->setChart(chart);
view->setRenderHint(QPainter::Antialiasing);
QVBoxLayout * vl{new QVBoxLayout};
vl->addWidget(view);
vl->addWidget(sld);
QWidget w;
w.setLayout(vl);
w.show();
QObject::connect(sld, &QSlider::valueChanged,
[=,&sine,&point](int val){
QPointF p{val/div, sine.at(val).y()};
point.clear();
point.append(p);
chart->axisX()->setRange(p.x()-(100/div), p.x()+(100/div));
chart->axisY()->setRange(p.y()-(.1), p.y()+.1);
// in my misbehaving application i have to use update() and only
// then I do not get 'leftovers' after changing axis ranges
// view->scene()->update(view->rect());
}
);
return a.exec();
}
非常烦人