Beginner's questions about QLayout
-
Hi, previously I was using QML, I am new to C++ GUI and I would like to 2 questions, I would like to have sth like:
- How can I specify a position and an area to put a grid layout? (Red rectangle)
- How can I position a single text (Blue rectangle) to the right of the grid? I know in QML I should use anchor, in C++ should I use setGeometry to specify the position? (Basically I don't want to put that single text into layout)
Thank you very much
-
Hi, previously I was using QML, I am new to C++ GUI and I would like to 2 questions, I would like to have sth like:
- How can I specify a position and an area to put a grid layout? (Red rectangle)
- How can I position a single text (Blue rectangle) to the right of the grid? I know in QML I should use anchor, in C++ should I use setGeometry to specify the position? (Basically I don't want to put that single text into layout)
Thank you very much
@StudyQt1
I have not used QML so I cannot compare against that.QWidget
s can have a position/size/geometry, but the whole point of usingQLayout
s is not to specify these, rather to allow the layout manager to figure this out. Once you put widgets on a layout you do not use widget coordinate positioning. You should start by reading Layout Management.(Basically I don't want to put that single text into layout)
Well, yes you do! You have two items which you want arranged horizontally, with the blue box at the top of the horizontal layout. So your layout would be a
QHBoxLayout
. You would first useQHBoxLayout::addWidget(redBox)
to add the red box widget, and then a secondQHBoxLayout::addWidget(blueBox, Qt::AlignTop)
to add the blue box to the right but aligned at the top.You can also add (sub-)layouts to layouts, instead of/as well as widgets. If your "Area to put a grid" is not just, say, a
QTableView
/Widget
widget for your "grid" but is actually a grid layout to hold a number of individualQWidget
s etc., then the first item you would add to your outer layout would beQHBoxLayout::addLayout(QGridLayout)
instead ofaddWidget()
(or you might a dd a genericQWidget
there and in turn use itssetLayout(QGridLayout)
). Then off you go and add widgets --- or further sub-layouts --- to the grid layout.You can use Designer in Qt Creator to play with adding layouts & widgets to see how they will appear and line up, etc.
The usual recommendation would be to use layouts rather than widget coordinate positioning if you can/unless you have some particular coordinate need. It will take care of changing device & window sizing for you appropriately, leaving you to specify the layout in "logical" terms.
-
@StudyQt1
I have not used QML so I cannot compare against that.QWidget
s can have a position/size/geometry, but the whole point of usingQLayout
s is not to specify these, rather to allow the layout manager to figure this out. Once you put widgets on a layout you do not use widget coordinate positioning. You should start by reading Layout Management.(Basically I don't want to put that single text into layout)
Well, yes you do! You have two items which you want arranged horizontally, with the blue box at the top of the horizontal layout. So your layout would be a
QHBoxLayout
. You would first useQHBoxLayout::addWidget(redBox)
to add the red box widget, and then a secondQHBoxLayout::addWidget(blueBox, Qt::AlignTop)
to add the blue box to the right but aligned at the top.You can also add (sub-)layouts to layouts, instead of/as well as widgets. If your "Area to put a grid" is not just, say, a
QTableView
/Widget
widget for your "grid" but is actually a grid layout to hold a number of individualQWidget
s etc., then the first item you would add to your outer layout would beQHBoxLayout::addLayout(QGridLayout)
instead ofaddWidget()
(or you might a dd a genericQWidget
there and in turn use itssetLayout(QGridLayout)
). Then off you go and add widgets --- or further sub-layouts --- to the grid layout.You can use Designer in Qt Creator to play with adding layouts & widgets to see how they will appear and line up, etc.
The usual recommendation would be to use layouts rather than widget coordinate positioning if you can/unless you have some particular coordinate need. It will take care of changing device & window sizing for you appropriately, leaving you to specify the layout in "logical" terms.
@JonB Thank you very much for your reply! Looks like in most C++ Qt GUI, "layout" is the fundamental, it always fills the parent widget area, and I don't need to care about the layout's elements positions. This is different from QML GUI, in QML though I use layout at the fundamental layer I still use "anchor" a lot
-
@JonB Thank you very much for your reply! Looks like in most C++ Qt GUI, "layout" is the fundamental, it always fills the parent widget area, and I don't need to care about the layout's elements positions. This is different from QML GUI, in QML though I use layout at the fundamental layer I still use "anchor" a lot
@StudyQt1 said in Beginner's questions about QLayout:
it always fills the parent widget area, and I don't need to care about the layout's elements positions
Indeed, that is their point. This scales well when users have all sorts of different screen resolutions, windowing systems etc. Layouts make a Qt GUI behave somewhat like an HTML page in terms of adjusting elements to fit. There are also stretches, spacers and various alignments available on layouts to control how it fits the available area if you wish.
You can use absolute positioning and size with widgets if you want instead of layouts, but mostly you do not want to do that.