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

GLwidget GraphicScene size problem



  • I at all, I revised my old app in Qt4.8 for upgrade it to 5.12 on linux.

    in my MainWindows i have a GlWidget for render an opencv video.

               ui->glwidget->setGeometry(450, 20, 450, 400);
    
               QRectF MatrixRect(QPoint(450, 20), QSize(450, 400));
    
               sceneMatrix = new QGraphicsScene(this);
               sceneMatrix->setSceneRect(MatrixRect);
    

    I have subclass QGLwidget in these manner (heather):

    #include .....
    #include <QPaintDevice>
    #include <QPainter>
    
    
    class glwidg : public QGLWidget
    {
        Q_OBJECT
    
    public:
        explicit glwidg(QWidget *parent = 0);
        static cv::Point SendMousePoint;
        static int xsend;
        static int ysend;
    
    
    
    
    
    
    signals:
        void imma_orig(QImage qi1);
        void MousePoint(cv::Point MousePt);
    
    
    
    
        
    public slots:
    
       // void camera01(QImage camera00);
         //void sendImage(cv::Mat* img);
         //void Mat22QImage(cv::Mat imma);
         void donextcam();
         void imma_orig_r1(QImage qi1);
         void stopcam1(bool st1);
    
    
    
    
    private:
         QImage qqframe;
         QImage qq1frame;
         QImage qq2frame;
         QTimer *tim;
         static int xAtPress, yAtPress;
    
    
    protected:
        void resizeGL(int w, int h);
        void initializeGL();
        void paintGL();
        void updateScene();
        void renderImage();
        void mouseMoveEvent(QMouseEvent * event);
        
    };
    
    #endif // GLWIDG_H
    
    

    And cpp file:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include "glwidg.h"
    
    #include <QThread>
    #include <QtCore>
    #include <QtConcurrent/QtConcurrent>
    #include <QtConcurrent/QtConcurrentRun>
    #include <QFuture>
    #include <QFutureWatcher>
    #include <QtWidgets>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QWidget>
    #include <QtWidgets/QWidgetAction>
    #include <QtWidgetsDepends>
    #include <QtWidgetsVersion>
    #include <QTableWidget>
    #include <QTableWidgetItem>
    #include <QTableWidgetSelectionRange>
    #include <QTableView>
    
    #include <opencv2/opencv.hpp>
    #include <opencv2/highgui.hpp>
    #include <iostream>
    #include <math.h>
    #include <stdio.h>
    #include <opencv2/opencv.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/core/core.hpp>
    #include <sstream>
    #include <string.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <GL/freeglut.h>
    #include <QtOpenGL/QGLWidget>
    //#include <QtGui>
    #include <QtOpenGL/QGLBuffer>
    #include <QtOpenGL/QtOpenGL>
    #include <QTime>
    #include <QTimer>
    #include <QDebug>
    
    
    QImage camera000;
    GLuint texture[1];
    QImage GL_formatt;
    QImage qform;
    QImage qform1;
    int cambio1 = 0;
    
    //cv::Mat imma;
    cv::Mat imma2;
    cv::Mat imma3;
    
    int play = 0;
    
    
    glwidg::glwidg(QWidget *parent) :
        QGLWidget(parent)
    
    {
    
        this->setAttribute(Qt::WA_DeleteOnClose);
    
        QGLWidget(QGLFormat(QGL::DirectRendering));
        setMaximumSize(480,360);//(352,288);
        setMinimumSize(480,360);
    
    
        initializeGL();
        //resizeGL(480,360);
        paintGL();
    
    
    }
    
    int glwidg::xAtPress = 0;
    int glwidg::yAtPress = 0;
    int glwidg::xsend = 0;
    int glwidg::ysend = 0;
    cv::Point glwidg::SendMousePoint(0,0);
    
    
    void glwidg::mouseMoveEvent(QMouseEvent * event)
    {
    
    
        xAtPress = event->x();
        yAtPress = event->y();
        xsend = round(xAtPress*1.422222222);
        ysend = round(yAtPress*1.2);
       /* if (xsend < 0) {xsend = 0;}
        if (xsend > 640) {xsend = 640;}
        if (ysend < 0) {ysend = 0;}
        if (ysend > 480) {ysend = 480;}*/
        SendMousePoint = cv::Point(xsend, ysend);
        emit MousePoint(SendMousePoint);
        //qDebug() << "Mausepoint glwidg: " << SendMousePoint.x << ", " << SendMousePoint.y;
    }
    
    
    void glwidg::resizeGL(int w, int h)
    {
    
        makeCurrent();
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
    
    }
    
    
    void glwidg::initializeGL()
    {
        makeCurrent();
        glClearDepth(2000.0);
        glDisable(GL_TEXTURE_2D);
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_COLOR_MATERIAL);
        glEnable(GL_BLEND);
        glEnable(GL_POLYGON_SMOOTH);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        qglClearColor(Qt::white);
    
    
    
    }
    
    void glwidg::paintGL()
    {
        makeCurrent();
    
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        qglColor(Qt::white);
    
    
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
        glMatrixMode(GL_MODELVIEW); /*+*/
        glEnable(GL_DEPTH_TEST);
    
    
        if (cambio1 == 0)
        {
            qform1.load("/home/myImage.png");
            qq1frame = qform1.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        }
        else
        {
            qq1frame = qform1.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        }
    
    
        //qq1frame.load("/home/myImage.png");
        qqframe = qq1frame.mirrored(false, true);
    
        if(!qqframe.isNull())
        {
            qqframe = qqframe.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
           // glDrawPixels(qqframe.width(), qqframe.height(), GL_RGBA, GL_UNSIGNED_BYTE, qqframe.bits());
    
            // or do 2D texture mapping
            glMatrixMode(GL_MODELVIEW); /**/
            glEnable(GL_DEPTH_TEST);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluOrtho2D(0,qqframe.width(),qqframe.height(),0);
            glMatrixMode(GL_MODELVIEW);
            //glDisable(GL_DEPTH_TEST);
            glLoadIdentity();
            glEnable(GL_TEXTURE_2D);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexImage2D( GL_TEXTURE_2D, 0, 4, qqframe.width(), qqframe.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, qqframe.bits() );
            glBegin(GL_QUADS);
            glTexCoord2f(0,0); glVertex2f(0,qqframe.height());
            glTexCoord2f(0,1); glVertex2f(0,0);
            glTexCoord2f(1,1); glVertex2f(qqframe.width(),0);
            glTexCoord2f(1,0); glVertex2f(qqframe.width(),qqframe.height());
            glEnd();
            glDisable(GL_TEXTURE_2D);
            // .... end
    
           // qDebug() << ("ho disegnato??");
            glDisable(GL_DEPTH_TEST);
    
    
            glFlush();
    
         }
    
        if(qqframe.isNull())
        {
            glClear(GL_COLOR_BUFFER_BIT);
            qglColor(Qt::white);
        }
    
        qDebug() << "x:   " << qqframe.width() << "y:   " << qqframe.height();
    
    /* AT THESE POINT.... FIRST TIME I SEE RIGHT SIZE OF IMAGE 480*360 .... AFTER THESE I SEE ONLY 100*30 .... TOO LITTLE */
    
    }
    
    
    void glwidg::donextcam()
    {
    
        makeCurrent();
    
    
    }
    
    
    void glwidg::imma_orig_r1(QImage qi1)
    {
        cambio1 = 1;
        qform1 = qi1;
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        updateGL();
    
    }
    
    void glwidg::stopcam1(bool st1)
    {
        if (st1)
        {
            cambio1 = 0;
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            updateGL();
        }
        else
        {
            cambio1 = 1;
        }
    }
    
    

    /* AT THESE POINT.... FIRST TIME I SEE RIGHT SIZE OF IMAGE 480360 .... AFTER THESE I SEE ONLY 10030 .... TOO LITTLE */

    These my problem .... sems that scene become real screen of GLwidget so it haa automatic resize .... but not know why.

    Regards
    gfxx


  • Lifetime Qt Champion

    Hi,

    Why are you calling initializeGL and paintGL from your constructor ? These are called automatically when appropriate by Qt.



  • @SGaist Really because in old Qt4.8 version it work .... witouth not work. But just now I try your suggest and obtain only X=100 and Y=30 instead 480x360 as in the past ..... I'm a little confused about ....



  • @gfxx Ok I try to convert my code in more new one using QOpenGLWidget isnstead of QGLWidget ....



  • @gfxx Problem is Portability related ... when change my class into QOpenGLwidget from QGLWidget all work fine.

    regards


Log in to reply