gpt4 book ai didi

c++ - 我在 QChartView 中找不到缩放图形的鼠标滚轮滚动事件

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:41 26 4
gpt4 key购买 nike

我在程序窗口上有 QChartView。有数据数组在图表上正确显示为 QLineSeries(温度与时间曲线)。我在 QChartView 上找不到“mousewheelup zoom-in”和“mousewheeldown zoom-out”的鼠标滚轮事件?需要能够仅在垂直方向进行缩放,例如 setRubberBand(QChartView::VerticalRubberBand) 但只能通过鼠标滚轮滚动。需要帮助

最佳答案

QChartView 是一个 QWidget,因此您可以使用 wheelEvent() 实现该逻辑方法:

#include <QApplication>
#include <QChartView>
#include <QLineSeries>
#include <random>

QT_CHARTS_USE_NAMESPACE

class ChartView : public QChartView
{
public:
using QChartView::QChartView;
enum DirectionZoom{
NotZoom,
VerticalZoom,
HorizontalZoom,
BothDirectionZoom = VerticalZoom | HorizontalZoom
};
DirectionZoom directionZoom() const{
return mDirectionZoom;
}
void setDirectionZoom(const DirectionZoom &directionZoom){
mDirectionZoom = directionZoom;
}

protected:
void wheelEvent(QWheelEvent *event)
{
if(chart() && mDirectionZoom != NotZoom){
const qreal factor = 1.001;
QRectF r = chart()->plotArea();
QPointF c = r.center();
qreal val = std::pow(factor, event->delta());
if(mDirectionZoom & VerticalZoom)
r.setHeight(r.height()*val);
if (mDirectionZoom & HorizontalZoom) {
r.setWidth(r.width()*val);
}
r.moveCenter(c);
chart()->zoomIn(r);
}
QChartView::wheelEvent(event);
}
private:
DirectionZoom mDirectionZoom = NotZoom;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QChart *chart = new QChart();
chart->legend()->hide();
chart->setTitle("Simple line chart example");
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> uni(0, 10);

for(size_t i=0; i< 5; i++){
QLineSeries *series = new QLineSeries();
for(size_t j=0; j < 10; j++){
*series << QPointF(j, uni(rng));
}
chart->addSeries(series);
}
chart->createDefaultAxes();

ChartView chartView(chart);
chartView.setDirectionZoom(ChartView::VerticalZoom);
chartView.setRenderHint(QPainter::Antialiasing);
chartView.resize(640, 480);
chartView.show();

return a.exec();
}

关于c++ - 我在 QChartView 中找不到缩放图形的鼠标滚轮滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51753500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com