Hello!
I want to implement a QwtPlotPicker capable of telling me when the user did a mouse press event and a mouse release event on a QwtPlot. I noticed that by using the machine QwtPickerDragPointMachine, the signal selected(QPointF) would only report the release event, so since I'm using a derivated class from QwtPlotPicker, I thought it would only be a matter of sending a signal each time the reimplemented widgetMousePressEvent(QMouseEvent*) was triggered:
{
public:
{
setRubberBandPen
( QColor( Qt
::darkGreen ) );
}
signals:
void signalMousePress();
protected:
{
emit signalMousePress();
}
{
QwtDate::toString( QwtDate::toDateTime( pos.x() ), "dd/MM hh:mm", QwtDate::FirstThursday ) );
text.setColor( Qt::white );
text.
setBorderPen(QPen(color
));
text.setBorderRadius(2);
color.setAlpha( 170 );
text.setBackgroundBrush(Qt::darkYellow);
return text;
}
};
class Picker: public QwtPlotPicker
{
public:
Picker( QWidget *canvas ):
QwtPlotPicker( canvas )
{
setRubberBandPen( QColor( Qt::darkGreen ) );
setTrackerMode( QwtPlotPicker::AlwaysOn );
}
signals:
void signalMousePress();
protected:
void widgetMousePressEvent(QMouseEvent *ev)
{
emit signalMousePress();
}
QwtText trackerTextF( const QPointF &pos ) const
{
QwtText text( "$" + QString::number(pos.y(),'f',2) + "\n" +
QwtDate::toString( QwtDate::toDateTime( pos.x() ), "dd/MM hh:mm", QwtDate::FirstThursday ) );
text.setColor( Qt::white );
QColor color = Qt::gray;
text.setBorderPen(QPen(color));
text.setBorderRadius(2);
color.setAlpha( 170 );
text.setBackgroundBrush(Qt::darkYellow);
return text;
}
};
To copy to clipboard, switch view to plain text mode
The problem is that while the code is compilible, when I add a Picker in my code the compler soon begins to report a "undefined reference to Picker::signalMousePress() / error: ld returned 1 exist status". That's very strange in my opinion, since the code seems to be fine; I even created a test class derived from QObject using a similar idea and nothing happend of problem, but it was just a matter of taking out the QObject references in that test class and putting QwtPlotPicker (and QWidget in the appropriate places) and the problem reappered.
I was giving up hope when I found this post: http://www.qtcentre.org/threads/5523...=QwtPlotPicker, talking about the idea of creating a machine to do the job. So I managed to create the following one:
{
public:
MousePressAndReleaseMachine():
{
}
{
QList<QwtPickerMachine::Command> cmdList;
if ( event
->type
() == QEvent::MouseButtonPress || event
->type
() == QEvent::MouseButtonRelease) {
cmdList += Begin;
cmdList += Append;
cmdList += End;
}
return cmdList;
}
};
class MousePressAndReleaseMachine: public QwtPickerMachine
{
public:
MousePressAndReleaseMachine():
QwtPickerMachine( PointSelection )
{
}
virtual QList<Command> transition(const QwtEventPattern &, const QEvent *event )
{
QList<QwtPickerMachine::Command> cmdList;
if ( event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)
{
cmdList += Begin;
cmdList += Append;
cmdList += End;
}
return cmdList;
}
};
To copy to clipboard, switch view to plain text mode
Now the compilation runs fine and the capture of both mouse press and mouse release are occuring, but with a considerable bug: in the first time I press the mouse in the QwtPlot, instead of only one selected(QPointF) signal being emitted (related to the mouse press event), TWO are emitted. Using a static bool variable to test for identification on when a press is done and when a release is done, I always receive both signals in the first press event in the qDebug() usage.
So essentially what is going on? First, why I can't emit the signal in my Picker class without having a undefined reference compiler problem and why I receive two signals instead of just one in the first mouse press when I use my picker machine?
And also: supose someone knows the answer to both of my question; in this case, which approach should I use? (specially regarding memory/time consumption).
Thanks,
Momergil
Note edit: it's obvious that the second problem could be overcome by using another static bool variable like this:
void GraphWindow::slotPointSelected(const QPointF& pos)
{
static bool firstSignal = true;
static bool press = true;
if (firstSignal)
{
firstSignal = false;
return;
}
if (press)
{
qDebug() << "Press";
press = false;
}
else
{
qDebug() << "Release";
press = true;
}
}
void GraphWindow::slotPointSelected(const QPointF& pos)
{
static bool firstSignal = true;
static bool press = true;
if (firstSignal)
{
firstSignal = false;
return;
}
if (press)
{
qDebug() << "Press";
press = false;
}
else
{
qDebug() << "Release";
press = true;
}
}
To copy to clipboard, switch view to plain text mode
I try that and it worked. But let's be reasonable: it should't be working just this way! ^^ I'll use this only as temporary solution, but I really prefer the actual corretion of the problem
Bookmarks