PDA

View Full Version : QwtPicker with QwtHistogram



cptpingu
22nd November 2010, 10:41
Hi.

I'm using Qwt 6.0.0-rc2, with Qt4.7 on Arch Linux.

I have a QwtPlotHistogram and I want to select one of the histogram, ie if I click on a histogram an action is triggered (like showing a message box).
I see QwtPicker is made for this kind of task. I read the documentation of QwtPlotPicker, and it was clearly made for QwtPlot.
Is there a QwtPicker which works for QwtPlotHistogram ?

Is there a better solution to accomplish what I want to do ?

Many thanks.

Uwe
22nd November 2010, 12:17
A picker is basically an event filter for mouse and key events on the plot canvas combined with tracker text and rubberband to provide some visual feedback for the user interaction.

In your case it looks like you want a picker with a QwtPickerClickPointMachine. Then you can implement a slot ( connected to QwtPlotPicker::selected( const QPointF &pos ) ) where you can identify the bar from the selected position and then trigger your action.

Uwe

cptpingu
22nd November 2010, 13:11
Hi Uwe.

Thanks for the answer.

I'm a beginner in Qwt, and I don't understand what I should do. I search on Google to find example with QwtPickerClickPointMachine, but I can't find any tutorial. Moreover, the documentation is not helping me.

I have some questions:
- You want me to use QwtPickerClickPointMachine to make a bridge between QwtPlotPicker and QwtHistogram ?
- If so, how can I initialize QwtPlotPicker ? It needs a canvas, but I don't found where I can get a canvas from a QwtHistogram.
- Can I have an example of a QwtPickerClickPointMachine asociated with a QwtHistogram (I can't find one on the web) ?

Thank you.

Uwe
22nd November 2010, 14:45
QwtPlot is a composite widget, that consists of widgets for title, legend, 4 scales and the canvas. The canvas is the widget in the center, where all plot items ( f.e your histogram ) are painted to. With a picker you can select a polygon a rectangle or a position on the canvas.

So what you need to do then is to translate the selected coordinate into something that has to do with your data ( this is what you have passed to the histogram item ). F.e iterate over your samples and check if the bounding rectangle of one of your sample contains the clicked coordinate. Then you have the selected bar ( = visual representation of your sample )

Uwe

cptpingu
22nd November 2010, 15:47
It's much much clearer for me now. Thanks for the explanation.

Using the "event_filter" example I found in the Qwt source, I tried to implement the move event, but it don't works. The "selected" works fine.

Can I have some help on this piece of code ?

HistogramPlot.hh

#ifndef HISTOGRAMPLOT_HH_
# define HISTOGRAMPLOT_HH_

# include <qwt_plot.h>
# include <qwt_picker.h>
# include <qwt_plot_picker.h>

class HistogramPlot : public QwtPlot
{
Q_OBJECT
public:
HistogramPlot(QWidget* = 0);

private:
void populate();

private Q_SLOTS:
void showItem(QwtPlotItem*, bool on);
void moved(const QPoint& pos);
void selected(const QPointF& pos);

protected:
QwtScaleDiv fixedNumberScaleDiv() const;
QwtPlotPicker* picker_;
};

#endif /* !HISTOGRAMPLOT_HH_ */


HistogramPlot.cc

HistogramPlot::HistogramPlot(QWidget* parent)
: QwtPlot(parent)
{
[...]

picker_ = new QwtPlotPicker(this->canvas());
picker_->setStateMachine(new QwtPickerClickPointMachine);
connect(picker_, SIGNAL(moved(const QPoint&)),
SLOT(moved(const QPoint&)));
connect(picker_, SIGNAL(selected(const QPointF&)),
SLOT(selected(const QPointF&)));
}

void HistogramPlot::moved(const QPoint& pos)
{
QString info;
info.sprintf("Freq=%g, Ampl=%g, Phase=%g",
invTransform(QwtPlot::xBottom, pos.x()),
invTransform(QwtPlot::yLeft, pos.y()),
invTransform(QwtPlot::yRight, pos.y())
);
qDebug() << info;
}

void HistogramPlot::selected(const QPointF& pos)
{
QString info;
info.sprintf("Freq=%g, Ampl=%g, Phase=%g",
invTransform(QwtPlot::xBottom, pos.x()),
invTransform(QwtPlot::yLeft, pos.y()),
invTransform(QwtPlot::yRight, pos.y())
);
qDebug() << info;
}


Edit: I just understand that "moved" is use when you drag something, not when you move the mouse. I don't found a signal/slot which deal with mouse move. How can I implement that ?

Uwe
22nd November 2010, 16:50
Maybe QwtPickerTrackerMachine is what you are looking for.

Uwe

cptpingu
22nd November 2010, 17:05
I understand. Problem solved :)

Thanks a lot.