2013-05-07 68 views
1

我有一個包含一些曲線的QwtPlot,我希望能夠從這些曲線中獲取選定的點(和曲線指針):通過單擊並選擇一個點通過拖動矩形選擇點。QwtPlot如何選擇點曲線

我使用下面的代碼:

// Picker with click point machine to provide point selection 
QwtPlotPicker* clickPicker = new QwtPlotPicker(this->canvas()); 
clickPicker->setStateMachine(new QwtPickerClickPointMachine); 
clickPicker->setMousePattern(0,Qt::LeftButton,Qt::SHIFT); 
connect(clickPicker, SIGNAL(appended(QPointF)), 
     this, SLOT(pointSelected(QPointF))); 

// Picker with drag rect machine to provide multiple points selection 
QwtPlotPicker* rectPicker = new QwtPlotPicker(
      this->xBottom, this->yLeft, QwtPicker::RectRubberBand, 
      QwtPicker::AlwaysOff, this->canvas()); 
QwtPickerDragRectMachine* test = new QwtPickerDragRectMachine(); 
test->setState(QwtPickerMachine::RectSelection); 
rectPicker->setStateMachine(test); 
connect(rectPicker, SIGNAL(selected(QRectF)), 
     this, SLOT(pointsSelected(QRectF))); 

但pointSelected槽被調用每一次我在QwtPlot點擊,而不是隻在一個曲線

BTW的時候,我也嘗試將槽連接到信號QwtPlotPicker::selected(const QVector &pa)但它從未發射...

回答

1

我認爲使用CanvasPicker更方便,因爲它帶有示例,並且可以輕鬆擴展。

請查看Qwt附帶的event_filter。您應該使用類CanvasPicker(它不是Qwt API的一部分,但您會在示例中找到代碼)。 可以使用

picker = new CanvasPicker(plot); // plot is a pointer to your instance of QwtPlot 

你會看到該事件過濾器安裝在的CanvasPicker構造函數初始化它在你的類。

現在看看CanvasPicker :: eventFilter(QObject * object,QEvent * e),它在事件循環QwtPlot中發生事件時調用。在switch結構f.i中實現您的應用程序邏輯。更改case QEvent::MouseMove: