Results 1 to 5 of 5

Thread: QwtPlotPicker capturing both MousePress and MouseRelease events

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. class Picker: public QwtPlotPicker
    2. {
    3. public:
    4. Picker( QWidget *canvas ):
    5. QwtPlotPicker( canvas )
    6. {
    7. setRubberBandPen( QColor( Qt::darkGreen ) );
    8. setTrackerMode( QwtPlotPicker::AlwaysOn );
    9. }
    10.  
    11. signals:
    12. void signalMousePress();
    13.  
    14. protected:
    15. void widgetMousePressEvent(QMouseEvent *ev)
    16. {
    17. emit signalMousePress();
    18. }
    19.  
    20. QwtText trackerTextF( const QPointF &pos ) const
    21. {
    22. QwtText text( "$" + QString::number(pos.y(),'f',2) + "\n" +
    23. QwtDate::toString( QwtDate::toDateTime( pos.x() ), "dd/MM hh:mm", QwtDate::FirstThursday ) );
    24. text.setColor( Qt::white );
    25.  
    26. QColor color = Qt::gray;
    27. text.setBorderPen(QPen(color));
    28. text.setBorderRadius(2);
    29. color.setAlpha( 170 );
    30. text.setBackgroundBrush(Qt::darkYellow);
    31.  
    32. return text;
    33. }
    34. };
    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:

    Qt Code:
    1. class MousePressAndReleaseMachine: public QwtPickerMachine
    2. {
    3. public:
    4. MousePressAndReleaseMachine():
    5. QwtPickerMachine( PointSelection )
    6. {
    7. }
    8.  
    9. virtual QList<Command> transition(const QwtEventPattern &, const QEvent *event )
    10. {
    11. QList<QwtPickerMachine::Command> cmdList;
    12.  
    13. if ( event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)
    14. {
    15. cmdList += Begin;
    16. cmdList += Append;
    17. cmdList += End;
    18. }
    19.  
    20. return cmdList;
    21. }
    22. };
    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:

    Qt Code:
    1. void GraphWindow::slotPointSelected(const QPointF& pos)
    2. {
    3. static bool firstSignal = true;
    4. static bool press = true;
    5.  
    6. if (firstSignal)
    7. {
    8. firstSignal = false;
    9. return;
    10. }
    11.  
    12. if (press)
    13. {
    14. qDebug() << "Press";
    15. press = false;
    16. }
    17. else
    18. {
    19. qDebug() << "Release";
    20. press = true;
    21. }
    22. }
    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
    Last edited by Momergil; 5th October 2013 at 16:26.

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlotPicker capturing both MousePress and MouseRelease events

    Add the Q_OBJECT macro to your class definition.

    Uwe

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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.
    Last edited by Momergil; 5th October 2013 at 19:07.

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlotPicker capturing both MousePress and MouseRelease events

    Quote Originally Posted by Momergil View Post
    Tried that already and it didn't work :T
    It works - of course you have to do a qmake(!) + make then.

    Uwe

  5. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlotPicker capturing both MousePress and MouseRelease events

    Quote Originally Posted by Uwe View Post
    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
    Last edited by Momergil; 5th October 2013 at 20:30.

Similar Threads

  1. Capturing Touch Screen events in MainWindow
    By lightydo in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2013, 16:23
  2. Ignoring keyboard events in QwtPlotPicker
    By frankiefrank in forum Qwt
    Replies: 4
    Last Post: 23rd January 2012, 12:45
  3. Replies: 0
    Last Post: 3rd March 2009, 16:38
  4. Can Qt4 capture mousePress events on the desktop?
    By simula in forum Qt Programming
    Replies: 5
    Last Post: 6th January 2009, 20:10
  5. MousePress Events on ContextMenu items
    By Naveen in forum Qt Programming
    Replies: 1
    Last Post: 22nd February 2006, 08:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.