August 17, 2011

basil_fawlty basil_fawlty
Lab Rat
35 posts

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

August 17, 2011

ludde ludde
Ant Farmer
325 posts

Not sure I understand what you want to do – you seem to have three widgets, and one of them is a QGlWidget? And you want to put the buttons on top of that widget?

August 17, 2011

basil_fawlty basil_fawlty
Lab Rat
35 posts

No i havent got 3 widgets. I have three glViewports. I had code in opengl which i now changed into qt.
(I am a newbie in Qt)
So is it impossible to place buttons in a viewport?

Thanks

August 17, 2011

ludde ludde
Ant Farmer
325 posts

I have three glViewports. I had code in opengl which i now changed into qt.

This makes no sense to me… If you still have glViewports you have not changed the code into Qt, have you? I think we’ll need to see some code to understand what you are doing.

August 17, 2011

basil_fawlty basil_fawlty
Lab Rat
35 posts

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)

  1. class GLWidget : public QGLWidget
  2. {
  3. Q_OBJECT
  4. public:
  5.     //variables...
  6.  
  7.     GLWidget(QWidget *parent = 0);
  8.     ~GLWidget();
  9.     QSize minimumSizeHint() const;
  10.     QSize sizeHint() const;
  11. protected:
  12.     void mousePressEvent(QMouseEvent* e);
  13.     void mouseMoveEvent(QMouseEvent* e);
  14.     void timerEvent(QTimerEvent *e);
  15.     void initializeGL();
  16.     void paintGL();
  17.          // in paintGL: draw the scene and defining the viewports
  18.          // in paintGL:here i am only using c++ and OpenGL
  19.          // in paintGL:only some differernces like: instead of glSwapBuffers() I am using swapBuffers()
  20.     void resizeGL(int width, int height);
  21. private:
  22.  };

The main class, which is the same as in the hellogl-example:

  1. int main(int argc, char *argv[])
  2. {
  3. QApplication app(argc, argv);
  4. Window window;
  5. window.show();
  6. return app.exec();
  7. }

The window-class, which also contains the keypressevent:
  1. Window::Window()
  2. {
  3.     glWidget = new GLWidget;
  4.     spline = new bspline;
  5.     QHBoxLayout *mainLayout = new QHBoxLayout;
  6.     mainLayout->addWidget(glWidget);
  7.     setLayout(mainLayout);
  8.     setWindowTitle("Qt window");
  9. }
  10.  
  11. void Window::keyPressEvent(QKeyEvent *e)
  12. {
  13.  
  14.     switch (e -> key())
  15.     {
  16.     case Qt::Key_F2:
  17.         glWidget->modus = 1;
  18.         cout << glWidget->modus << endl;
  19.         update();
  20.         break;
  21.     .....
  22.     default:
  23.         QWidget::keyPressEvent(e);
  24.     }
  25. }

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.

August 17, 2011

ludde ludde
Ant Farmer
325 posts

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.

August 17, 2011

basil_fawlty basil_fawlty
Lab Rat
35 posts

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…

August 17, 2011

ludde ludde
Ant Farmer
325 posts

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):

  1. QHBoxLayout *mainLayout = new QHBoxLayout;  
  2. mainLayout->addWidget(glWidget);
  3. QVBoxLayout *vLayout = new QVBoxLayout;
  4. mainLayout->addLayout(vLayout);
  5. QPushButton *button1 = new QPushButton("Button 1");
  6. vLayout->addWidget(button1);
  7. QPushButton *button2 = new QPushButton("Button 2");
  8. vLayout->addWidget(button2);
  9. vLayout->addStretch();

 
  ‹‹ [Moved] How to display in an editor the result of encryption      Newbie Qtabwidget Question ››

You must log in to post a reply. Not a member yet? Register here!