Results 1 to 2 of 2

Thread: displaying proper x-coordinates of mouse move

  1. #1
    Join Date
    Dec 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default displaying proper x-coordinates of mouse move

    Hello all,

    I started to build application based on stockchart example. Basing on CanvasPicker class I would like to handle mouse moves, forward them by emitting signal to main class and there display selected coordinates and draw lines from it on the chart. It is simple idea of technical analysis above the bar chart. My problem is coordinates are not properly displayed and lets say if I select point and move mouse once on 1.03(x-scale is qwtDate) on the x-scale and then change application window size (it is f.e. maximize) and after that once again move mouse on 1.03 -my cooridnates will be different.
    Qt Code:
    1. CanvasPicker::CanvasPicker(QwtPlot *plot):
    2. QObject(plot),
    3. d_selectedCurve(NULL),
    4. d_selectedPoint(-1)
    5. {
    6. plot->setMouseTracking(true);
    7. QwtPlotCanvas *canvas = qobject_cast<QwtPlotCanvas*>(plot->canvas());
    8. canvas->installEventFilter(this);
    9.  
    10. canvas->setFocusPolicy(Qt::StrongFocus);
    11. canvas->setFocusIndicator(QwtPlotCanvas::ItemFocusIndicator);
    12. canvas->setFocus();
    13.  
    14. shiftCurveCursor(true);
    15. }
    16.  
    17. bool CanvasPicker::event(QEvent *e)
    18. {
    19. if ( e->type() == QEvent::User )
    20. {
    21. showCursor(true);
    22. return true;
    23. }
    24. return QObject::event(e);
    25. }
    26.  
    27. bool CanvasPicker::eventFilter(QObject *object, QEvent *e)
    28. {
    29. if ( object != (QObject *)plot()->canvas() )
    30. return false;
    31.  
    32. switch(e->type())
    33. {
    34. case QEvent::FocusIn:
    35. showCursor(true);
    36. case QEvent::FocusOut:
    37. showCursor(false);
    38.  
    39. case QEvent::Paint:
    40. {
    41. QApplication::postEvent(this, new QEvent(QEvent::User));
    42. break;
    43. }
    44. case QEvent::MouseButtonPress:
    45. {
    46. select(((QMouseEvent *)e)->pos());
    47. return true;
    48. }
    49. case QEvent::MouseMove:
    50. {
    51. move(((QMouseEvent *)e)->pos());
    52. return true;
    53. }
    54. default:
    55. break;
    56. }
    57. return QObject::eventFilter(object, e);
    58. }
    59.  
    60. void CanvasPicker::select(const QPoint &pos)
    61. {
    62. emit createCurve();
    63. TAcurveDataX[0]=pos.x();
    64. TAcurveDataY[0]=pos.y();
    65. }
    66.  
    67. void CanvasPicker::move(const QPoint &pos)
    68. {
    69. TAcurveDataX[1]=pos.x();
    70. TAcurveDataY[1]=pos.y();
    71. emit emitCurve(TAcurveDataX[0],TAcurveDataY[0],TAcurveDataX[1],TAcurveDataY[1]);
    72.  
    73. }
    To copy to clipboard, switch view to plain text mode 

    Above is CanvasPicker class, simple idea is to handle mouse click and mouse move to create 2 points -
    {TAcurveDataX[0],TAcurveDataY[0]},
    {TAcurveDataX[1],TAcurveDataY[1]}.

    These points are forwarding into main class.
    After create QwtPlotCurve and connect with appropriate slots like below:
    Qt Code:
    1. d_plot[chartUseNumber] = new Plot( this );
    2. CanvasPicker *pick=new CanvasPicker(d_plot[chartUseNumber]);
    3.  
    4. connect(pick,SIGNAL(createCurve()),this,SLOT(CreateNewCanvasCurve()));
    5. connect(pick,SIGNAL(emitCurve(double,double,double,double)),this,SLOT(ReceiveCurveData(double,double,double,double)));
    To copy to clipboard, switch view to plain text mode 

    Creating plot:
    Qt Code:
    1. Plot::Plot( QWidget *parent ):
    2. QwtPlot( parent )
    3. {
    4. this->setMouseTracking(true);
    5. this->curve=NULL;
    6. QwtDateScaleDraw *scaleDraw = new DateScaleDraw( Qt::UTC );
    7. QwtDateScaleEngine *scaleEngine = new QwtDateScaleEngine( Qt::UTC );
    8.  
    9. //setAxisTitle( QwtPlot::xBottom, QString( "2010" ) );
    10. setAxisScaleDraw( QwtPlot::xBottom, scaleDraw );
    11. setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
    12. setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
    13. setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
    14.  
    15. Legend *legend = new Legend;
    16. //insertLegend( legend, QwtPlot::RightLegend );
    17.  
    18. this->global_sample_count=255;
    19. this->current_instr=0;
    20.  
    21. QwtPlotPanner *panner = new QwtPlotPanner( canvas() );
    22. panner->setMouseButton( Qt::MidButton );
    23.  
    24. connect( legend, SIGNAL( checked( QwtPlotItem *, bool, int ) ),
    25. SLOT( showItem( QwtPlotItem *, bool ) ) );
    26. }
    27.  
    28. void Plot::populate2(int instrument,int start,int end,int mode)
    29. {
    30.  
    31. GridItem *gridItem = new GridItem();
    32. gridItem->attach( this );
    33.  
    34. const Qt::GlobalColor colors[] =
    35. {
    36. Qt::red,
    37. Qt::blue,
    38. Qt::darkCyan,
    39. Qt::darkMagenta,
    40. Qt::darkYellow
    41. };
    42.  
    43. if(curve!=NULL)
    44. {
    45. delete curve;
    46. }
    47.  
    48. const int numColors = sizeof( colors ) / sizeof( colors[0] );
    49. if(instrument!=0)
    50. {
    51. QuoteFactory::Stock stock = static_cast<QuoteFactory::Stock>( instrument-1 );
    52. curve = new QwtPlotTradingCurve();
    53. curve->setTitle( QuoteFactory::title( stock ) );
    54. curve->setOrientation( Qt::Vertical );
    55. curve->setSamples( QuoteFactory::samples_fn( stock,start,end,mode ) );
    56. curve->setSymbolExtent( 12 * 3600 * 1000.0 );
    57. curve->setMinSymbolWidth( 3 );
    58. curve->setMaxSymbolWidth( 15 );
    59. const Qt::GlobalColor color = colors[ instrument % numColors ];
    60. curve->setSymbolPen( color );
    61. curve->setSymbolBrush( QwtPlotTradingCurve::Decreasing, color );
    62. curve->setSymbolBrush( QwtPlotTradingCurve::Increasing, Qt::white );
    63. curve->attach( this );
    64. showItem( curve, true );
    65. }
    66. }
    To copy to clipboard, switch view to plain text mode 

    I use it in the following way:
    Qt Code:
    1. void MainWindow::ReceiveCurveData(double x1,double y1,double x2,double y2)
    2. {
    3. QDateTime year2010( QDate( 2010, 1, 1 ), QTime( 0, 0 ), Qt::UTC );
    4. QVariant yvar;
    5. yvar=( year2010.addDays( x1 )) ;
    6. ine->setText(yvar.toString());
    7.  
    8. double arr1[2];
    9. double arr2[2]={(d_plot[0]->invTransform(currvamac->yAxis(), y1)),(d_plot[0]->invTransform(currvamac->yAxis(), y2))};
    10.  
    11. QDateTime dt1 = QwtDate::toDateTime(x1);
    12. QDateTime dt2 = QwtDate::toDateTime(x2);
    13. double arr1o=QwtDate::toDouble(dt1);
    14. double arr2o=QwtDate::toDouble(dt2);
    15. double d1=QwtDate::toDouble( year2010.addDays( x1 )) ;
    16. double d2=QwtDate::toDouble( year2010.addDays( x2 )) ;
    17. arr1[0] = d1;
    18. arr1[1] = d2;
    19.  
    20. currvamac->setSamples(arr1,arr2,2);
    21. d_plot[0]->replot();
    22. }
    To copy to clipboard, switch view to plain text mode 


    There is no problem with y-coordinates, only with x-coordinates. Thanks for help in advance.
    Last edited by chmodfx; 3rd December 2013 at 18:08.

  2. #2
    Join Date
    Dec 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: displaying proper x-coordinates of mouse move

    Ok, after digging in it I can express it more clear.

    Without QwtDateScale as scale for X-axis it works great. with normal x-axis,y-axis and invTransform() on both x points and y points everything works successful.
    But how to transform pos.x() into QwtDateScaleDraw position?




    OK, I solved it.
    It seems it is enough to use invTransform for both axes. I don't know why did it cause problems, some stupid mistakes.
    Last edited by chmodfx; 3rd December 2013 at 22:59. Reason: updated contents

Similar Threads

  1. Replies: 6
    Last Post: 2nd March 2018, 04:39
  2. Replies: 3
    Last Post: 22nd February 2013, 19:56
  3. No QGraphicsScene proper coordinates
    By harmodrew in forum Newbie
    Replies: 15
    Last Post: 16th November 2010, 12:04
  4. getting mouse coordinates
    By eric in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 19:34
  5. Move Rectangle on mouse Move
    By vermarajeev in forum Qt Programming
    Replies: 24
    Last Post: 14th May 2007, 05:34

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.