QCustomPlot Discussion and Comments

Updating QCP Version/Legend PositionReturn to overview

I have a GUI that uses QCustomPlot, but it's based on a version from 09.06.2012. I'm trying to help update the version to the most current one just by replacing the old qcustomplot.cpp and qcustomplot.h files with the new ones and fixing whatever make errors I come across.

I'm currently stuck on the change in how elements are handled, namely a legend. I have a function where a context menu on the legend is requested. The old version that I'm trying to fix is this:

void GraphViewer::contextMenuRequest(QPoint pos)
      QMenu *menu = new QMenu(this);

      if (this->legend->selectTest(pos, true, 0) == -1.0) // context menu on legend requested
        menu->addAction("Move to top left", this, SLOT(moveLegend()))->setData((int)QCPLegend::psTopLeft);
        menu->addAction("Move to top center", this, SLOT(moveLegend()))->setData((int)QCPLegend::psTop);
        menu->addAction("Move to top right", this, SLOT(moveLegend()))->setData((int)QCPLegend::psTopRight);
        menu->addAction("Move to bottom right", this, SLOT(moveLegend()))->setData((int)QCPLegend::psBottomRight);
        menu->addAction("Move to bottom left", this, SLOT(moveLegend()))->setData((int)QCPLegend::psBottomLeft);
      } else  // general context menu on graphs requested

        //if (this->selectedGraphs().size() > 0)
          //menu->addAction("Remove selected graph", this, SLOT(removeSelectedGraph()));

        if(this->selectedGraphs().size() == 1)
            QCPGraph* g = this->selectedGraphs().first();
                menu->addAction("Hide selected graph", this, SLOT(setVisibleSelectedGraph()));
                menu->addAction("Show selected graph", this, SLOT(setVisibleSelectedGraph()));

        if (this->graphCount() > 0 && !m_mwPtr->isLogging())
          menu->addAction("Remove all graphs", this, SLOT(removeAllGraphs()));


The problem I'm having is with the chunk inside the first if, with adding the actions to a menu and the equivalent of position. Obviously it doesn't have the ps positions anymore because it's handled completely different. I was looking at using setInsetAlignment, and making sure the inset placement is ipBorderAligned (I'm not sure even sure if this is the right way to approach it) but then I'm not sure if I need to add an element to represent the legend of my plot, etc etc.

I'm basically looking to do the exact same thing as the code above (the first if statement), but with the new version of things.

If I need to provide any more information, let me know. I've only been getting into QCustomPlot for the past two and half or so weeks, so still a bit new...

Thanks in advance for any help!


Seems this code is strongly inspired by the interactions example (see full package in the download section). You should be able to fix your code by just seeing how it's done there now.

You are right with using setInsetAlignment. The actions must now carry a Qt::Alignment flag and in the moveLegend() slot, you call setInsetAlignment on the respective legend, which is probably the default primary legend (e.g. customPlot->axisRect()->insetLayout()->setInsetAlignment(0, [the new alignment flags])).