QCustomPlot Discussion and Comments

Controlling size of axisRectReturn to overview

Like other posters, I can make no sense at all of how to precisely scale the size of axes in pixels. In my case, I need to do this to align the plot precisely with scales seismograms from another window. Below is a code snippet where I have attempted to set both minimum and maximum sizes of the axisRect. I added in the margins based on an earlier comment that, despite the documentation, the axisRect is not the inner rect, but includes margins. It doesn't work either with or without margins. And in both cases, when I query the width of the rect, I get a nonsensical small number like 29 or 55. Any ideas?

    spectrogramPlot_ = new QCustomPlot(this);
    spectrogramPlot_->setInteractions(0);
    spectrogramPlot_->axisRect()->setupFullAxesBox(true);
    spectrogramPlot_->xAxis->setLabel("time");
    spectrogramPlot_->yAxis->setLabel("freq");
    QCPColorMap* colorMap = new QCPColorMap(spectrogramPlot_->xAxis, spectrogramPlot_->yAxis);
    axisRect = spectrogramPlot_->axisRect(0);
    axisRect->setMaximumSize(trace_->selectionRect().width() + axisRect->margins().left()+ axisRect->margins().right(), 256);
    axisRect->setMinimumSize(trace_->selectionRect().width() + axisRect->margins().left()+ axisRect->margins().right(), 256);
    std::cerr << trace_->selectionRect().width() << "\n";
    std::cerr << spectrogramPlot_->axisRect(0)->width() << "\n";
...
    spectrogramPlot_->replot();
    spectrogramPlot_->update();

Qt doesn't assign width/height to a widget until it went through the event loop and the associated parent window is created. Did you take that into account?

If you have the above code e.g. in the mainwindow constructor, it won't give you the expected results, since the mainwindow itself doesn't have a well-defined geometry. That's why you get nonsensical values.

If the parent window does have a defined geometry before calling this code, you should call a replot before querying the layout system (width/height of axis rect). Because QCP defers arranging/scaling the layout objects to when it is actually needed (right before replot), and not every time one parameter changes.

Solved my problem by manually controlling some margins.

Glenn