QwtPlotPicker capturing both MousePress and MouseRelease events
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:
Code:
{
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;
}
};
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:
Code:
{
public:
MousePressAndReleaseMachine():
{
}
{
QList<QwtPickerMachine::Command> cmdList;
if ( event
->type
() == QEvent::MouseButtonPress || event
->type
() == QEvent::MouseButtonRelease) {
cmdList += Begin;
cmdList += Append;
cmdList += End;
}
return cmdList;
}
};
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:
Code:
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;
}
}
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 :)
Re: QwtPlotPicker capturing both MousePress and MouseRelease events
Add the Q_OBJECT macro to your class definition.
Uwe
Re: QwtPlotPicker capturing both MousePress and MouseRelease events
Tried that already and it didn't work :T
(Btw just for checking I decided to do it again, and now (independente on emiting or not the signal) the compiler says "undefined reference to 'vtable for Picker'" twice and "error: ld returned 1 exist status" poiting to the line "QwtPlotPicker( canvas) ")
Btw 2: I noticed that by using the MousePressAndReleaseMachine class I created, the signal moved(QPointF) stops being emitted (just for consideration, the previous machine I was using was QwtPickerDragPointMachine), what is a problem since I also need it; this means that if I'm going to use this approach, I'ld have to learn how to activate the moved() signal again.
Re: QwtPlotPicker capturing both MousePress and MouseRelease events
Quote:
Originally Posted by
Momergil
Tried that already and it didn't work :T
It works - of course you have to do a qmake(!) + make then.
Uwe
Re: QwtPlotPicker capturing both MousePress and MouseRelease events
Quote:
Originally Posted by
Uwe
It works - of course you have to do a qmake(!) + make then.
Uwe
Well, if by "qmake(!) + make then" you mean do the process "clear all / run qmake / rebuild all", than as I sad it didn't work :P
--
Edit: Ok, I found the problem: I was declaring the class in the .cpp file; it was just a matter of moving the code to the header file and it compiled Ok (I got a flashback on the pass when the same problem happened, and I had forgot :P)
Added after 12 minutes:
And now I found myself with another problem: the implementation of void widgetMousePressEvent(QMouseEvent*) is blocking the moved and selected signals \o/
What now? :S And once again I can't figure out what could be doing this. I also tried with installing a event filter; same problem!
I'm glad for any help :)