简体   繁体   中英

Setting minimum fixed size QCustomPlot bar graph inside QSplitter

I am using QCustomPlot to plot a bar graph. There are two such bar graphs horizontally arranged inside a QSplitter (which can be dragged). On dragging the splitter horizontally, the bar graphs resize and scale to the point that the axes labels start to overlap. I have a parent QWidget which contains a QScrollArea which ultimately contains the plot. This widget is added to the QSplitter .

QWidget* topLeftParent = new QWidget;
topLeftParent->setLayout(new QVBoxLayout);
QScrollArea *topLeftScrollArea = new QScrollArea(this);
topLeftScrollArea->setWidget(energyGraph->GetPlot());
topLeftScrollArea->setWidgetResizable(true);
topLeftScrollArea->setFrameShape(QFrame::NoFrame);
topLeftParent->layout()->addWidget(topLeftScrollArea);
m_pTopLeftHorizontalSplitter->setMinimumWidth(500);
m_pTopLeftHorizontalSplitter->addWidget(topLeftParent);

Existing Behaviour:

On dragging the splitter horizontally, the bar graphs expand/shrink horizontally till the axes tick labels overlap and ultimately at a very small value of width, the horizontal scrollbar appears, which is useless. Same is the case for resizing the window as well.

Required Behavior

However, I seek a different behavior. I want the bar graphs to be horizontally scrollable without them resizing on dragging the splitter ie dragging the splitter left/right should reveal more/less of the graphs, and not resize them

Queries

  1. How do I specify that the bar graph should have a minimum size and expand from that depending on the number of ticks on x-axis?
  2. How do I stop the plot from automatically expanding/shrinking?

What I have tried so far

I have tried setting minimum size for the plot, but it does not work. I have also tried specifying the stretch factor for the splitter to 0, but that also has no effect ie the behavior is auto resizing bar graphs in both the cases with scrollbar appearing too late.

To achieve the required behavior you can use the QTableWidget widget. You need to:

  1. Create a QTableWidget widget with two columns and and one row.
  2. Set the QScrollArea widget as the parent of the QTableWidget widget.
  3. Hide the horizontal and vertical headers. To do so set horizontalHeaderVisible and verticalHeaderVisible to false .
  4. Add QCustomPlot widgets into the table using the setCellWidget(int row, int column, QWidget * widget) method.

这就是它的解决方法:我只需要为滚动区域调用setWidgetResizablefalse ,现在我有了一个永久滚动条,并且在拖动拆分器时自定义绘图不会调整大小。

Find if you have this line:

ui->my_plot->setInteractions(QCP::iRangeZoom | QCP::iRangeDrag);

And edit it to whatever you want, for example just range zoom:

ui->my_plot->setInteractions(QCP::iRangeZoom);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM