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

Send real time data from another class to MainWindow Label



  • Hello!, I need help to send data from my OpenGLWidget class to a label in my MainWindow class, here is my code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    //MainWindow.cpp
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    MyOpenGLWidget *a = new MyOpenGLWidget;
    
    connect(a, SIGNAL(textChanged(QString)),ui->lb_ax, SLOT(setText(QString)));
    
    a->someFunctionThatChangesText("");
    
    QApplication::processEvents();
    QWidget::repaint();
    
    update();
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    //MainWindow.h
    #ifndef MYOPENGLWIDGET_H
    #define MYOPENGLWIDGET_H

    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <GL/gl.h>
    #include <mainwindow.h>
    #include <QObject>
    #include <signal.h>

    class MyOpenGLWidget : public QOpenGLWidget, public QOpenGLFunctions
    {
    Q_OBJECT
    public:
    MyOpenGLWidget(QWidget *parent = nullptr);
    float tiempo = 0;

    signals:
    void textChanged(QString);
    public:
    void someFunctionThatChangesText(const QString& newtext);

    public slots:
    void funcionActivacionTimer();

    protected:
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int w, int h) override;

    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
    

    };

    #endif // MYOPENGLWIDGET_H

    //MyOpenglGlWidget.cpp
    #include "myopenglwidget.h"
    #include <GL/gl.h>
    #include <QDebug>
    #include <math.h>
    #include <QMouseEvent>
    #include <mainwindow.h>
    #include <ui_mainwindow.h>
    #include <QTimer>

    float xM=0;
    float yM=0;
    float w=0; //Variable que guarda el ancho del OpenGL widget.
    float h=0; //Variable que guarda el alto del OpenGL widget.

    float m=20;
    float k=20;
    float a=0;
    float x=0;

    float A=0;

    float xP=0;
    float t=0;

    int cont=0; //Almacena el mismo valor que el contador.

    bool m_is_dragging=false; //Variable boleaana que indica si se clickea el mouse.
    bool act=false;

    MyOpenGLWidget::MyOpenGLWidget(QWidget *parent)
    : QOpenGLWidget {parent}
    {
    QTimer *cronometro=new QTimer(this);
    connect(cronometro, SIGNAL(timeout()), this, SLOT(funcionActivacionTimer()));
    cronometro->start(50); //Se actualiza cada 50 milisegundos.
    }

    void MyOpenGLWidget::funcionActivacionTimer(){
    if(t<=20 && m_is_dragging==false && act==true){
    tiempo+=0.1f;
    t=tiempo;
    }if(t==20){
    t=0;
    act=false;
    }
    }

    void MyOpenGLWidget::mousePressEvent(QMouseEvent *event){ //Función que detecta si se ha o esta clickeando el mouse (hace a esa variable true).

    if(event->button() == Qt::LeftButton){ //Esta variable boleana se hace true si se mantiene apretado el click izquierdo del mouse.
        m_is_dragging = true;
        act=true;
    }
    

    }

    void MyOpenGLWidget::mouseMoveEvent(QMouseEvent event){ //Se obtine el ancho y el alto del openGL widget (w,h).
    if(m_is_dragging==true){
    A=(event->pos().rx())
    (10/w)-5;
    }
    }

    void MyOpenGLWidget::mouseReleaseEvent(QMouseEvent *event){ //Función que detecta si se ha dejado clickear el mouse (hace a esa variable false).
    m_is_dragging = false; //Esta variable boleana se hace false si se no esta apretado el click izquierdo del mouse.
    }

    void caja(float l, float x, float y){
    glPushMatrix();
    glTranslatef(x,y,0);
    if(m_is_dragging==true){
    glTranslatef(xM-4,0,0);
    }else if(m_is_dragging==false){
    glTranslatef(xP,0,0);
    }
    glColor3f(1,0,0);
    glBegin(GL_POLYGON);
    glVertex2f((-l/2),(l/2));
    glVertex2f((-l/2),(-l/2));
    glVertex2f((l/2),(-l/2));
    glVertex2f((l/2),(l/2));
    glEnd();
    glPopMatrix();
    }

    void posicionDeEquilibrio(void){

    glColor3f(0,0,0);
    glBegin(GL_LINES);
    for(float i=0.25; i<=10; i+=0.5){
        glVertex2f(5,i-0.25);
        glVertex2f(5,i);
    }
    glEnd();
    

    }

    void MyOpenGLWidget::initializeGL(){
    initializeOpenGLFunctions();
    glClearColor(1,1,1,1);
    w = MyOpenGLWidget::width();
    h = MyOpenGLWidget::height();
    }

    void calculos(){
    a = (kabs(xM-5))/(m);
    if(A<0){
    xP = A
    cos(sqrt((k/m))t+M_PI);
    }else if(A>0){
    xP = A
    cos(sqrt((k/m))*t);
    }
    }

    void MyOpenGLWidget::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QPoint p = mapFromGlobal(QCursor::pos());
    xM = (p.rx())*(10/w); //Se ontiene la posiciób en X del cursor y se convierte a coordenadas ortogonales.
    yM = ((p.ry())*(20/h) -10)-8; //Se ontiene la posiciób en Y del cursor y se convierte a coordenadas ortogonales.
    
    //qDebug() << xP;
    //qDebug() << t << A << xP;
    
    calculos();
    
    glColor3f(0,0,0);
    glBegin(GL_LINE_STRIP);
    glVertex2f(0.01,5);
    glVertex2f(0.01,3);
    glVertex2f(10,3);
    glEnd();
    
    
    
    glPushMatrix();
    if(m_is_dragging==true){
        //glTranslatef(xM,0,0);
        glScalef(xM/4,1,0);
    }else if(m_is_dragging==false and act==true){
        glScalef((xP+5)/5,1,0);
    }
    
    glBegin(GL_LINE_STRIP);
    //glVertex2f(-4,2);
    //glVertex2f(4,2);
    for(float i=0; i<5; i+=0.05){
        glVertex2f(i,sin(12*i)+4);
    }
    glEnd();
    glPopMatrix();
    
    caja(2,5,4);
    
    posicionDeEquilibrio();
    
    update();
    

    }
    void MyOpenGLWidget::resizeGL(int w, int h){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 10, 0, 10, 0, 10);
    }

    void MyOpenGLWidget::someFunctionThatChangesText(const QString& newtext){
    emit textChanged(QString::number(tiempo));
    qDebug()<<tiempo;
    }

    //MyOpenGLWidget.h
    #ifndef MYOPENGLWIDGET_H
    #define MYOPENGLWIDGET_H

    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <GL/gl.h>
    #include <mainwindow.h>
    #include <QObject>
    #include <signal.h>

    class MyOpenGLWidget : public QOpenGLWidget, public QOpenGLFunctions
    {
    Q_OBJECT
    public:
    MyOpenGLWidget(QWidget *parent = nullptr);
    float tiempo = 0;

    signals:
    void textChanged(QString);
    public:
    void someFunctionThatChangesText(const QString& newtext);

    public slots:
    void funcionActivacionTimer();

    protected:
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int w, int h) override;

    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
    

    };

    #endif // MYOPENGLWIDGET_H

    ------------------------------------------------>

    I´m think that the problem is in this function

    void MyOpenGLWidget::someFunctionThatChangesText(const QString& newtext){
    emit textChanged(QString::number(tiempo));
    qDebug()<<tiempo;
    }

    I want to know how to update tha var "Tiempo" because I need to send in real time that data to the la label lb_ax in MainWindow, i think that someFunctionThatChangesText is not updating the emit, some idea please????


  • Lifetime Qt Champion

    Hi
    emit rarely fails so i would check
    bool result = connect(a, SIGNAL(textChanged(QString)),ui->lb_ax, SLOT(setText(QString)));
    qDebug()<<"conn;" << result;

    Since you do
    MyOpenGLWidget *a = new MyOpenGLWidget;
    connect(a, SIGNAL(textChanged(QString)),ui->lb_ax, SLOT(setText(QString)));
    a->someFunctionThatChangesText("");

    It should be the MyOpenGLWidget you think it is, which else is a classic error.

    Last thing that can be wrong, it that you might somehow block the event loop as you inserted
    QApplication::processEvents();
    QWidget::repaint();
    which should not be needed.


Log in to reply