Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWidget with QopenGLwidget



  • How can I bind Qwidget in to QopenGLwidget?


  • Lifetime Qt Champion

    Hi,

    What exactly do you want to do ?



  • @SGaist I have created one line in QopenGlWidget. one point of this line is fixed and at another point there is a widget.
    now,When I drag that widget the line should not be seperated from widget(this problem I am facing here).
    So,I want to bind that widget with that line point.


  • Lifetime Qt Champion

    Can you show how you are doing that ?



  • @SGaist

    Not completed yet,

    #include "openglclass.h"
    
    
    openGlClass::openGlClass(QWidget *parent):QOpenGLWidget(parent)
    {
        count=0;
        W= new QWidget(this);
        W->setStyleSheet("background-color : rgb(0,170,127);");
    
       W->setFixedSize(50,40);
    
       QPoint pl= W->geometry().bottomLeft();
       QPoint pr= W->geometry().bottomRight();
        qDebug()<<"point : "<<pr << pl;
        W->show();
    }
    
    void openGlClass::initializeGL()
    {
     qDebug()<<"Initialize"<<count   ;
     //initializeOpenGLFunctions();
    
     glClearColor(0.7,0.7,0.7,1);
     glEnable(GL_LIGHTING);
    }
    void openGlClass::paintGL()
    {
        qDebug()<<"paint"<<count;
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_LINES);
             glColor3f(1,0,0);
          glVertex2f(0.1f, 0.7f);
          glVertex2f(-0.9f, 0.1f);
         glEnd();
    
    }
    
    void openGlClass::resizeGL(int w, int h)
    {
        qDebug()<<"Resize"<<count;
    
    }
    
    void openGlClass::mousePressEvent(QMouseEvent *event)
    {
    
    }
    
    
    
    
    

    O/p:
    0_1532409871207_Untitled.png

    The Green Box is simple widget and line I have Render in OpenGl,not i want to Bind both so that when i Drag that box the line Should also adjust with that.


  • Lifetime Qt Champion

    @hjohn said in QWidget with QopenGLwidget:

    QPoint pl= W->geometry().bottomLeft();
    QPoint pr= W->geometry().bottomRight();

    These should be class variables. ( live in .h)
    and u do
    pl= W->geometry().bottomLeft();
    pr= W->geometry().bottomRight();
    and also updates them when you move the widget
    and then use them in the
    void openGlClass::paintGL()
    for the line drawings.



  • @mrjj But Problem is that, Qwidget coordinate system and openGL coordinate system are different.
    In Qwidget,the Origin(0,0) is on top left corner whereas In QOpenglwidget origin(0,0) is centered.
    So,when in Widget if we set Geometry as a

    W->setGeometry(0,0,50,40);
    

    and in line

      glBegin(GL_LINE);
              glVertex2f(0.0f, 0.0f);
              glVertex2f(0.5f, 0.5f);
              glEnd();
    

    Both are rendered in Diffetnt place.
    the Geometry position of widget is set based on Qpoint.
    In openGl is based on Veretex. I have manually scale the values.But how to set it dynamically.Any idea?


  • Lifetime Qt Champion

    @hjohn

    yep they live in completely different world.

    You have to make a function that maps between Widget x,y to openGl
    or find a better way to archive what u want.

    Normally one would paint the widget to a texture and show on a plane
    inside the openGl or similar ways. I also think u can paint with QPainter.

    Do you really need the lines to be opengl?



  • @mrjj said in QWidget with QopenGLwidget:

    Normally one would paint the widget to a texture and show on a plane

    If I paint Widget then there is no chance to add something or modify the values display on widget,and I want some data to be displayed on that widget.
    Yes,I need that Line to be OpenGl

    @mrjj said in QWidget with QopenGLwidget:

    You have to make a function that maps between Widget x,y to openGl

    Do You have any Idea,the way to map coordinates.



  • How Can I find coordinates of openGLwidget when mouse press.


  • Lifetime Qt Champion

    Hi
    Sorry. i have no good idea how to convert in any easy way.

    By mouse Coordinates you mean in Widgets coordinates or as
    actual openGl coordinates?



  • @mrjj said in QWidget with QopenGLwidget:

    By mouse Coordinates you mean in Widgets coordinates or as
    actual openGl coordinates?

    Actual OpenGl coordinates.


  • Lifetime Qt Champion

    @hjohn
    I do not think openGLwidget directly offer that.
    I tried to google for it but just too much deprecated fixed pipeline
    openGl so im not sure how you would do it.

    This would be very easy to do with QPainter but i guess you want
    openGl for a reason ?



  • @mrjj yeah..
    No worry.. If I come up with any solution,I will mention here. Thanks for guiding me..


Log in to reply