I am designing a user interface with Qt Creator. I have a openGLwidget which covers all the form and I want to place some buttons aligned to bottom and some kind of notification frames over the widget. Is there any default way to do that?
When I try to design that on creator, I could not get rid of layout limits and don't allow me to place a widget over openglwidget.
When reading the question first, I was reading this as “How to make a
QOpenGLWidget
with Head-Up Display with buttons?”.How to make a
QOpenGLWidget
with Head-Up Display (painted withQPainter
), I once answered in SO: Paint a rect on qglwidget at specifit times. However, there was only painting – no interactive widgets.Hence, I prepared a new sample
testQGLWidgetHUDButtons.cc
:and the project file
testQGLWidgetHUDButtons.pro
:Compiled and tested in VS2013 on Windows 10:
It's actually quite easy –
QOpenGLWidget
is derived fromQWidget
.QWidget
provides the methodQWidget::setLayout()
. To add child widgets, aQLayout
should be used which manages layout of children in parent widget (and takes part in size negotiation when parent widget is layouted).To keep it simple, I used a
QGridLayout
where buttons are placed in cells (1, 1), (1, 2), and (1, 3). Row 0 and column 0 are left empty but enabled for stetching. This results effectively in attaching the buttons in lower right corner.When I was about to publish this answer, I took a second look onto the question and realized the Qt Button Group in title. I would've ignore it (multiple buttons are a button group?) but there is also the qbuttongroup which made me stumbling.
A
QButtonGroup
:(Emphasize mine.)
So, a button group can be used e.g. to keep radio buttons in sync. It is derived from
QObject
and hence, neither aQWidget
nor aQLayout
. Thus, it's not suitable to add visible child widgets to theQOpenGLWidget
.Though I've no experiences with QCreator, I assume it should work there as well as it does in my sample:
Assign a layout to the
QOpenGLWidget
(e.g.QGridLayout
as I did in my sample).Add
QPushButton
s to this layout.Btw. IMHO this is no specific problem about the
QOpenGLWidget
– it should've happend with any otherQWidget
as well.