Having layout like below. In QT Design, right clicked on below layout and clicked on 'Break Layout' and resized the QlineEdit control. But when I select layout and clicked 'Layout in a Grid' the QlineEdit reset to original size. Resized QlineEdit control does not restored.
Original image:
QGridLayout stretches all stretchable widgets in each column to have same width, you can limit widget's width using maximumSize
Related
I have a QLabel inside a QFrame.
Sometimes I have too much text in the QLabel and it resizes the QFrame where it is in.
Now, I want to prevent the QLabel from resizing the QFrame where it resides in.
I don't want to limit the amount of lines or setting the maximum size of the QLabel because if the window size of the app increases, I do want to allow the QLabel to increase in size.
Just want to prevent the QLabel from expanding it's parent.
Any clean way to do it?
Use a QScrollArea (which inherits QFrame), and hide its scrollbars:
label = QtGui.QLabel(text)
frame = QtGui.QScrollArea()
frame.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
frame.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
frame.setWidgetResizable(True)
frame.setWidget(label)
This has the side-benefit that the user will still be able to view any hidden text by scrolling with the mouse-wheel.
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.
I have a QListWidget with some widgets inside:
QListWidgetItem* w = new QListWidgetItem(ui->listWidget);
ui->listWidget->addItem(w);
w->setSizeHint(widget->sizeHint());
ui->listWidget->setItemWidget(w, widget);
However, everything seems to work fine until I resize the the QListWidget (it is embedded into a QDockwidget). Do I have to iterate over all items and resize them manually or is there a simple trick?
It is just a list with items having a button on the left and a button on the right. If the list gets resized (QSize changes) than the buttons gets hidden and a scrollbar appears. I want to have the size of the item according to the list width.
Try setting the resizeMode property to Adjust. This will cause your QListWidget to automatically resize all of the items in the view to the size of the QListWidget anytime the widget is resized.
See the documentation here. Your code will be structured like so:
QListWidgetItem* w = new QListWidgetItem(ui->listWidget);
w->setSizeHint(widget->sizeHint());
ui->listWidget->setResizeMode(QListView::Adjust);
ui->listWidget->addItem(w);
ui->listWidget->setItemWidget(w, widget);
EDIT: Depending on the uniformity of your items, you may also benefit from setting the uniformItemSizes property. QListView can optimize the layout of your view if you set this property.
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.
I have a window and a bunch of push buttons. This window will be my "Main Menu". After placing the buttons, I would like to have these buttons fixed to the size of this window. So they should fill the window and change their size if the window changes his (by the user for instance).
How do I do that?
You should put your buttons in a layout (see for instance QGridLayout or QVBoxLayout).
Example (assumes that window is your window, and button is your button):
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(button);
window->setLayout(layout);
This will make the button(s) expand horizontally. Would you like them to expand vertically as well, you need to change the vertical size policy of the button(s) as the default vertical policy for a button is to not use up more space than its preferred size.
button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);