QObject::connect:没有这样的信号:当连接无法识别信号时,如何子类化和连接用户定义的插槽?
QObject::connect: No such signal : How do I subclass and connect a user defined slot when connect does not recognize the signal?
Qt5 无法识别我的 glwidget.h header 中定义的信号,它是 QOpenGLWidget 的子class。
我已经更改了class的声明以继承QObject而不是QWidget,但是在设计器中创建然后提升为subclass的glwidget,如果不是,则不显示继承自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()));
}
subclassed 鼠标事件正确注册,但即使在 clean、qmake 和 build 为 运行 时,应用程序输出:
QObject::connect: No such signal glwidget::sendMousePosition(Qpoint&)
QObject::connect: (sender name: 'openGLWidget_1')
QObject::connect: (receiver name: 'MainWindow')
所需的连接信号未被识别和注册。
'It should be QPoint instead of Qpoint' - 已解决
Qt5 无法识别我的 glwidget.h header 中定义的信号,它是 QOpenGLWidget 的子class。
我已经更改了class的声明以继承QObject而不是QWidget,但是在设计器中创建然后提升为subclass的glwidget,如果不是,则不显示继承自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()));
}
subclassed 鼠标事件正确注册,但即使在 clean、qmake 和 build 为 运行 时,应用程序输出:
QObject::connect: No such signal glwidget::sendMousePosition(Qpoint&)
QObject::connect: (sender name: 'openGLWidget_1')
QObject::connect: (receiver name: 'MainWindow')
所需的连接信号未被识别和注册。
'It should be QPoint instead of Qpoint' - 已解决