How to center widget inside QStackedLayout? - c++

I have a QProgressBar that I need to be centered inside QStackedLayout. How can I achieve it, given that QStackedLayout::setAlignment doesn't have any effect, and the widget is always in the top-left?

Place the QProgressBar in a container (i.e. QWidget) that allows it to be centered with an additional layout.

Related

Qt: Force size of QDockWidget

I have a QMainWindow with a center widget and a number of dockable widgets. The size of the various widgets get manipulated by the sliders between the dockables. I would like to fix the center widget's height to it's minimumSize height and not allow it to be made larger by the user. The width should remain relative to the QMainWindow (I only allow top and bottom dockables, so the center widget is stretched to the width of the main window).
The center widget is a QDockWidget, containing a QWidget with a QVBoxLayout (containing more stuff).
I have tried deriving from the QVBoxLayout and overriding maximumSize, but it wasn't respected. Overriding expandingDirections also didn't make any difference.
Any advise on how to achieve this?
Got it, thanks to Johannes Schaub.
I thought the QDockWidget propagates the constraints to its contained widget, which it does not. Setting maximumHeight on the inner Widget does the trick.

Lay out an image on a Qt dialog

How to add an image to a dialog in Qt?
I know this has been often asked in the past and most answers come up with a QLabel and its setPixmap member. However, this usually is not what the user (me) intends:
A QLabel with a pixmap set does not participate in the surrounding QLayout. That is, it simply refuses to resize when the dialog is resized, like e.g., a QPushButton would do. Two QPushButtons next to each other in a QHorizontalLayout will (something like) equally divide the available horizontal space between them. A QLabe with a pixmap next to a QPushButton in the same layout will just stay fixed in size.
By default, a naked QLabel won't resize its contents when it's resized.
But when it does (QLabel::setResizeContents) it won't keep aspect ratio.
Is there any native way to have a pixmap shown on a dialog and have it reasonably participate in the layout?
Item resizing can be managed via sizePolicy property. From Qt documentation:
sizePolicy : QSizePolicy
This property holds the default layout behavior of the widget.
If there is a QLayout that manages this widget's children, the size
policy specified by that layout is used. If there is no such QLayout,
the result of this function is used.
The default policy is Preferred/Preferred, which means that the widget
can be freely resized, but prefers to be the size sizeHint() returns.
Button-like widgets set the size policy to specify that they may
stretch horizontally, but are fixed vertically. The same applies to
lineedit controls (such as QLineEdit, QSpinBox or an editable
QComboBox) and other horizontally orientated widgets (such as
QProgressBar). QToolButton's are normally square, so they allow growth
in both directions. Widgets that support different directions (such as
QSlider, QScrollBar or QHeader) specify stretching in the respective
direction only. Widgets that can provide scroll bars (usually
subclasses of QScrollArea) tend to specify that they can use
additional space, and that they can make do with less than sizeHint().
I think you are searching for QSizePolicy::Expanding size policy:
The sizeHint() is a sensible size, but the widget can be shrunk and
still be useful. The widget can make use of extra space, so it should
get as much space as possible (e.g. the horizontal direction of a
horizontal slider).
Set this for your QLabel and check how it will resize. Try other values from QSizePolicy::Policy enum.

How to hide/show a QLabel and QTextEdit at the same time in my Qt application?

I am working on a hide/show feature for my console in my Qt GUI application. The console consists of 2 widgets; QLabel and QTextEdit. Do I need to add the QLabel and QTextEdit to a QWidget in order to show/hide them, or is there a better way?
So basically I am looking for a container such as 'JPanel' in Java...
Do I need to add the QLabel and QTextEdit to a QWidget in order to show/hide them, or is there a better way?
Multiple methods are possible here. You can, as you suggest, create a parent QWidget and add the QLabel and QTextEdit to a QWidget. Calling show and hide on the parent widget will affect its children.
Another method would be to have a slot function, which when an action is called, the slot calls show / hide on the 2 widgets.
Neither is right or wrong and depends upon the overall design of your application.
Ok, I think you need to use a layout (horizontal / vertical):
The simplest way to arrange objects on a form is to place them in a
horizontal or vertical layout. Horizontal layouts ensure that the
widgets within are aligned horizontally; vertical layouts ensure that
they are aligned vertically.
Horizontal and vertical layouts can be combined and nested to any
depth. However, if you need more control over the placement of
objects, consider using the grid layout.

Avoid automatic transition of QGraphicsScene on QGraphicsView

Qt's QGraphicsView has a habit of automatic transition/sliding of the view in such a way that everything drawn on the attached QGraphicsScene is visible. For example, suppose you've drawn something on the scene and viewing in the view. Now upon a button press you draw something along the right edge of the view. The scene/view will slide to the right slightly so that everything is again visible.
I want to prevent this to happen. How can I do that?
Thanks.
The scene rectangle of QGraphicsScene is by default the bounding rectangle of all the items in the scene. As such it grows when items are added.
The scene rectangle of QGraphicsView is by default taken from QGraphicsScene::sceneRect. If you don't want this to change automatically you can set one explicitly with QGraphicsView::setSceneRect.

Is it possible force Qt to call paintEvent after other Qt components are drawn?

I've a class that extends QWidget and contains a QLabel (lblBackground). I've overriden paintEvent function too.
I want to draw something on top of lblBackground however paintEvent method is called before the QLabel is drawn. Thus my custom drawings are overwritten.
Is there a way to change drawing order?
Painting the children on top of their parent is the common thing to do. That being said you could try one of the following options:
extend QLabel itself to paint whatever you want
try to set the Qt::WA_TranslucentBackground flag on the QLabel and having an alpha channel, so that the underlying parent (QWidget) would shine through
if you are only using the QLabel to paint some background, maybe you can get rid of it and paint the desired background first thing in the QWidget's paintEvent()?
If you want to use label as a background then just create your custom widget as a child of your label. May be split some window frame related tasks if any (to be implemented as a parent of the label) and drawing/controls/etc (to be child of the label).