c++qtopenglqglwidget

how qglwidget interacts with with other widgets in the main window


Basically I would like to show pressed buttons on Text Edit widget. The Key event is working for GLWidget. How can I interact with widgets of MainWindow from GLWidget? This is my code so far,

mainwindow.cpp

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

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

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

void MainWindow::on_startButton_clicked()
{

}

glwidget.h

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QTimer>
#include <QtOpenGL/QGLWidget>
#include <gl/GLU.h>
#include <gl/GL.h>
#include <QKeyEvent>


class GLWidget : public QGLWidget
{
    Q_OBJECT

public:
    explicit GLWidget(QWidget *parent = 0);
    ~GLWidget();

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

    //--------------( Key Event )-----------------//
    void keyPressEvent(QKeyEvent *event);
    void keyReleaseEvent(QKeyEvent *event);

private:
    QTimer timer;

};

#endif // GLWIDGET_H

glwidget.cpp

.
.
.    
void GLWidget::keyPressEvent(QKeyEvent *event)
{
   qDebug() << event->text() << " is pressed ...";
}

void GLWidget::keyReleaseEvent(QKeyEvent * event)
{
    qDebug() << event->text() << " is released ...";

     //ui->textEdit->setText("hhh"); <- ????????????????

}

Solution

  • You can do it just like any other widget.

    Put a signal in your GLWidget that will be emitted when the key is released

    class GLWidget : public QGLWidget
    {
        ....
    signals:
        void textChanged(QString text); 
    }
    
    void GLWidget::keyReleaseEvent(QKeyEvent * event)
    {
        qDebug() << event->text() << " is released ...";
        emit textChanged(event->text());
    }
    

    and connect this signal to your textEdit slot setText(const QString & text).