QObject :: connect:没有此类信号:当connect无法识别该信号时,如何子类化并连接用户定义的插槽?

时间:2019-02-11 16:24:39

标签: qt widget signals subclassing slot

Qt5无法识别在我的glwidget.h标头中定义的信号,该标头是QOpenGLWidget的子类。

我已将类的声明更改为继承QObject而不是QWidget,但是如果不是从QWidget继承的,则在设计器中创建然后提升为子类的glwidget不会显示。

//////////////
//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()));
}

子类鼠标事件正确注册,但是即使运行clean,qmake和build,应用程序也会输出:

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

所需的连接信号未被识别和注册。

1 个答案:

答案 0 :(得分:0)

“应该是QPoint而不是Qpoint”-已解决