How to keep widget in the center of layout - c++

I'm working on Qt interface now and I have a widget in the center of the window, the widget has fixed size. when I resize the window with mouse the widget stays at the left of the window. I don't want this, but I want to stay it in the center.
I tried this;
listWidget->setFixedSize(640,480);
listWidget->adjustSize();
layout->addWidget(listWidget,Qt::AlignCenter);
but it doesn't do that.
can you help me?

This code works fine for me :
QGridLayout * layout = new QGridLayout(this);
QListWidget * listWidget = new QListWidget(this);
listWidget->setFixedSize(640,480);
listWidget->adjustSize();
layout->addWidget(listWidget,0, 0,Qt::AlignCenter);
But an other way is to put two vertical spacers and two horizontal spacers in the layout. One should be placed at top most, the other one at the bottom, one on the left and one on the right :

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.

QHBoxLayout widget alignment only affects first widget

when I create a QHBoxLayout and I add widgets in it with addWidget and then call myLayout->setAlignment(newWidget, Qt::AlignLeft), this only snaps the first widget I add. When I add other widgets and set their alignment to left too, they still start off at the center of the layout and then get squeezed in the more widgets I add. How do I make them all stick to the left next to each other?
If I understood it correct, what you want is to have your widgets in a horizontal line but as left as possible?
If so you can add your QHBoxLayot to the general this->getLayout()->addLayout(YourLayout)and then use setContentMargins(); or setStretchFactor(QWidget * widget, int stretch)
Without the code is difficult but ... Another possible problem can be the widgets size. You can fix it using setSizePolicy(...) with the MinimumExpanding setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );

setGeometry on button in group with Qt?

So I am in the works of making my first GUI. So far I have made a groupbox and then made a layout and added buttons widgets to that layout(and then added that layout to the group).
All this works fine but when I then compile it, it fills the whole width of the groupbox and something like "50" in height.
I then tried to use setGeometry on the buttons but this does nothing, I can set a size with setFixedSize() but not a positions, why is this? and how can i set a position on the buttons?
You should try to add horizontal and vertical spacers in your layout to position your widget in a proper place in layout. For instance you can place two horizontal spacers on the left and right side of your button and two vertical spacers at the bottom and top. This way your button always stays at the center of layout with an appropriate size.
If you are using Qt Designer you can find them in the Spacers section and add them to your layout. If not you can use QSpacerItem like:
QSpacerItem *horizontalSpacer;
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer, 0, 0, 1, 1);

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.

resize problem in scroll area

Hello everyone, here is my code:
myplot *p = new myplot(gao.structpayloadgraph,
gao1.structpayloadgraph,
gao.structcol-2, "payload");
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setWidgetResizable(false);
p->resize(ui->scrollArea->size().width(), ui->scrollArea->size().height());
ui->scrollArea->setWidget(p);
I want p to take up the full available space of the scrollbar area and fit itself. However, the appearance looks 'squeezed' even though I called the resize function. What should I do to achieve the desired effect?
You have to treat the scroll area content widget as a normal QWidget. If you want automatic resize and you must use layouts in Qt. Try the following :
QVBoxLayout layout = new QVBoxLayout( ui->scrollAreaContent);
layout->setMargin(0);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
ui->scrollAreaContent->setLayout( layout);
layout->addWidget(p);
NOTE: ui->scrollAreaContent is a guess, but I think you are using ui files and default content widget is named like that ...
Go to the top right of the QT creator designer screen (Object, Class), right click on the QScrollArea row and select the "Lay Out" menu item, choose a layout (eg vertical or horizontal layout), make sure that your QWidget has a minimum or above size policy. Your scroll widget should now resize with the layout.