Defining buttons in OpenGL-window
Hello there,
my question is, if it is possible to display buttons on an opengl-window. As you can see on the picture i have created in the main window two smaller ones. The right grey corner is designated for the buttons(marked with a red rectangle).This I created before using qt. Now i see that Qt is always creating the buttons at the border of the window. Is it possible to place my buttons in this red rectangle?
I am using a mac.
!http://s7.directupload.net/file/d/2619/9qc4ah7r_jpg.htm()!
Thanks in advance….
7 replies
Hm..the code is a bit long, but i am doing more or less the same as in the hellogl-example.
I have 4 classes.One is for creating spacial geometry and has nothing to do with my question.
One is GLWidget, which contains all viewport-information:
(I am posting only the header with some descriptions, because it is a bit long)
- {
- Q_OBJECT
- public:
- //variables...
- ~GLWidget();
- protected:
- void initializeGL();
- void paintGL();
- // in paintGL: draw the scene and defining the viewports
- // in paintGL:here i am only using c++ and OpenGL
- // in paintGL:only some differernces like: instead of glSwapBuffers() I am using swapBuffers()
- void resizeGL(int width, int height);
- private:
- };
The main class, which is the same as in the hellogl-example:
- int main(int argc, char *argv[])
- {
- Window window;
- window.show();
- return app.exec();
- }
The window-class, which also contains the keypressevent:
- Window::Window()
- {
- glWidget = new GLWidget;
- spline = new bspline;
- mainLayout->addWidget(glWidget);
- setLayout(mainLayout);
- setWindowTitle("Qt window");
- }
- {
- switch (e -> key())
- {
- glWidget->modus = 1;
- cout << glWidget->modus << endl;
- update();
- break;
- .....
- default:
- }
- }
Everything is working fine as it did before the transformation. But now i would like to use buttons in my window. As I said before Qt is new to me. I have read that i have to use setParent() but that using setParent is not possible under mac.I am a bit confused.
OK… so everything you have is really inside one QGlWidget, and the three “windows” are just something that is drawn using OpenGL. Right?
In the “window” where you want to put the buttons, do you need to be able to use OpenGL to draw things there? If not, I would not let that be part of the QGlWidget, but have that as a separate QWidget (or a subclass thereof). And then simply create the buttons as children of that widget, using a layout to keep them where you want them.
If you really need buttons on top of a QGlWidget, that might also be possible. You would have to place them using e.g. setGeometry(), and not using a layout, and possibly change their position in the resizeGL() function. But I’ve never tried this, so not sure if it works.
Thank you for your help.
In the “window” where you want to put the buttons, do you need to be able to use OpenGL to draw things there? If not, I would not let that be part of the QGlWidget, but have that as a separate QWidget (or a subclass thereof). And then simply create the buttons as children of that widget, using a layout to keep them where you want them.
The part where i want to have the buttons is not designated for any drawing or opengl-code. Only buttons. So I will have to create a new qwidget. What do you mean by:
using a layout to keep them where you want them.
Do I need the ui.-files for this or is this something different? Do you know any examples?
Sorry for my ignorant questions but Qt is so big and I feel striken down by the mass of possibilities, that i sometimes dont know where to start and how to put everything together.
Thank you so much for your help…
Qt is big, but pretty easy when you start getting a hang of it…
To just understand what you can do with a layout, simply add a QVBoxLayout to your mainLayout, and add some buttons to it, something like this (where the first two lines are already in your code):
- mainLayout->addWidget(glWidget);
- mainLayout->addLayout(vLayout);
- vLayout->addWidget(button1);
- vLayout->addWidget(button2);
- vLayout->addStretch();
You must log in to post a reply. Not a member yet? Register here!


