我目前正在使用Qts Chart繪圖工具。我現在有一個情節,我可以通過使用由this示例(小調整)提供的chartview類來放大和縮小。 我希望看到不僅可以縮放的能力,還可以將我的視圖按下鼠標中鍵(這在其他應用程序中使用很多,因此非常直觀)。qt圖表按住鼠標中鍵移動視圖
我該如何在Qt中做到這一點?如何檢查鼠標中鍵是否被按下並釋放,如果鼠標在按下鼠標中鍵時移動,則更改我的視圖...
我確定有人編寫過此代碼,一個小例子/幫助。
我目前正在使用Qts Chart繪圖工具。我現在有一個情節,我可以通過使用由this示例(小調整)提供的chartview類來放大和縮小。 我希望看到不僅可以縮放的能力,還可以將我的視圖按下鼠標中鍵(這在其他應用程序中使用很多,因此非常直觀)。qt圖表按住鼠標中鍵移動視圖
我該如何在Qt中做到這一點?如何檢查鼠標中鍵是否被按下並釋放,如果鼠標在按下鼠標中鍵時移動,則更改我的視圖...
我確定有人編寫過此代碼,一個小例子/幫助。
你需要從QChartView
派生類和重載鼠標事件:
class ChartView: public QChartView
{
Q_OBJECT
public:
ChartView(Chart* chart, QWidget *parent = 0);
protected:
virtual void mousePressEvent(QMouseEvent *event) override;
virtual void mouseMoveEvent(QMouseEvent *event) override;
private:
QPointF m_lastMousePos;
};
ChartView::ChartView(Chart* chart, QWidget *parent)
: QChartView(chart, parent)
{
setDragMode(QGraphicsView::NoDrag);
this->setMouseTracking(true);
}
void ChartView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton)
{
QApplication::setOverrideCursor(QCursor(Qt::SizeAllCursor));
m_lastMousePos = event->pos();
event->accept();
}
QChartView::mousePressEvent(event);
}
void ChartView::mouseMoveEvent(QMouseEvent *event)
{
// pan the chart with a middle mouse drag
if (event->buttons() & Qt::MiddleButton)
{
QRectF bounds = QRectF(0,0,0,0);
for(auto series : this->chart()->series())
bounds.united(series->bounds())
auto dPos = this->chart()->mapToValue(event->pos()) - this->chart()->mapToValue(m_lastMousePos);
if (this->rubberBand() == QChartView::RectangleRubberBand)
this->chart()->zoom(bounds.translated(-dPos.x(), -dPos.y()));
else if (this->rubberBand() == QChartView::HorizontalRubberBand)
this->chart()->zoom(bounds.translated(-dPos.x(), 0));
else if (this->rubberBand() == QChartView::VerticalRubberBand)
this->chart()->zoom(bounds.translated(0, -dPos.y()));
m_lastMousePos = event->pos();
event->accept();
}
QChartView::mouseMoveEvent(event);
}
什麼是m_zoomRect? – user7431005
這是一個更大的項目(https://github.com/nholthaus/chart)的一部分,它也擴展了圖表並處理了縮放。我編輯了答案,但在這種情況下,「m_zoomRect」是所有圖表系列邊界的聯合。 –
可能重複的:https://stackoverflow.com/questions/18551466/qt-proper-method-to-implement- panningdrag/ –