Search code examples
qtwidgetsignalssubclassingslot

QObject::connect: No such signal : How do I subclass and connect a user defined slot when connect does not recognize the signal?


Qt5 does not recognize the signal defined in my glwidget.h header, which is a subclass of QOpenGLWidget.

I have changed the declaration of the class to inherit QObject rather than QWidget, however the glwidget, created in the designer and then promoted to subclass, does not display if it is not inherited from QWidget.

//////////////
//glwidget.h//
//////////////

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QObject>
#include <QWidget>
#include <QOpenGLWidget>
#include <QMouseEvent>

class glwidget : public QOpenGLWidget
{

    Q_OBJECT

public:
    explicit glwidget(QWidget *parent = nullptr);

protected:
    void mouseMoveEvent(QMouseEvent *mouse_event);

signals:
    void sendMousePosition(QPoint& pos);

};

#endif // GLWIDGET_H
////////////////
//glwidget.cpp//
////////////////

#include "glwidget.h"

glwidget::glwidget(QWidget *parent) : QOpenGLWidget(parent)
{

    this->setMouseTracking(true);

}


void glwidget::mouseMoveEvent(QMouseEvent *mouse_event)
{
    QPoint mouse_pos = mouse_event->pos();

    if(mouse_pos.x() <= this->size().width() && \
       mouse_pos.y() <= this->size().height())
    {
        if (mouse_pos.x() >= 0 && mouse_pos.y() >= 0)
        {

            emit sendMousePosition(mouse_pos);

        }
    }
}
//////////////////
//mainwindow.cpp//
//////////////////

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

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), 
     ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QObject::connect(ui->openGLWidget_1,SIGNAL(sendMousePosition(Qpoint&)), \ 
                     this,SLOT(showMousePosition(QPoint&)));
}

void MainWindow::showMousePosition(QPoint &pos)
{
    ui->mouse_position_label->setText("x: " + QString::number(pos.x()) + \
                                      ", y: " +  QString::number(pos.y()));
}

The subclassed mouse events register properly, however even when clean, qmake, and build are run, the application outputs:

QObject::connect: No such signal glwidget::sendMousePosition(Qpoint&)
QObject::connect:  (sender name:   'openGLWidget_1')
QObject::connect:  (receiver name: 'MainWindow')

The desired connect signal is not recognized and registered.


Solution

  • 'It should be QPoint instead of Qpoint' - solved