Hi
I created a small QCP widget for testing where I want to add graphs either to yAxis or yAxis2 and have a legend below the plot area for each axis. Based on the layout system example it was rather easy to create a layout containing the two legends below the plot area, however, the newly created QCPLegend is formated differently that the default legend. Also the new legend does not accept any changes to borderPen etc.
In the following picture you can find the generate widget. Both legends at the bottom are displayed and also updated with the QTimer::singleshot, but the right one (m_secLegend) is missing the border and does not accept any brush or pen settings:
https://drive.proton.me/urls/4ZTZRCRCEC#5oUYmYx5MaPS
debugCustomPlot::debugCustomPlot(QWidget *parent)
: QWidget{parent}
{
m_plot = new QCustomPlot();
m_plot->legend->setVisible(true);
// Create the secondary legend
m_secLegend = new QCPLegend();
m_secLegend->setVisible(true);
m_secLegend->setLayer("legend");
QCPLayoutGrid *subLayout = new QCPLayoutGrid;
m_plot->plotLayout()->addElement(1, 0, subLayout);
subLayout->setMargins(QMargins(5, 0, 5, 5));
subLayout->setColumnSpacing(50);
subLayout->addElement(0, 0, m_plot->legend);
subLayout->addElement(0, 1, m_secLegend);
// change the fill order of the legend, so it's filled left to right in columns:
m_plot->legend->setFillOrder(QCPLegend::foColumnsFirst);
m_secLegend->setFillOrder(QCPLegend::foColumnsFirst);
// set legend's row stretch factor very small so it ends up with minimum height:
m_plot->plotLayout()->setRowStretchFactor(1, 0.001);
// add graphs to left axis / left legend
m_plot->addGraph()->setName("Torque");
m_plot->addGraph()->setName("Power");
m_plot->addGraph()->setName("Efficiency");
// add graph to second axis / right legend
QCPGraph *gRight = m_plot->addGraph(m_plot->xAxis, m_plot->yAxis2);
gRight->setName("Secondary Axis Curve");
gRight->addToLegend(m_secLegend); // add graph to right (custom) legend
gRight->removeFromLegend(); // removes default from m_plot->legend
QTimer::singleShot(5000, this, &debugCustomPlot::addGraphY1);
QTimer::singleShot(8000, this, &debugCustomPlot::addGraphY2);
// Create widget layout
QVBoxLayout* vlay = new QVBoxLayout();
vlay->addWidget(m_plot);
setLayout(vlay);
}
void debugCustomPlot::addGraphY1() {
m_plot->addGraph()->setName("Power");
m_plot->addGraph()->setName("Efficiency");
m_plot->replot();
}
void debugCustomPlot::addGraphY2() {
// add graph to second axis / right legend
QCPGraph *gRight = m_plot->addGraph(m_plot->xAxis, m_plot->yAxis2);
gRight->setName("Secondary Axis Curve 2");
gRight->addToLegend(m_secLegend); // add graph to right (custom) legend
gRight->removeFromLegend(); // removes default from m_plot->legend
m_plot->replot();
}
Thanks in advance and best regards
Daniel
Nevermind, found the issue:
setLayer only works if the legend is already placed into the Layout, which then set the m_parentPlot variable and the QCPLegend can be moved to the layer.
For future reference, this code works fine:
debugCustomPlot::debugCustomPlot(QWidget *parent)
: QWidget{parent}
{
// Create plot widgets
m_plot = new QCustomPlot();
m_secLegend = new QCPLegend();
// create layout below plot area
QCPLayoutGrid *subLayout = new QCPLayoutGrid;
m_plot->plotLayout()->addElement(1, 0, subLayout);
subLayout->setMargins(QMargins(5, 0, 5, 5));
subLayout->setColumnSpacing(50);
subLayout->addElement(0, 0, m_plot->legend);
subLayout->addElement(0, 1, m_secLegend);
// style legends:
m_plot->legend->setVisible(true);
m_plot->legend->setFillOrder(QCPLegend::foColumnsFirst);
m_plot->legend->setBorderPen(QPen(Qt::black, 1));
m_plot->legend->setBrush(QBrush(QColor(255,255,255,150)));
m_secLegend->setVisible(true);
m_secLegend->setLayer(m_plot->legend->layer());
m_secLegend->setFillOrder(QCPLegend::foColumnsFirst);
m_secLegend->setBorderPen(QPen(Qt::black, 1));
m_secLegend->setBrush(QBrush(QColor(255,255,255,150)));
// set legend's row stretch factor very small so it ends up with minimum height:
m_plot->plotLayout()->setRowStretchFactor(1, 0.001);