I'm using Qt Designer to create a graphic interface, but i am struggling to do a basic operation.
I want to create a vertical layout in which there are 2 (or more) horizontals layouts. But if I try to grab my horizontal layouts to put them in the vertical one, the first horizontal layout ends up taking all the space in vertical layout, so i can't add the second one other than inside the first hozitontal layout.
I end up with Horizontal layout 1 in Horizontal layout 2 in Vertical layout which is not what i want
here is the picture or what i get :
VS what i want :
I tried to do it through the Tree object at the right, but it seems we can't drag and drop objets there nor can i copy and paste it to change the ancestor. Is there any way to do it other than editing the XML file ?
One fewer horizontal layout (spacers added for clarity):
Cause
Qt Designer sets the margins of the newly added layouts to 0, 0, 0, 0, so it is impossible to target with the mouse a 0px-wide area when you try to add something to the layout.
Solution
Change the margins of the layout temporarily to something, which is comfortable for you to hit with the mouse. Then you can easily drag and drop new items to this layout. Later you could change the margins back to 0.
Related
I want to make a GUI with QT Creator 4.9.1 ,my aim is to split my mainwindow into 5 areas(no multiple window), my question is how can i realize that, or better what is the best Layout solution for that?
I have allready tried to set a datagrid and add inside that grid 5 frames, the problem is that the first frame has the size of my whole datagrid and i can't resize it.
My next try was to add 5 different datagrid on my GUI but i can't set the size of the datagrid's in QT like wpf or forms.
My last try and my current solution is without any layout, i add 5 fame's inside my mainwindow but that isn't a good solution.
Inside visual studio i realize that with datagrids i create for every menu one grid and change the visibility when the user need a other (area 2).
friendly wishes sniffi
The suggestion to using dock windows may be a good one to investigate, particularly if you need to allow the user to resize or move things around. However, to get the layout you want with just layouts, the trick is to use multiple layouts.
Create a vertical layout for the left-hand side and add the four widgets to it. Create a horizontal layout and add the vertical layout in the first column and your tall, fifth widget to the second column. Apply the layout to your main windows, and that should give you roughly what you're looking for.
You'll almost certainly need to play with the row stretches on the vertical layout to get the proportions you want, and with the column stretches on the horizontal layout.
The alternate would be to create a grid layout where the widget on the right side spans four rows, but I think you'll be happier with the mix of the two layouts.
I am trying to make a basic wireframe design before I add colours, etc. As seen in the two pictures below, the sizing of the layouts is very different in designer and when the program is run. I have tried changing the layout stretch but it has no effect.
Designer:
Running program:
You need to decide what should happen when the window is resized larger than the size shown in Qt Designer. Presumably, you don't want the "Logo" and "Date" rows to expand to take up the extra vertical space, and you don't want the "Modules" row to expand either. So the extra space will have to go at the bottom.
The simplest way to achieve that is to put an expanding spacer immediately below the "Modules" row, but inside the outer layout. This will push everything up against the top of the window, and only allow space to grow at the bottom.
I use grid layout (horizontal and vertical too). I like the fact that when resizing the window fills the entire window contents. but this extension is poorly managed. I often want to change the size of only one column in grid layout without changing the size of the window. such as in Windows Explorer. there are two columns - the left list of directories and their contents to the left to the right. and i can always press mouse button therebetween and pulling change the mutual sizes of columns in relation to each other.
how can I do this in Qt?
You need to use a QSplitter rather than a QGridLayout in this specific case (where you just want 2 widgets shown together). QSplitters are draggable.
You are looking for QSplitter
(The following is the procedure in the Qt Designer)
Group your widgets, and click Lay Out Horizontally/Vertically in Splitter
Put this group into another layout (QGridLayout, for example) to automatically expand it.
Congrats! Your Layout is now draggable(from step1) and expandable(from step2).
I'd appreciate advise on to how create a Qt UI consisting of four layouts and has the following properties.
Any increases in the height of the UI is absorbed by the layouts, as shown below
Any further decreases in the height of the UI is absorbed by the contents of a specific layout, e.g. the two large buttons as shown below
1# Create new UI form base on QWidget: File -> New file or project -> Qt -> Qt Designer form class -> select Widget form templates, next, next, select project and finish
2# Add Vertical layout from left bar
3# After that click right somewhere on UI form, where is not just added layout, "Lay out" -> "Lay out in a grid"
4# You can adjust layout margin on right menu (I'm always setting 5 points)
5# Add four Horizontal layout
6# Add Button and text and what you need
7# Add Vertical spacers between Horizontal layout
8# Final result:
I prepared quick code what fits your problem, please take a look: https://github.com/troyane/StackOverflow-pro/tree/master/creating-auto-scaling-qt-ui-using-layouts
Grab that code and take a look at next moments (you can open mainwindow.ui in QtCreator):
centralWidget has next layoutStretch param: 1,2,1,1 -- it means, that we'll have next correlation among all items placed into this vertical layout.
TextLabel and both SmallButtons has Fixed VerticalPolicy
Both BigButtons has Minimum vertical policy and set minimumSize's Height to 100. UPD: Also maximumSize->height parameter is 250 px. So, it is guarantee that both BigButtons will not grow more than 250 px on height.
Take a look at another answer, there you can find lots of literature to read about Layouts.
You can put two vertical spacers in each layout. One should be placed at top most and the other one at the bottom :
Good morning,
I have to layout some QWidgets and layouts into a main layout, but I have a problem with a space that I can not remove.
Basically what I would achieve is a horizzontal layout containing a grid layout and some buttons ( all in a horizontal line ). The grid layout (2x2) contains 2 QLabels and 2 QLeds.
Unfortunately Qt place a space between the grid layout and the first button as you can see in the attached image here http://img413.imageshack.us/img413/9132/problemhu.png
I would remove such space.
Here the code I wrote:
QGridLayout* gl = new QGridLayout();
gl->setAlignment(Qt::AlignLeft);
gl->setContentsMargins(0, 0, 0, 0);
gl->addWidget(activeLabel, 0, 0);
gl->addWidget(m_focusLed, 0, 1);
gl->addWidget(encodingLabel, 1, 0);
gl->addWidget(m_encodingLed, 1, 1);
This created the grid layout and added the QLabels and QLeds on it.
Then I add the buttons into the horizontal layout so:
/* layout buttons */
QHBoxLayout* lo = new QHBoxLayout();
lo->setSpacing(0);
lo->addLayout(gl); // <--here I add the grid layout
lo->addWidget(m_goToBeginBtn);
lo->addWidget(m_goToEndBtn);
lo->addWidget(m_frewBtn);
lo->addWidget(m_fforBtn);
lo->addSpacing( 10 );
lo->addWidget(m_ffrewBtn);
lo->addWidget(m_ffforBtn);
lo->addSpacing(10);
lo->addWidget(m_prevBtn);
lo->addWidget(m_nextBtn);
lo->addWidget(m_playBtn);
lo->addWidget(m_stopBtn);
lo->addWidget(m_cutBtn);
lo->addSpacing(10);
lo->addWidget(m_zoomInBtn);
lo->addWidget(m_zoomOutBtn);
lo->addSpacing(10);
lo->addWidget(m_bgSndCheckBox);
lo->addWidget( m_showPanelBtn);
I don't know why Qt place such space between the grid layout and the first button. I would remove it. How can I do? I didn't get help from the Qt mailing list.
Best Regards
How to fix this largely depends on what behavior you want to see. I'm guessing what you want is for the labels and the Leds to stay exactly where they are, and keep their size.
What is happening is that the grid layout is resizing with your window, (like your buttons), but the left alignment is keeping the controls stuck to the left, thus the space.
First, remove the gl->setAlignment(Qt::AlignLeft) line.
Secondly, you want to make sure you set the sizePolicy properly on both your QLabels and your QLeds, because otherwise your QLeds will start to resize horizontally. What you want is a fixed horizontal size policy. Here is an example:
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
label->setSizePolicy(sizePolicy);
A completely different way to accomplish the same thing would be to add a bunch of calls to setStretch() on your horizontal layout. But you would have to do this for each column in your layout that you want to stretch. Basically each for each button, but skipping the grid layout in the first column. Like this...
lo->setStretch(1, 1); // Column 1 is your first button
lo->setStretch(2, 1);
...
lo->setStretch(19, 1)l // 19 columns in total, 15 buttons plus 4 spacing.