I have a QWidget with following items aranged in a vertical layout:
- QWidget
- QVBoxLayout
- QPushButton
- QPushButton
- QPushButton
- QListView
- QComboBox
now, I would like to arrange the QComboBox in a horizontal alignment to one of the buttons. I gave the booton a maximumSize of 36 x 16777215 and it sits at the top left of the widget. How Do I get the combo box to align on the right side of it? Here's a screen shot of what I currently have:
Embed your combobox into an horizontallayout and add spacer https://doc.qt.io/archives/2.3/designer/chap4_2.html
You can add a QWidget (or a QFrame) at the top of your vertical layout, then you give this widget (or frame) an horizontal layout and you put your button and your combox box inside this widget with the horizontal layout.
Related
I have 7 QWidgets in a QHBoxLayout.
Some of these are QPushButtons and some are QLabels with custom painting (homemade gauges).
How can I make sure that the 4th widget is centered in the layout?
I want to be able to resize the layout.
You can use horizontal QSplitter, and use void QSplitter::setStretchFactor(int index, int stretch) to set the factor of stretch.
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.
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.
Consider a dialog window:
I want button 1 to be centered horizontally, and button 2 to be in the right corner. How to achieve this?
You can put a horizontal layout that contains a horizontal spacer, pushbutton and another horizontal layout. This second horizontal layout contains a horizontal spacer and a pushbutton.
Something like this:
I have a bunch of buttons in Qt with a fixed size. Now I want to create a layout in which they are shown in a grid. I want the grid to be scrollable and when I resize my mainwindow, the grid should fit as much buttons in the layout as possible. The number of rows and columns are therefore not fixed.
I tried creating this with the hbox, vbox and gridlayout but they either put all the buttons below each other, or side by side. Also it resizes my mainwindow to an enormous size to fit all the buttons in.
Any Ideas?
Put QScrollArea inside your QMainWindow. Apply QGridLayout to QScrollArea's inside widget and put the buttons inside it.