Dear all,
iltommi and flemming put me on the track to find a solution. I try to describe it in detail, so it should be possible to follow. The created application possesses a crosshair cursor that follows the mouse motion. The crosshair cursor color can be changed by mouse clicks.
Note, that I am new to Qt and QCustomplot, so this is not a solution created by an expert.
I appreciate comments!
I created a project, where I followed the hints given in http://www.qcustomplot.com/index.php/tutorials/settingup, but instead of promoting class QCustomPlot to the widget I promoted class CustomPlotWithFullWidthFullHeightCrosshairCursorClass, which is derived from QCustomPlot.
In the following I give a step by step description, but since my QT-Creator user interface is German some terms may differ, i.e. they are translated by me.
1. Create a Qt-Widgets Application in a certain directory
2. Copy qcustomplot.h and qcustomplot.cpp to that directory
3. Add two files to the project CustomPlotWithFullWidthFullHeightCrosshairCursorClass.h and CustomPlotWithFullWidthFullHeightCrosshairCursorClass.cpp
Here is CustomPlotWithFullWidthFullHeightCrosshairCursorClass.h
#ifndef CUSTOMPLOTWITHFULLWIDTHFULLHEIGHTCROSSHAIRCURSORCLASS_H
#define CUSTOMPLOTWITHFULLWIDTHFULLHEIGHTCROSSHAIRCURSORCLASS_H
#include "qcustomplot.h"
#include <QMainWindow>
namespace Ui
{
class MainWindow;
}
class CustomPlotWithFullWidthFullHeightCrosshairCursorClass : public QCustomPlot
{
Q_OBJECT
public:
CustomPlotWithFullWidthFullHeightCrosshairCursorClass( QWidget* ); // Constructor
void setMousePosition(double, double); // Initial Position
void mouseMoveEvent(QMouseEvent *event); // Reimplement the protected function inherited from QWidget
void mousePressEvent(QMouseEvent *event); // Reimplement the protected function inherited from QWidget
private:
QPointer<QCPItemStraightLine> mouseMarkerX;
QPointer<QCPItemStraightLine> mouseMarkerY;
QPen crosshairpen;
};
#endif // CUSTOMPLOTWITHFULLWIDTHFULLHEIGHTCROSSHAIRCURSORCLASS_H
And here is CustomPlotWithFullWidthFullHeightCrosshairCursorClass.cpp
#include<QtGui>
#include"CustomPlotWithFullWidthFullHeightCrosshairCursorClass.h"
CustomPlotWithFullWidthFullHeightCrosshairCursorClass::CustomPlotWithFullWidthFullHeightCrosshairCursorClass(QWidget *parent):QCustomPlot(parent)
{
setMousePosition(0.3, 0.3); // initial position
// note the absence of a connect - the mouse events are handled by overwriting ...
// ... the functionality inherited from QWidgets
}
void CustomPlotWithFullWidthFullHeightCrosshairCursorClass::setMousePosition(double positionX, double positionY)
{
/* put the crosshair cursor on the initial position */
if(!mouseMarkerX) mouseMarkerX = new QCPItemStraightLine(this);
if(!mouseMarkerY) mouseMarkerY = new QCPItemStraightLine(this);
// initial color is yellow
crosshairpen.setColor(Qt::yellow);
mouseMarkerX->setPen(crosshairpen);
mouseMarkerY->setPen(crosshairpen);
if(mouseMarkerX && mouseMarkerY)
{
mouseMarkerX->point1->setCoords(positionX, 0);
mouseMarkerX->point2->setCoords(positionX, 1);
mouseMarkerY->point1->setCoords(0, positionY);
mouseMarkerY->point2->setCoords(1, positionY);
}
replot();
}
void CustomPlotWithFullWidthFullHeightCrosshairCursorClass::mouseMoveEvent(QMouseEvent *event)
{
/* let the crosshair cursor follow the mouse position */
if(!mouseMarkerX) mouseMarkerX = new QCPItemStraightLine(this);
if(!mouseMarkerY) mouseMarkerY = new QCPItemStraightLine(this);
double positionX = xAxis->pixelToCoord( event->pos().x() );
double positionY = yAxis->pixelToCoord( event->pos().y() );
if(mouseMarkerX && mouseMarkerY)
{
mouseMarkerX->point1->setCoords(positionX, 0);
mouseMarkerX->point2->setCoords(positionX, 1);
mouseMarkerY->point1->setCoords(0, positionY);
mouseMarkerY->point2->setCoords(1, positionY);
}
replot();
}
void CustomPlotWithFullWidthFullHeightCrosshairCursorClass::mousePressEvent(QMouseEvent *event)
{
/* change the crosshair cursor color with the mouse buttons */
Qt::MouseButton button = event->button();
switch (button)
{
case Qt::LeftButton: // left click blue
crosshairpen.setColor(Qt::blue);
break;
case Qt::RightButton:
crosshairpen.setColor(Qt::red); // right click red
break;
default:
crosshairpen.setColor(Qt::green); // middle button or whatever green
break;
}
mouseMarkerX->setPen(crosshairpen);
mouseMarkerY->setPen(crosshairpen);
}
4. Use QT Creator to create the user interface as shown in http://www.qcustomplot.com/index.php/tutorials/settingup but use CustomPlotWithFullWidthFullHeightCrosshairCursorClass as Promoted class name.
5. Select the widget and set the cursor property to empty (here I am not sure what is presented in the English version. Maybe it is none or something like that. We simply say that there will be no cursor when hovering with the mouse over the widget.)
6. Select the widget and check the mouse tracking property.
7. Create and Execute
Hope this example is useful. Please comment.
Once again thanks a lot to all who participated.
Cheers Reinhard