首页 > 技术知识 > 正文

法一:

customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignLeft|Qt::AlignTop);

说明:

0 — 表示 legend 图层索引

1 — 表示 legend 位置,可选择如下情况;

Qt::AlignLeft|Qt::AlignTop); //图例置于左上

Qt::AlignCenter|Qt::AlignTop);//图例置于中上

Qt::AlignRight|Qt::AlignTop);//图例置于右上

Qt::AlignLeft|Qt::AlignCenter);//图例置于左中

Qt::AlignCenter); //图例置于正中

Qt::AlignRight|Qt::AlignCenter);//图例置于右中

Qt::AlignLeft|Qt::AlignBottom);//图例置于左下

Qt::AlignCenter|Qt::AlignBottom);//图例置于中下

法二:

customPlot->plotLayout()->insertRow(2); QMargins marg(150,0,150,0); customPlot->legend->addElement(0,1,customPlot->legend->item(1)); customPlot->legend->addElement(0,2,customPlot->legend->item(2)); customPlot->legend->addElement(0,3,customPlot->legend->item(3)); customPlot->plotLayout()->addElement(2, 0, customPlot->legend); customPlot->plotLayout()->setRowStretchFactor(2, 0.00001); customPlot->legend->setMargins(marg);

法三:

customPlot->plotLayout()->setMargins(QMargins(0, 0, 0, 30)); // 首先为底部预留出足够的空间 customPlot->axisRect()->insetLayout()->setInsetPlacement(0, QCPLayoutInset::ipFree); // 设置以任意位置放置图例,以对齐边界的方式不行 customPlot->axisRect()->insetLayout()->setInsetRect(0, QRectF(0.0, 1.2, 1.0, 0.1)); // 将y方向的位置设置为超过1以便它的位置超过QCPAxisRect的位置

如图所示:

QCustomPlot 更改图例legend位置

猜你喜欢