Results 1 to 6 of 6

Thread: How do i select a QwtPlotMarker using a QPlotPicker ?

  1. #1
    Join Date
    Feb 2009
    Posts
    8
    Thanks
    1

    Default How do i select a QwtPlotMarker using a QPlotPicker ?

    Greetings,

    I am using QwtPlotPicker to track the mouse and get selection events... but how do i translate that to actually selecting a marker ?

    The brute force way (below) in the select signal...doesnt really work because the Markers return a bounding rect that has a 0 width/height...

    Whats the best way to do this ?

    Qt Code:
    1. void PitchSightPlot::cursorSelected(const QwtDoublePoint &Point)
    2. {
    3. List = itemList();
    4. Iter = List.begin();
    5.  
    6. while(Iter != List.end())
    7. {
    8. QwtPlotItem *pItem = (QwtPlotItem *)(*Iter);
    9.  
    10. if(pItem->rtti() == QwtPlotItem::Rtti_PlotMarker)
    11. if(pItem->boundingRect().contains(Point))
    12. qDebug()<< "MATCH" << Point;
    13.  
    14. Iter++;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How do i select a QwtPlotMarker using a QPlotPicker ?

    Use pixel coordinates instead of plot coordinates and add some pixels around the translated marker position.

    Uwe

  3. #3
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do i select a QwtPlotMarker using a QPlotPicker ?

    Hello,

    I have got the same problem, because of size 0 of the boundingRect() the item cannot be found:

    Qt Code:
    1. void Plot::cursorSelected(const QwtDoublePoint &Point)
    2. {
    3. //selectedPoint = NULL;
    4. std::cout << "in Plot::cursorSelected, selectedPoint = " << selectedPoint << std::endl;
    5. const QwtPlotItemList& List = itemList();
    6. QwtPlotItemIterator Iter = List.begin();
    7. for ( QwtPlotItemIterator it = List.begin();it != List.end(); ++it )
    8. {
    9. std::cout << "(*it) "<< (*it) << std::endl;
    10. if ( (*it)->rtti() == QwtPlotItem::Rtti_PlotMarker )
    11. {
    12. QwtPlotMarker *mark = ((QwtPlotMarker*)(*it));
    13. QwtDoubleRect rec = mark->boundingRect();
    14. std::cout << "mark->boundingRect()" <<rec.width()<<rec.height()<< std::endl;
    15. if(mark->boundingRect().contains(Point)){
    16. std::cout << "MATCH" << std::endl;
    17. selectedPoint = dynamic_cast<QwtPlotMarker*>(mark);
    18. std::cout << "selectedPoint = " << selectedPoint << std::endl;
    19. }
    20. else selectedPoint = NULL;
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    What do you mean by using Pixel coordinates? Di you mean I have to transform them like that? :
    Qt Code:
    1. double x = this->transform(mark->xAxis(),Point.x());
    2. double y = this->transform(mark->yAxis(),Point.y());
    3. QwtDoublePoint trPoint = QwtDoublePoint(x,y);
    To copy to clipboard, switch view to plain text mode 

    I also uses this, but it does not work too. Perhaps because I did not expansed the size of boundingRect. How can I do it?

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

    Default Re: How do i select a QwtPlotMarker using a QPlotPicker ?

    If you want to decide if a mouse click matches a point on a widgets you have to think ( and implement ) in widgets coordinates:

    1) Translate the position of your marker to widget coordinates.
    2) Create a rectangle around the mouse click position ( adding a tolerance of a couple of pixels )
    3) Check if the marker position ( or the rectangle in symbol size around it) is inside the rectangle.

    If you have many points you might have to introduce some spatial index (like a quadtree) to speed up your implemetation.

    HTH,
    Uwe

  5. #5
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do i select a QwtPlotMarker using a QPlotPicker ?

    Thank you, Uwe!

    I followed your advice and managed to translate the coordinates of items into Plot coordinates and can now select the corresponding items as well as drag them on the plot. There is only one little bad behaviour (perhaps it depends on my bad c++ code ), namely that if I release the mouse the pointer to the dragged items is not set to NULL and thus if I don't select any other items, but drag just the empty area, the last selected or dragged item is moved there. How can I set the pointer to this item to NULL after the MouseButtonRelease event is called?

    Here is the code for those, who had similar problem:

    Qt Code:
    1. ...
    2. // create QwtPlotPicker to allow user moving the points on the plot
    3.  
    4. picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
    5. QwtPicker::PointSelection | QwtPicker::DragSelection,
    6. QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
    7. this->canvas());
    8.  
    9. connect( picker, SIGNAL(moved(const QwtDoublePoint& ) ),
    10. this, SLOT(cursorMoved( const QwtDoublePoint& ) ) );
    11.  
    12. connect( picker, SIGNAL(selected( const QwtDoublePoint& ) ),
    13. this, SLOT(cursorSelected( const QwtDoublePoint& ) ) );
    14. ...
    15.  
    16. void Plot::cursorSelected(const QwtDoublePoint &Point)
    17. {
    18. //selectedPoint = NULL;
    19. std::cout << "in Plot::cursorSelected, selectedPoint = " << selectedPoint << std::endl;
    20. const QwtPlotItemList& List = itemList();
    21. QwtPlotItemIterator Iter = List.begin();
    22.  
    23. while(Iter != List.end())
    24. {
    25. if((*Iter)->rtti() == QwtPlotItem::Rtti_PlotMarker){
    26. QwtPlotMarker *mark = ((QwtPlotMarker*)(*Iter));
    27. double x = this->transform(mark->xAxis(),mark->xValue());
    28. double y = this->transform(mark->yAxis(),mark->yValue());
    29. std::cout << "mark.xValue()=" << mark->xValue() << ", mark.yValue()=" << mark->yValue()<< std::endl;
    30. std::cout << "transformed mark coord = " << x << ", " << y<< std::endl;
    31. QwtDoublePoint trPoint = QwtDoublePoint(x,y);
    32. double xClick = this->transform(mark->xAxis(),Point.x());
    33. double yClick = this->transform(mark->yAxis(),Point.y());
    34. QwtDoubleRect recOfClick(xClick-10, yClick-10, 20,20);
    35. std::cout << "recOfClick(x,y,width, height) =" <<recOfClick.x()<<", " << recOfClick.y() << ", " << recOfClick.width()<< ", "<<recOfClick.height()<< std::endl;
    36. if(recOfClick.contains(trPoint)){
    37. std::cout << "MATCH" << std::endl;
    38. selectedPoint = mark;
    39. std::cout << "selectedPoint = " << selectedPoint << std::endl;
    40. }
    41. /* else{
    42.   selectedPoint = NULL; // at the beginning this pointer should be NULL
    43.   }*/
    44. }
    45. Iter++;
    46. }
    47. }
    48. //##################################################################
    49. void Plot::cursorMoved( const QwtDoublePoint& Point){
    50. std::cout << "in Plot::cursorMoved, selectedPoint = " << selectedPoint << std::endl;
    51. std::cout << "Point.x()= " << Point.x() <<"Point.y()= " << Point.y() << std::endl;
    52. if(selectedPoint!=NULL){
    53. selectedPoint->setValue(Point); // set the new coordinates to the moved item
    54. //std::cout << "selectedPoint->xValue(): " << selectedPoint->xValue() <<"selectedPoint->yValue(): " << selectedPoint->yValue()<< std::endl;
    55. this->replot(); // replot the plot widget after the selected point was moved
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: How do i select a QwtPlotMarker using a QPlotPicker ?

    I expected that the QwtPlotPicker can handle both the PointSelection and RectSeletion modes, so that if I click one time on an item then the SIGNAL selected(const QwtDoublePoint &pos) is called and if drag a rectangle around several items then the SIGNAL selected(const QwtDoubleRect &rect) would be called. Obviosly my expectations are not right, because I can only use one of this two selection modes. How shall be the QwtPlotPicker parameterized to fulfill my expectation? The following code does not do the right thing:

    Qt Code:
    1. picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
    2. QwtPicker::PointSelection | QwtPicker::RectSelection | QwtPicker::DragSelection,
    3. QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOn,
    4. this->canvas());
    5.  
    6. // connect the signals coming from QwtPlotPicker with the member functions
    7. connect( picker, SIGNAL(moved(const QwtDoublePoint& ) ),
    8. this, SLOT(cursorMoved( const QwtDoublePoint& ) ) );
    9.  
    10. connect( picker, SIGNAL(selected( const QwtDoublePoint& ) ),
    11. this, SLOT(cursorSelected( const QwtDoublePoint& ) ) );
    12.  
    13. connect( picker, SIGNAL(selected (const QwtDoubleRect &) ),
    14. this, SLOT(RectangleSelected( const QwtDoubleRect& ) ) );
    To copy to clipboard, switch view to plain text mode 

    Or is here another logical problem? Help me please.

    Thank you

Similar Threads

  1. Resize caught by select()
    By mhoover in forum Qt Programming
    Replies: 3
    Last Post: 17th July 2009, 04:04
  2. Select Region from image
    By yazwas in forum Newbie
    Replies: 4
    Last Post: 15th July 2009, 17:41
  3. can't select item in a QTreeWidget via code
    By zack in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2009, 20:31
  4. QTableWidget row select
    By nowire75 in forum Newbie
    Replies: 4
    Last Post: 23rd December 2007, 18:59

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.