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')
所需的连接信号未被识别和注册。
答案 0 :(得分:0)
“应该是QPoint而不是Qpoint”-已解决