Changing alignment of a QChart axis
-
Given an existing
QAbstractAxis, is there a way to change its alignment (Qt 5.15.2)?For example, say I have a chart and a button which, when pressed, should toggle the Y axis between the left and right side of the chart:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QLineSeries *series = new QLineSeries; series->append(0, 2); series->append(1, 0); series->append(2, 4); series->append(3, 3); QChart *chart = new QChart; chart->addSeries(series); chart->createDefaultAxes(); ui->chartView->setChart(chart); } void MainWindow::on_btnToggleYAxisSide_clicked () { QAbstractAxis *axis = ui->chartView->chart()->axisY(); setAxisSide(axis, axis->alignment() ^ (Qt::AlignLeft | Qt::AlignRight)); }Where my goal would be to implement this function:
static void setAxisSide (QAbstractAxis *axis, Qt::Alignment alignment) { // ??? }Is there some way to do that conveniently?
The only way I've been able to figure out so far doesn't meet my "change the alignment of an existing QAbstractAxis" goal, and also is just... way too much code just to change one aesthetic property:
- Create a new copy of the axis -- a non-trivial (and in some cases, impossible) task especially if support for arbitrary
QAbstractAxis-derived axes is desired. Pass the desired new alignment to the constructor. - Create a list of all series the existing axis is attached to (necessary if you have a multi-axis chart).
- Remove the existing axis from the chart.
- Add the new axis to the chart.
- Re-attach the new axis to all the series that used the old one.
It's a bit strange to me that the alignment property is
constand is a constructor parameter, but, who knows... - Create a new copy of the axis -- a non-trivial (and in some cases, impossible) task especially if support for arbitrary