QCustomPlot Discussion and Comments

ItemText not visibleReturn to overview


I have created a widgetPlot-Window, in which I can plot up to 4 plots. This number is variable via a SpinBox and I create the respective number of AxisRect. In each AxisRect can be a number of Graphs, each graph for one measurement. I. e. if I want to show 5 different measurements, I will have 5 graphs in each AxisRect.

Now I want to be able to click on one graph and to show a text above the graph.
I have managed to do it successfully only for the first AxisRect. If I select a graph in another AxisRect (not the first) the graph gets selected, but the ItemText is not visible.

My Code of the selectionChanged()-Method:

QList<QCPGraph*> selGraphList = customPlot->selectedGraphs();
    bool foundrange;
    if (!selGraphList.isEmpty()){
        foreach(QCPGraph* currGraph, selGraphList){
            QCPAxisRect* currAxisRect = qobject_cast<QCPAxisRect*>(currGraph->parentLayerable());
            QCPRange leftRange = currAxisRect->axis(QCPAxis::atLeft)->range();
            QCPRange bottRange = currAxisRect->axis(QCPAxis::atBottom)->range();
            QCPRange keyRange = currGraph->getKeyRange(foundrange, QCP::sdBoth);
            QCPRange valRange = currGraph->getValueRange(foundrange, QCP::sdBoth, keyRange);
            QCPItemText *testText = new QCPItemText(customPlot);
            testText->setFont(QFont("Helvetica", 14, QFont::Bold));
            double xLow = keyRange.lower;
            double xUps = keyRange.upper;
            double yLow = valRange.lower;
            double yUps = valRange.upper;
            testText->position->setCoords(xLow + (xUps - xLow)/2, yLow + (yUps - yLow)/2);

The function RemoveTextSelectionChanged() just loops through all items and removes them.

In the end, I check which Graph is selected and clip an ItemText to the AxisRect the selected graph is in. The AxisRect is correctly chosen, the position is correct, but only the text is not visible.

I appreciate any help.


If you want to specify the position in plot coordinates, you must specify which axes you want to use for that. Normally it's set to the xAxis and yAxis I think.
testText->position->setAxes(plot->axisRect(1)->axes(QCPAxis::atLeft), ...)

Thanks, your comment helped.

I added the two following lines into the foreach loop, now it works:

testText->position->setAxes(currAxisRect->axis(QCPAxis::atBottom), currAxisRect->axis(QCPAxis::atLeft));