Setting the sizepolicy in qt - qt

I have a widget and a graphics view in the central widget of my application. The widget has four push buttons in it. I added the both in horizontal layout using the following code:
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(ui->entityWidget);
layout->addWidget(ui->graphicsView);
ui->centralWidget->setLayout(layout);
I have set the size policy of the widget to be minimum and that of the graphics view to preferred. When I run the app, the widget is displayed appropriately in the top left corner but the graphics view's top left corner is in the middle of the central widget, which is not required. It should be right next to the widget.
How do I modify the size policy for correct placement of the graphics view in the main window?

It looks like you're not setting a parent widget to the Layout. That's likely your problem.
Try:
QHBoxLayout *layout = new QHBoxLayout(ui->centralWidget);
layout->addWidget(ui->entityWidget);
layout->addWidget(ui->graphicsView);
ui->centralWidget->setLayout(layout);
Also, if you post how your setting the SizePolicy that would help.

Just add a stretch to the layout right after the graphicsView:
layout->addStretch();

Related

Cannot align items to top in scroll area Qt

In a Qt Widget Application, I'm trying to make the following:
My aim is to have a form with a scrollArea with a vertical layout and each item in the scroll area aligned at the top.
Unfortunately, it's only staying in the centre shown here:
I've tried selecting the scrollArea and chaninging vertical alignment from AlignVCenter to AlignTop, but this seems to have no effect.
How can I align all the content to the top of the scroll box?
You can use a QListWidget to hold a list of not only just text, but also QWidget's (such as push buttons, line edits, etc).
Simply add a QListWidget to your window, and in C++ add the widgets to the QListWidget as needed.
Here are some steps to insert a button into your QListWidget:
Create a QListWidget item into your application.
change selectionMode = NoSelection and focusPolocy = NoFocus
Put the following C++ code into your application:
// Create list item
QListWidgetItem *listWidgetItem = new QListWidgetItem();
// Create widget
QWidget *widget = new QWidget();
QVBoxLayout *verticalLayout = new QVBoxLayout();
verticalLayout->setSizeConstraint(QLayout::SetFixedSize);
widget->setLayout(verticalLayout);
// Add button to widget layout
QPushButton *pushButton = new QPushButton("Button!");
verticalLayout->addWidget(pushButton);
// Add list item to ul->listWidget
listWidgetItem->setSizeHint(widget->sizeHint()); // Set the size of the list item to the same as the widget
ui->listWidget->addItem(listWidgetItem);
ui->listWidget->setItemWidget(listWidgetItem, widget);
Old answer:
I found my answer here but I will re-post it for anyone else interested.
To align elements at the top of a scroll area, add a vertical layout as a child to the scroll area and put all your elements inside that vertical layout. Like so:
In your C++, add a spacer item to your vertical layout inside the scroll area by using addStretch():
ui->verticalLayout->addStretch();
All the items should now magically be aligned to the top.
Edit:
This seems to only work for the very first item, and all other items are not pushed up to the top.

Qt-designer: enlarging the window does not replace my items

