Setting minimum fixed size QCustomPlot bar graph inside QSplitter - c++

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 i.e. dragging the splitter left/right should reveal more/less of the graphs, and not resize them
Queries
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?
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 i.e. 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:
Create a QTableWidget widget with two columns and and one row.
Set the QScrollArea widget as the parent of the QTableWidget widget.
Hide the horizontal and vertical headers. To do so set horizontalHeaderVisible and verticalHeaderVisible to false.
Add QCustomPlot widgets into the table using the setCellWidget(int row, int column, QWidget * widget) method.

This is how it was solved: I just had to call the setWidgetResizable with false for the scroll area and now I have a permanent scrollbar and the custom plot does not resize while dragging the splitter.

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);

Related

Get size of QScrollArea viewport before showing

I have a custom QDialog comprised of a QStackedWidget with QScrollArea widgets for each page of the stacked widget.
I want to set the size hint for the QDialog such that the dialog is just large enough that the scroll bars for the scroll area are not visible when the dialog is first shown (i.e. ensure size of QScrollArea viewport = size hint of child widget in scroll area). Currently, the default sizeHint() implementation for the QDialog has insufficient height, which causes the vertical scroll bar to be shown when first loaded.
I thought this could be achieved by re-implementing sizeHint() for the QDialog, whereby the size hint of the dialog would be adjusted by the amount required for the size of QScrollArea viewport to equal the size hint for child widget in the scroll area (for the first page of the stacked layout). Unfortunately, in sizeHint(), the size of the QScrollArea viewport is set to the default size of QStackedWidget (640x480), and only updates to the correct size once the QDialog is shown.
Is there some way to get the correct size of the QScrollArea viewport before it is shown, or another way to achieve the desired effect of adjusting the size hint of the dialog to prevent scroll bars from being shown when it is first displayed (aside from hard-coding the dialog size).
With the composition of your dialog as:
I have a custom QDialog comprised of a QStackedWidget with QScrollArea
widgets for each page of the stacked widget.
The tricky part is to answer:
Is there some way to get the correct size of the QScrollArea viewport
before it is shown?
Well, before switching to certain page you can estimate the scroll area viewport if it is either correctly set or you can just measure the content going inside the scrollarea. I usually force the widget to demand certain height from the scroll area like that:
wdgetInScrollArea->setMinimumSize( widgetInScrollArea->sizeHint() );
wdgetInScrollArea->adjustSize(); // sometimes it is needed
The the scroll area viewport hint is then more 'adequate':
qDebug() << scrollArea->viewPortSizeHint(); // report
I don't see the code but usually it is not even required to do any custom event handling here, just prepare all the nested widgets like that.

Add widget next to qscrollbar

How do I place a widget next to a QScrollbar like here seen:
I use a QScrollArea and overwrite the Horizontal-QScrollBar. First I thought, I could use the paintEvent to draw a text like the "100 %" next to the bar. But I can only overwrite the existing painting.
Now I think, the only opportunity would be to implement the hole QScorllBarPrivate from the source code... anyone any idea?
The main idea is to create an overlay obscuring default horizontal scroll bar and displaing your own bottom line instead.
Create a widget representing the bottom line, i.e. a widget with labels displaying some information and a horizontal QScrollBar, all together put in a hbox layout.
Put this widget in the scroll area without adding it to a layout by making QScrollArea direct parent widget of this widget.
Use move() and resize() on the bottom line widget to position it properly on initialization. Also resize and reposition it on scroll area resize (you can use event filters or inheritance to get to resize events).
Make sure that scroll area's horizontalScrollBarPolicy is Qt::ScrollBarAlwaysOn so that scroll area's internal layout always keeps space enough for bottom line widget to fit in.
Also make sure that bottom line widget has the same height as default scroll bar. It should be easy as long as you remove spacing and margins in the hbox layout and labels (or other widgets) don't require more vertical space than a scroll bar.
Use horizontalScrollBar() to get scroll area's internal QScrollBar and syncronize it with your own bar. QScrollBar has rangeChanged() and valueChanged() signals so you can connect to them and update your bar properly. When user changes value of your scroll bar and triggers its valueChanged() signal, you should set the same value for the internal scrollbar. You can protect from infinite recursion in there by using a flag that indicates that this is your own change.

Preventing a QScrollArea from displaying a horizontal Scroll bar

I currently have the following structure in my form
I have a QFrame (Brown) that has a QScrollArea . Now Multiple QFrames are dynamically added to the QScrollArea (gray).The dynamically added QFrame are composed of a QLabel.
Now here is the problem I have disabled the horizontal scrollbar in the QScrollArea . Thus the horizontal scrollbar does not show up. The problem is that when the dynamically added QFrame (gray) is added to the the QScrollArea. Half of the frame is cut off. This is because I have no way to scroll horizontally. What I want is to have the dynamically added Qframe expand vertically instead of horizontally. Any suggestions ?
Update :
I have a QVBoxLayout inside the QScrollArea
Set the proper horizontal size policies for your dynamically created frames while creating them. One option is fixed size (QSizePolicy::Fixed), the other is QSizePolicy::Maximum (it's not very intuitive, but actually maximum means that the frame won't be bigger than the size specified by sizeHint() function). If you want the widget to expand vertically, set the vertical size policy to QSizePolicy::MinimumExpanding or QSizePolicy::Expanding - whatever works for you.

QScrollArea widget is not expanding with a Flowlayout

I have a ui with a QScrollArea Widget. The QScrollArea uses a Flowlayout. My problem is when I add widgets to my layout the scroll area begins to scroll and does not expand when it has room to expand. I want the scroll area to expand to its limit before the scroll bar appears first.
How can I get the scroll area to expand before the scroll bar appears?
can you try doing setWidgetResizable(true) for your QScrollArea
ScrollArea->setWidgetResizable(true);
A couple of suggestions:
Ensure that the size policy of the scroll area itself is Expanding.
Set the "stretch" values of the size policy of the scroll area to a value greater than that of the other widgets in the same layout. Ie:
QSizePolicy policy = pScrollArea->sizePolicy()
policy.setVerticalStretch(1);
policy.setHorizontalStretch(1);
This assumes that the siblings of the scroll area (if any) have a stretch value of 0 (the default).
Subclass the scroll area and override the sizeHint() method.

Anchoring a QGraphicsView to the corner of the screen

I have a QGraphicsView that renders my game. This view fills the entire screen at all times. In the top right I have another QGraphicsView which I'm using as a mini-map; it sits over the game view. I want this mini-map to be anchored to the top right of the screen, always maintaining its size. This code almost works, except that the left side of the mini-map never changes (which is to be expected).
void MainWindow::resizeEvent(QResizeEvent *event)
{
mainWindow->graphicsView->resize(event->size().width(), event->size().height());
QRect newRect(mainWindow->miniMapGraphicsView->geometry());
newRect.setRight(event->size().width() - 20);
mainWindow->miniMapGraphicsView->setGeometry(newRect);
}
How can I do this?
When you want to position widgets in a certain way, layouts are usually the best way to do it.
Add a QFormLayout to your graphicsView and set its layoutDirection to Qt::RightToLeft. Then add your miniMapGraphicsView to the layout. Editing the properties of the mini map, set its horizontal and vertical sizePolicy to Fixed and set its minimumSize and maximumSize to the dimensions you would like it to be.
An alternative would be to use a QGridLayout and use horizontal and vertical spacers to push the mini map to any corner of the view.
NOTE: Layouts have margins set by default so if you want your widgets to align snugly at the edges, zero them out.