gpt4 book ai didi

qt - 如何防止 QChartView 缩放到负 x 或 y 轴值

转载 作者:行者123 更新时间:2023-12-05 03:49:34 27 4
gpt4 key购买 nike

我想防止将 QChartView 缩放到负 x 或 y 轴值。所以我试图在我的 QChartView 子类中拦截 mouseReleaseEvent,然后在执行缩放之前修改 rubberBandRect 的坐标。但似乎 rubberBandRect 坐标在鼠标释放后始终为零,所以我认为我没有使用正确的 rubberBandRect。

我正在尝试根据 QChartView 的文档进行调整(强调已添加):

void QChartView::mouseReleaseEvent(QMouseEvent *event)

If the left mouse button is released and the rubber band is enabled, the event event is accepted and the view is zoomed into the rectangle specified by the rubber band.

当我尝试在下面的代码片段中查看 rubberBandRect 中的实际值时,无论我如何使用光标缩放,矩形的 x/y 和高度/宽度值始终为零。

我也看了这个问题的答案:Qt How to connect the rubberBandChanged signal但在那种情况下,他们想要主窗口中的行为,这不是我想要的。谢谢!

class MyQChartView : public QtCharts::QChartView
{
Q_OBJECT
public:
MyQChartView(QWidget *parent = 0);
//...
protected:
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);

};


void MyQChartView::mouseReleaseEvent(QMouseEvent *event)
{

//always shows zeroes for the x and y position
if(event->button() == Qt::LeftButton){
std::cout << "x: " << this->rubberBandRect().x() << " y: " << this->rubberBandRect().y() << std::endl;

QChartView::mouseReleaseEvent(event);
}

//any other event
QChartView::mouseReleaseEvent(event);
}

最佳答案

经过广泛的实验,我认为我找到了最好的解决方法。有必要制作一个 rubberBand 指针,然后 findChild 才能找到实际的橡皮筋。然后在释放鼠标后,我得到绘图区域坐标以及橡皮筋坐标。然后我制作了一个边界框 (bounded_geometry) 并在橡皮筋超出范围时限制它的值。然后我放大到有界几何框。仅仅修改橡皮筋是行不通的,因为这些坐标不是 float ,舍入错误会导致缩放错误。

class MyQChartView : public QtCharts::QChartView
{
Q_OBJECT
public:
MyQChartView(QWidget *parent = 0):
QChartView(parent)
{
myChart = new QtCharts::QChart();
setRubberBand(QtCharts::QChartView::RectangleRubberBand);
rubberBandPtr = this->findChild<QRubberBand *>();
this->setChart(myChart);
//...other things
}
//...
protected:
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
private:
QRubberBand * rubberBandPtr;
QtCharts::QChart * myChart;
};


void MyQChartView::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
//first get the plot area and save the needed values (could change each time)
QRectF plotArea = myChart->plotArea();
qreal xbound = plotArea.x(); //values < xbound are out of bounds on x axis
qreal ybound = plotArea.y() + plotArea.height(); // !!! note that values > ybound are out of bounds (i.e. negative y values)

QPointF rb_tl = rubberBandPtr->geometry().topLeft();
QPointF rb_br = rubberBandPtr->geometry().bottomRight();

QRectF bounded_geometry = rubberBandPtr->geometry();

if(rb_tl.x() < xbound){
bounded_geometry.setX(xbound);
}
if(rb_br.y() > ybound){ //note that values > ybound are out of bounds
bounded_geometry.setBottom(ybound);
}

myChart->zoomIn(bounded_geometry);
rubberBandPtr->close();

return;
}

//any other event
QChartView::mouseReleaseEvent(event);
}

关于qt - 如何防止 QChartView 缩放到负 x 或 y 轴值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63926286/

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