基本上我想在Text Edit
小部件上显示按下的按钮。Key事件正在为GLWidget工作。如何从GLWidget
中与MainWindow
的小部件交互?这是目前为止的代码,
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"); <- ????????????????
}
您可以像使用其他小部件一样使用它。
在你的GLWidget
中放一个信号,当键被释放时发出
class GLWidget : public QGLWidget
{
....
signals:
void textChanged(QString text);
}
void GLWidget::keyReleaseEvent(QKeyEvent * event)
{
qDebug() << event->text() << " is released ...";
emit textChanged(event->text());
}
并将此信号连接到您的textEdit插槽setText(const QString & text)
。