QObject::connect: No such signal GLWidget::textChanged
-
I'm moving from GLUT to Qt. I've implemented simple projects with
QGLWidget
. Now I would like to interact with other widgets inMainWindow
. My problem is that howQGLWidget
interacts withMainWindow
? I'm implemented the following sample but I get this error
QObject::connect: No such signal GLWidget::textChanged(const QString &text) in ..\KeyEvent\mainwindow.cpp:12 QObject::connect: (receiver name: 'textEdit')
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class GLWidget; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_startButton_clicked(); private: Ui::MainWindow *ui; GLWidget *glwidget; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); glwidget = new GLWidget; connect( glwidget, SIGNAL(textChanged(const QString &text)), ui->textEdit, SLOT( setText(const QString &text)) ); //connect(ui->startButton, SIGNAL(clicked()), // glwidget, SLOT( print()) ); } 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); signals: void textChanged(const QString &text); public slots: void print(); private: QTimer timer; }; #endif // GLWIDGET_H
glwidget.cpp
#include "glwidget.h" #include <QDebug> GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
QGLFormat glFormat; glFormat.setDoubleBuffer(true); glFormat.setDepth(true); setFormat(glFormat); connect(&timer, SIGNAL(timeout()), this, SLOT(updateGL())); timer.start(10); } GLWidget::~GLWidget() { } void GLWidget::initializeGL() { glClearColor(0,0,0,1); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); } void GLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glRotatef(0.5, 0, 1, 0); glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); glVertex3f(-0.5, -0.5, 0); glColor3f(0.0, 1.0, 0.0); glVertex3f( 0.5, -0.5, 0); glColor3f(0.0, 0.0, 1.0); glVertex3f( 0.0, 0.5, 0); glEnd(); } void GLWidget::resizeGL(int w, int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, (float)w/h, 0.01, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,5,0,0,0,0,1,0); } void GLWidget::keyPressEvent(QKeyEvent *event) { qDebug() << event->text() << " is pressed ..."; emit textChanged(event->text()); update(); } void GLWidget::keyReleaseEvent(QKeyEvent * event) { qDebug() << event->text() << " is released ..."; } void GLWidget::print() { qDebug() << "print()"; update(); }
-
Hi,
You connect statement is wrong. It should be:
connect(glwidget, SIGNAL(textChanged(QString)), ui->textEdit, SLOT(setText(QString)));
Never put the variable name in the SIGNAL or SLOT macros