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....



  • 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?



  • 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



  • bq. 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.



  • 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)
    @
    class GLWidget : public QGLWidget
    {
    Q_OBJECT
    public:
    //variables...

    GLWidget(QWidget *parent = 0);
    ~GLWidget();
    QSize minimumSizeHint() const;
    QSize sizeHint() const;
    

    protected:
    void mousePressEvent(QMouseEvent* e);
    void mouseMoveEvent(QMouseEvent* e);
    void timerEvent(QTimerEvent *e);
    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[])
    {
    QApplication app(argc, argv);
    Window window;
    window.show();
    return app.exec();
    }
    @
    The window-class, which also contains the keypressevent:
    @
    Window::Window()
    {
    glWidget = new GLWidget;
    spline = new bspline;
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(glWidget);
    setLayout(mainLayout);
    setWindowTitle("Qt window");
    }

    void Window::keyPressEvent(QKeyEvent *e)
    {

    switch (e -> key())
    {
    case Qt::Key_F2:
        glWidget->modus = 1;
        cout << glWidget->modus << endl;
        update();
        break;
    .....
    default:
        QWidget::keyPressEvent(e);
    }
    

    }
    @

    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.

    bq. 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:

    bq. 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):
    @
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(glWidget);
    QVBoxLayout *vLayout = new QVBoxLayout;
    mainLayout->addLayout(vLayout);
    QPushButton *button1 = new QPushButton("Button 1");
    vLayout->addWidget(button1);
    QPushButton *button2 = new QPushButton("Button 2");
    vLayout->addWidget(button2);
    vLayout->addStretch();
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.