Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QHBoxLayout, QVBoxLayout, QBoxLayout or other?
I am new to Qt, and working on existing large code project.
I want to create a simple layout like:
<Button1> <Explanation1> <Button2> <Explanation2>
The explanations (and buttons) should line up vertically.
What is the right/correct/simplest approach to achieving this?
I believe I have seen that existing code would create a
QVBoxLayout, and then add each row onto a
QHBoxLayoutappended to the
QVBoxLayout(though unsure whether that would vertically align multiple controls on each line).
I thought raw
QBoxLayoutallowed a grid (both row & column position), but I now realise it's only either horizontal or vertical, so that doesn't help.
So I guess it is indeed an outer
QHBoxLayoutchildren, but how does that know to make each row's columns align with each other? And is this indeed the right way? (Ah, what about e.g.
QGridLayout, which existing code does not seem to use...?)
Sorry if dumb question, just want to get on with this...!
mostefa last edited by mostefa
I think that the QGridLayout is more appropriate for your needs
Did you check out
its what i often use for xxx | yyy layouts.
Could you just comment on: if code does use
QVboxLayoutparent + multiple-
QHboxLayoutchildren, and the
QHBoxLayouts each have multiple (but same number of) widgets added to them, do the "columns" of child widgets actually line up? I.e. does Qt left to itself do anything about making sibling
QHBoxLayouts have same column widths? Otherwise perhaps in existing code they are just divided up like 50% each for two, so they line up, or perhaps existing code sets fixed widths?
mostefa last edited by mostefa
@JNBarchan said in QHBoxLayout, QVBoxLayout, QBoxLayout or other?:
if code does use QVboxLayout parent + multiple-QHboxLayout children, and the QHBoxLayouts each have multiple (but same number of) widgets added to them, do the "columns" of child widgets actually line up? I.e. does Qt left to itself do anything about making sibling QHBoxLayouts have same column widths?
if code does use QVboxLayout parent + multiple-QHboxLayout children, and the QHBoxLayouts each have multiple (but same number of) widgets added to them, do the "columns" of child widgets actually line up?
No, there is no automatic alignement in this case , you can achieve a good alignement , but you have to play with (column stretch , row stretch...)
Trust me using QGridLayout , or QFormLayout will offer you more comfort , by using one of them you can maintain your code more easily in the future.
Thanks, that (lack of alignemnt in separate
QHBoxLayouts) confirms what I suspected.
I will indeed look at using
QFormLayoutfor new code I write. My question was because I wonder how the existing code has achieved columnar layout using only
QV/HBoxLayout, when i don't fancy trawling through it all to understand. I have a feeling it might be using fixed-width columns on each
QHBoxLayoutand that's how they achieved the alignment, but it doesn't matter going forward.
@JNBarchan my two cents worth is to use the form layout. The code you mentioned, if it didn't use forms or grids, probably used some other constructs (like setting fixed sizes) to get the alignment you saw. Anyway, forms are meant for just what you're doing. Grids would be fine too.
OK then in that case, anyone care to summarize the difference of
QFormLayout, e.g. which one is one more lightweight for my presumably minimal needs?
@JNBarchan well QFormLayout is a two-column layout designed for input devices and their labels...seems precisely right for you. QGridLayout is more general purpose. In actuality, I'd expect the difference in coding effort and performance to be minimal, but you might as well use something that's tailor-made for your purpose.
designed for input devices and their labels
By "devices" do you mean "controls/widgets"?
I think you're saying
Label1 Input1 Label2 Combo2 Label3 Checkbox3
I see the point of that. Funnily enough, though, although I (presently, but might change) do have 2 columns, it's a bit different here, it's something like
Button1 Explanatory text of why you might like to click the button :) Button2 Explanatory text ....
Since I'm not really sure what I might end up with, but I know there will be columnar alignment required whatever, I might rather look into
Yes, you did a better job wording it than I did. If you need the option of adding additional columns, then you'll want to use QGridLayout.
mranger90 last edited by
Slightly off topic, but if all you want to display is an explanation of the proper usage for the button, why not use a tool tip? That's what a tool tip is supposed to do without taking up screen real estate.
Because my stakeholders/users demand an on-screen explanation in this context. (Screen estate is not limited because this is the totality of this page.) Because they are fussy, pampered users who won't look at a tooltip (won't even see it --- "You mean I have to move the mouse?") and will moan if the layout is not exactly what they have decided they want, claiming they cannot possibly know what the buttons might do.
Not nice friendly, co-operative, open-minded, tolerant developers.
Get the picture? ;-)