Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Changing alignment of a QChart axis

Changing alignment of a QChart axis

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 364 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mnbv
    wrote on last edited by
    #1

    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:

    1. 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.
    2. Create a list of all series the existing axis is attached to (necessary if you have a multi-axis chart).
    3. Remove the existing axis from the chart.
    4. Add the new axis to the chart.
    5. 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 const and is a constructor parameter, but, who knows...

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved