QCustomPlot Discussion and Comments

Different behavior of setMargins() under almost identical conditionsReturn to overview

Hi! I am having two plots in a splitter window. I am trying to change the margins of my plots when either of them is collapsed due to the movement of the splitter. I connected the splitterMoved() signal to my slot heightChanged().
When I do not check for any condition and just change the margins using setMargins() function inside heightChanged() the changes take effect immediately:

void mainWindow::heightChanged(int pos, int index)
{
    ui.plot->axisRect()->setMargins(QMargins(100, 100, 100, 100));
}
However, if I check for a condition such as if the splitter handle is at 0 position, the changes do not take effect immediately:
void mainWindow::heightChanged(int pos, int index)
{
    if(pos == 0)
    {
        ui.plot->axisRect()->setMargins(QMargins(100, 100, 100, 100));
    }
}
In the second case, I have to either call replot() or drag or zoom the axis (I have iRangeDrag and iRangeZoom enabled) which I know calls replot() internally. My question is why is this happening? I have checked using qDebug() and the margins are changed immediately in both cases.

Also, in both cases, the heightChanged() and setMargins()functions are called multiple times, and each time the margins are set (I checked it) but this change is only reflected immediately in the first case. I am mentioning this so as to remove any doubts if the second call to heightChanged() function is responsible for immediate change in appearance in the first case.

I do not want to call replot() multiple times by myself because I know it can make things slow especially when I am plotting multiple QVector<double> with each having its length in millions. Is there any way out of it? I am thinking of calling replot() only when the QVector<double> is changed and maybe call some other function when things like axis size are changed. Is this possible?

It appears to me you rely on the resizeEvent of QCustomPlot to do the replot. And for some reason, that last heightChanged (when pos==0) happens after the last resizeEvent. As such, the margin change is not visualized on screen.

In any case, there is no harm in calling plot->replot(QCustomPlot::rpQueuedReplot), as it will only put a replot in the event queue. If another replot is issued (e.g. in the resizeEvent), it wouldn't cause two consecutive wasteful replots.