PDA

View Full Version : tooltip labelAlignment



Surfman19
30th June 2015, 21:32
Hi,

i use a QwtPlotMarker to visualize the data value of a qwtplotcurve. when i move the mouse over the datapoint the value will be visualized in the QwtPlotMarker.

When i move the mouse into the corners the QwtPlotMarker gets displayed out of widget view:
http://s13.postimg.org/bq6vsrd9j/IMG_6968.jpg

how can i implement some sort of auto setLabelAlignment?

Thanks,
Surf

Uwe
1st July 2015, 08:55
Why using a marker, when implementing a tooltip ?
Instead you could use QToolTip itself - or maybe easier a customized QwtPlotPicker with a trackerText method, that returns the value of a datapoint or QString::null else.

Uwe

Surfman19
1st July 2015, 15:18
i mean QwtPlotMarker. the label for QwtPlotMarker should re-adjust automatically in case if i move the mouse over a datapointer beeing at the outer corner...
do you know what i try to achieve?

here is the snipped i have at the moment:



std::shared_ptr<QwtPlot> plot;
std::shared_ptr<QwtPlotCurve> curve1;
std::shared_ptr<QwtPlotCurve> curve2;
std::shared_ptr<QwtPlotMarker> marker;
std::shared_ptr<QwtPlotShapeItem> plotItem;

///////////////////

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *m = static_cast<QMouseEvent*>(event);

QwtPlotItemList curves = plot.get()->itemList(QwtPlotItem::Rtti_PlotCurve);
if (curves.size() == 0) {
return false;
}

double dist1 = 1e99;
double dist2 = 1e99;

int point_index1 = static_cast<QwtPlotCurve*>(curves[0])->closestPoint(QPoint(m->x(), m->y()), &dist1);
int point_index2 = static_cast<QwtPlotCurve*>(curves[1])->closestPoint(QPoint(m->x(), m->y()), &dist2);

if (point_index1 != -1 && point_index2 != -1) {
QPointF p;

if (dist1 < dist2) {
p = static_cast<QwtPlotCurve*>(curves[0])->sample(point_index1);
}
else {
p = static_cast<QwtPlotCurve*>(curves[1])->sample(point_index2);
}

//marker->setSpacing(10);
marker->setLineStyle(QwtPlotMarker::Cross);
marker->setLinePen(QPen(Qt::black, 2, Qt::DotLine));
marker->setLabel("t: " + QString::number(p.x()) + ", Volts: " + QString::number(p.y()));
marker->setLabelAlignment(Qt::AlignRight | Qt::AlignTop); // | Qt::AlignCenter);
//marker->setLabelAlignment(Qt::AlignCenter | Qt::AlignCenter);
marker->setXValue(p.x());
marker->setYValue(p.y());
}
marker->attach(plot.get());
plot->replot();
}

return false;
}

Uwe
1st July 2015, 16:13
i mean QwtPlotMarker. the label for QwtPlotMarker should re-adjust automatically in case if i move the mouse over a datapointer beeing at the outer corner...
do you know what i try to achieve?
Guess yes, you want to use QwtPlotPicker - even if not knowing it yet :-/

Uwe

Surfman19
1st July 2015, 19:04
why cant i use QwtPlotMarker?

do you have a small QwtPlotPicker example? and how do you auto - align the label to avoid the issue i have?

how can you set the label of the QwtPlotPicker, i look for somth like: void setTrackerText(const QString &str);
the label should be e.g.: "Amplitude: 1.0, Sample: 10" ... the closest distance to a point of those 2 curves, as in my sample before.

i see QwtPlotPicker has a move member function: void QwtPlotPicker::move(const QPoint &pos)

... how do you know the distance to transform? this will depend on the size of the label, basically the number of characters...

Uwe
3rd July 2015, 12:26
Have a look at the curvetracker example - not exactly your use case, but you should get the idea.

Uwe