To keep the question as simple as possible, I prepared a simple Qt designer form below
There is a Tab Widget on the left side and empty QWidget on the right side, the QWidget as a GroupBox. The Groupbox has a radio and pusbuttons (you can see them on Object inspector window on the photo as well ). The tab widget has a line edit. The central widget has a gridlayout and horizontal qsplitter is used.
My issue is that when I enlarge the window, all items (lineedit, radiobutton, pushbutton) are on the fix position. Here is an example what I mean:
What I want is that when the window is enlarged the items should be placed on the bottom of the window, or if they are in the middle, then they should stay in the middle. ( I don't want size of the buttons/lineedits to be changed).
How can I do it?
The items you want to move dynamically, with window resizing must be in a layout.
So, in the example you've posted, you need two layouts; one inside the tab widget, for the QLineEdit and at least one in the GroupBox for the radio button and push button.
If you want the radio and push button to be aligned horizontally, you can start by placing them in a horizontal layout, before placing that layout in another, which all reside in the group box.
When you start to add items to layouts, such as push buttons, you'll start to notice that they can get stretched, so you may need to set the size policies of the widgets.
If you want the line edit to be centered horizontally, you will have to place two horizontal spacers on each side of the line edit and select the three together and set "Lay out Horizontally". This can found at the top toolbar in Qt Designer.
To always have it at the bottom of the tab widget, put a vertical spacer above the line edit in your tab widget. Then select the option "Lay out vertically" for the tab widget.
The same goes for your radio button and push button. Keep them in a horizontal layout, with horizontal spacers if required and put a vertical spacer into the group box and set the layout property for the group box as "Lay out Vertically".
Most important of all, I suggest you go through some basic tutorials before you continue. Here is a link to a good channel on youtube.
https://www.youtube.com/playlist?list=PL2D1942A4688E9D63
If you don't have a layout in your tabWidget or GroupBox:
You must set a layout (for example QVBoxLayout) inside your tab widget and a group box.
It can be done using QtDesigner. It also can be done in code like this:
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
window->setLayout(layout);
But if you do, and then you want your buttons to stay at the bottom:
Then you have to try setRowStretch method http://qt-project.org/doc/qt-4.8/qgridlayout.html#setRowStretch or take a look at QSpacerItem.

Qt ScrollArea on widget messes up size and position of widget [Qt 5.1]

I'm new with Qt and I want to implement a scrollable widget which can grow dynamically, e.g. by adding buttons into it when another button is pressed. I try to implement it using the following layout and code:
scrollArea = new QScrollArea(ui->outerWidget);
scrollArea->setWidget(ui->innerWidget);
layout = new QVBoxLayout(ui->outerWidget);
ui->innerWidget->setLayout(layout);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// code for PushButton to add buttons in innerWidget
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("button"+QString::number( nameCounter ));
nameCounter ++;
ui->innerWidget->layout()->addWidget(button);
}
This implementation has two problems when I run the program, first the innerWidget appears out of place (I define its position in Qt Creator's Design mode) and second after many widgets are added in the layout the scroll bar is doesn't grow, but instead the widgets are become smaller to fit into the layout:
In another thread it was suggested to set widgetResizable to true:
scrollArea->setWidgetResizable(true);
The scroll bar seems to work now but the new problem is that the innerWidget becomes very small so it is barely visible:
So, how can I achieve a scrollable widget that will respect the size and position of the design?
have no environment to verify but good luck.
to place scrollArea the target position,
Manually call SetGeometry, or
Place a QScrollArea in ui form, (suggested if it's static), or
Place a widget in your target position, and set QScrollArea's parent to it, and add QScrollArea to its layout, and Set QScrollArea to be expanding.
manually create a QWidget innerWidget and assign it to scrollArea via QScrollArea::setWidget(*QWidget), try different size policy to innerWidget, such as "Preferred".
Also be aware of Qt's reference mentioned : void QScrollArea::setWidget ( QWidget * widget )
Sets the scroll area's widget.
....
Note that You must add the layout of widget before you call this function; if you add it later, the widget will not be visible - regardless of when you show() the scroll area. In this case, you can also not show() the widget later.
Above list solutions, below are reasons to the problems you mentioned:
scrollArea->setWidget(ui->innerWidget); Setting a widget to ScrollArea will change the parenting and layouting of ui->innerWidget, so the geometry values written in ui form (in Qt Creater) will no longer take effect, this is the reason of innerWidget out of place. ui->innerWidget is no longer a child of outerWidget, it's geometry will follow its new parent (but not scrollArea, there's some tricky layouting inside QScrollArea". To be clear, innerWidget is not helpful to locate scrollArea in such scenario.
In your first clip of code, widget 'scrollArea' is created with parent outerWidget, again no size policy or layout or geometry is specified, so scrollArea will by default be placed at the left top corner of the parent "outerWidget". To place scrollArea to your target geometry, you can " set geometry manually " or " assign innerWidget as scrollArea's parent and expand scrollArea". Obviously the latter method cannot assign ui->innerWiget to scrollArea->setWidget().
scrollArea->setWidgetResizable(true); makes the scrollArea "shrink" at left top corner of outerWidget. This is because, QScrollArea does not increase along with it's contents, it can scroll to display all of it contents so the required size of QScrollArea can be as small as possible. Once the 'Resizable' property is set to "true", QScrollArea decides to shrink to its minimum necessary size, thus the size to display its scroll bar and scroll buttons....

How to prevent QTableWidget from occupying the whole window in a QHBoxLayout?

In my Qt program, I programmatically generate a modal QDialog. I want to show two widgets in this dialog window: A custom widget showing a camera output and a QTableWidget, showing the pixel coordinates of the corners found in the camera image. I generate a QHBoxLayout and add my custom widget and the QTableWidget into it. Then I set this QHBoxLayout as the Layout of the QDialog window. What I want to achieve is to share the available space in the QDialog's window area equally between my custom QWidget and the QTableWidget, horizontally, by using a QHBoxLayout. But I always end up with QTableWidget occupying the whole QDialog area, by overlapping my custom widget. How can I instruct these two widgets to exactly share the QDialog area?? Note that I first add my custom widget and then the QTableWidget into the QHBoxLayout.
Make sure on your custom widget you've specified a minimumSizeHint and a sizeHint, this instructs the QLayout manager that the widget requires a specific space. To have them split equally you'll be best off detecting the size of the QDialog and then specifying the width for both by removing the boundary sizes (spacing between widgets + space to QDialog edge) and dividing it up.

QWidget is not showing up on entire screen

I am a newbie to Qt and facing issue in my application. So here is what I am trying to do.
I have a class with QWidget as parent. This class has a grid layout on it. I set this widget as a central widget to main window. (this thing worked fine though this widget is not centered on Main Window.)
Now I wanted to rotate this widget in 90 degrees so that it can bee shown on device in landscape mode. So I created a graphics view and added this widget to it. (created a scene added widget to that and assigned scene to view.) then this graphics view was rotated and made central widget. this worked amazingly and it was very well centered, looked good on device as well.
But in final integration we want to have all QWidgets so Graphics View is not an option. To tackle this I created a Qwidget member inside my class. applied the layout to it. added this widget to graphicsView and my class was made parent to it.
This also works but has the similar issue I faced in first step, it sits in the top left corner of MainWindow and does not adjust to center. To make things even worse, when deployed on device it was not applied to entire screen. Widget was sitting in some part of top left area and had scroll bars to it! I even tried the set Window State to maximized but had no effect on it.
Here is what I tried
The widget is created inside my class and been added to Graphics View. this view has my class as parent and it will be rotated by using rotate api.
m_gridContainer = new QWidget();
m_gridContainer->setAutoFillBackground(true);
m_gridContainer->setPalette(blackPalette);
m_gridContainer->setLayout(m_grid);
m_gridContainer->setMinimumSize(480,265);
m_scene = new QGraphicsScene(0, 0, 480, 265);
m_window = new QGraphicsView(m_scene,this);
m_scene->setBackgroundBrush(brush);
m_window->setAlignment(Qt::AlignCenter);
m_scene->addWidget(m_gridContainer,Qt::Widget);
To use it from main Window
m_window = new CMyWidget(label, m_txtBox->text());
m_window->getRotatedWidget(90);
setCentralWidget(m_window);
I tried whatever I can but this widget is not being shown full screen (in just a small area) and still have scroll bars to it. I have no idea what exactly is happening there.
m_gridContainer being a graphics item part of a graphics scene, it doesn't receive the resize events of you main window.
You have to handle the resizeEvent() on your CMyWidget and adjust the size of your m_gridContainer accordingly, e.g.
void CMyWidget::resizeEvent ( QResizeEvent * event )
{
m_gridContainer->resize(event->size());
}
BTW, if you are handling the rotation for use on a mobile device, it is managed by Qt itself (by simply resizing your main window). You don't have to do it in your code (you'll actually end up with a widget looking like it has been rotated twice).

Resources