Getting a `No alignment specified !` when setting alignment Qt::AlignCenter when adding axis using addAxis()
-
Hi there,
I have the following snippet of code which initializes 2 QCharts
void InitializeCharts() { // pan panPositionChart = new QChart(); panPositionChart->setTitle("Pan Position"); QValueAxis *panAxisX = new QValueAxis(); panAxisX->setTickCount(5); panPositionChart->addAxis(panAxisX, Qt::AlignCenter); QValueAxis *panAxisY = new QValueAxis(); panAxisY->setTickCount(5); panPositionChart->addAxis(panAxisY, Qt::AlignCenter); ui->gimbalPositionPanChartview->setChart(panPositionChart); ui->gimbalPositionPanChartview->setRenderHint(QPainter::Antialiasing); // tilt tiltPositionChart = new QChart(); tiltPositionChart->setTitle("Tilt Position"); QValueAxis *tiltAxisX = new QValueAxis(); tiltAxisX->setTickCount(5); tiltPositionChart->addAxis(tiltAxisX, Qt::AlignCenter); QValueAxis *tiltAxisY = new QValueAxis(); tiltAxisY->setTickCount(5); tiltPositionChart->addAxis(tiltAxisY, Qt::AlignLeft); ui->gimbalPositionTiltChartview->setChart(tiltPositionChart); ui->gimbalPositionTiltChartview->setRenderHint(QPainter::Antialiasing); }
For both of the
QChart
I want the X axis to be centered aligned. I want the Y axis to be center aligned for one of the charts and left aligned for the other. The above code is based on the QtCharts multiaxis example https://doc.qt.io/qt-6.2/qtcharts-multiaxis-example.html. When I run the above code, I get the following errorNo alignment specified ! Segmentation fault (core dumped)
If I use the
Qt::AlignBottom
as it is in the example, I get no error.
If I use theQt::AlignHCenter
orQt::AlignVCenter
, I get theNo alignment specified !
error.What am I doing wrong? Do I need to add something else?
FYI I use the Qt 6.2.3 and for the ChartView I have compiled the designer plugin from the git repo https://code.qt.io/cgit/qt/qtcharts.git/tree/?h=6.2.3 and add it in the designers plugin directory.
Next following how I want the chart's axis to be like and possibly there is another way to achieve this
Pan chart
Tilt chart
Let me know if you need any more information
Kind regards,
Stavros
-
Hi @J-Hilk,
thanks for your reply and your suggestions. I was looking the
Qwt
myself yesterday and I used it, as you suggested, to create the charts as I wanted. Next following the functionInitializeCharts()
at my first comment but with theQwt
module/library now#include <QwtPlot> #include <QwtPlotScaleItem> void InitializeCharts() { // pan ui->gimbalPositionPanQwtPlot->setTitle("Pan Position"); ui->gimbalPositionPanQwtPlot->enableAxis(QwtAxis::YLeft, false); ui->gimbalPositionPanQwtPlot->enableAxis(QwtAxis::XBottom, false); ui->gimbalPositionPanQwtPlot->enableAxis(QwtAxis::YLeft, false); ui->gimbalPositionPanQwtPlot->enableAxis(QwtAxis::XBottom, false); ui->gimbalPositionPanQwtPlot->setAxisScale(QwtAxis::YLeft, -90.0, 90.0); ui->gimbalPositionPanQwtPlot->setAxisScale(QwtAxis::XBottom, -90.0, 90.0); QwtPlotScaleItem *panVerticalAxisScale = new QwtPlotScaleItem(QwtScaleDraw::RightScale, 0.0); panVerticalAxisScale->attach(ui->gimbalPositionPanQwtPlot); QwtPlotScaleItem *panHorizontalAxisScale = new QwtPlotScaleItem(QwtScaleDraw::BottomScale, 0.0); panHorizontalAxisScale->attach(ui->gimbalPositionPanQwtPlot); // tilt ui->gimbalPositionTiltQwtPlot->setTitle("Tilt Position"); ui->gimbalPositionTiltQwtPlot->enableAxis(QwtAxis::YLeft, false); ui->gimbalPositionTiltQwtPlot->enableAxis(QwtAxis::XBottom, false); ui->gimbalPositionTiltQwtPlot->enableAxis(QwtAxis::YLeft, false); ui->gimbalPositionTiltQwtPlot->enableAxis(QwtAxis::XBottom, false); ui->gimbalPositionTiltQwtPlot->setAxisScale(QwtAxis::YLeft, -90.0, 90.0); ui->gimbalPositionTiltQwtPlot->setAxisScale(QwtAxis::XBottom, 0.0, 90.0); QwtPlotScaleItem *tiltVerticalAxisScale = new QwtPlotScaleItem(QwtScaleDraw::RightScale, 0.0); tiltVerticalAxisScale->attach(ui->gimbalPositionTiltQwtPlot); QwtPlotScaleItem *tiltHorizontalAxisScale = new QwtPlotScaleItem(QwtScaleDraw::BottomScale, 0.0); tiltHorizontalAxisScale->attach(ui->gimbalPositionTiltQwtPlot); }
The output would look like the following
which is close enough for me of what I wanted to achieve.
Thanks again @J-Hilk for your time replying to my issue.
Kind regards,
Stavros
-
thats because QCharts only accepts a limited amount of alignments:
void QAbstractAxisPrivate::setAlignment(Qt::Alignment alignment) { switch (alignment) { case Qt::AlignTop: case Qt::AlignBottom: m_orientation = Qt::Horizontal; break; case Qt::AlignLeft: case Qt::AlignRight: m_orientation = Qt::Vertical; break; default: qWarning("No alignment specified !"); break; } m_alignment = alignment; }
https://github.com/qt/qtcharts/blob/dev/src/charts/axis/qabstractaxis.cpp
-
Great thanks for your prompt reply and this info. So it seems that the alignment in the chart axis is actually orientation.
So I suppose there is no way to move the axis as I want using the QtCharts, is this correct?
-
@Stavros-Vaionitis said in Getting a `No alignment specified !` when setting alignment Qt::AlignCenter when adding axis using addAxis():
So I suppose there is no way to move the axis as I want using the QtCharts, is this correct?
not that I know of, but I'm by no means an expert in QtCharts 😅
going by this SO thread,
Qwt
seems to have this featurePersonally I wouldn't use QCharts for anything half way complex/custom. It is nice and easy when you want a graph for some data you have. But it's far from perfekt, and to my knowledge hasn't been actively worked on in years.
-
Hi @J-Hilk,
thanks for your reply and your suggestions. I was looking the
Qwt
myself yesterday and I used it, as you suggested, to create the charts as I wanted. Next following the functionInitializeCharts()
at my first comment but with theQwt
module/library now#include <QwtPlot> #include <QwtPlotScaleItem> void InitializeCharts() { // pan ui->gimbalPositionPanQwtPlot->setTitle("Pan Position"); ui->gimbalPositionPanQwtPlot->enableAxis(QwtAxis::YLeft, false); ui->gimbalPositionPanQwtPlot->enableAxis(QwtAxis::XBottom, false); ui->gimbalPositionPanQwtPlot->enableAxis(QwtAxis::YLeft, false); ui->gimbalPositionPanQwtPlot->enableAxis(QwtAxis::XBottom, false); ui->gimbalPositionPanQwtPlot->setAxisScale(QwtAxis::YLeft, -90.0, 90.0); ui->gimbalPositionPanQwtPlot->setAxisScale(QwtAxis::XBottom, -90.0, 90.0); QwtPlotScaleItem *panVerticalAxisScale = new QwtPlotScaleItem(QwtScaleDraw::RightScale, 0.0); panVerticalAxisScale->attach(ui->gimbalPositionPanQwtPlot); QwtPlotScaleItem *panHorizontalAxisScale = new QwtPlotScaleItem(QwtScaleDraw::BottomScale, 0.0); panHorizontalAxisScale->attach(ui->gimbalPositionPanQwtPlot); // tilt ui->gimbalPositionTiltQwtPlot->setTitle("Tilt Position"); ui->gimbalPositionTiltQwtPlot->enableAxis(QwtAxis::YLeft, false); ui->gimbalPositionTiltQwtPlot->enableAxis(QwtAxis::XBottom, false); ui->gimbalPositionTiltQwtPlot->enableAxis(QwtAxis::YLeft, false); ui->gimbalPositionTiltQwtPlot->enableAxis(QwtAxis::XBottom, false); ui->gimbalPositionTiltQwtPlot->setAxisScale(QwtAxis::YLeft, -90.0, 90.0); ui->gimbalPositionTiltQwtPlot->setAxisScale(QwtAxis::XBottom, 0.0, 90.0); QwtPlotScaleItem *tiltVerticalAxisScale = new QwtPlotScaleItem(QwtScaleDraw::RightScale, 0.0); tiltVerticalAxisScale->attach(ui->gimbalPositionTiltQwtPlot); QwtPlotScaleItem *tiltHorizontalAxisScale = new QwtPlotScaleItem(QwtScaleDraw::BottomScale, 0.0); tiltHorizontalAxisScale->attach(ui->gimbalPositionTiltQwtPlot); }
The output would look like the following
which is close enough for me of what I wanted to achieve.
Thanks again @J-Hilk for your time replying to my issue.
Kind regards,
Stavros