我想在Widget上画一些点,所以我将Widget推广到了课堂。并且需要从类外部获取点的信息(例如,位置,颜色,数量),因此我设置了一个槽来接收信息。在获取数量之前,我将其设置为零。
这是类'cpp文件,
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
pointClouds = NULL; //[x y z]
imageRGB = NULL; //[R G B]
oldx = -1, oldy = -1;
c = (float)(CV_PI / 180); theta = 0,
rho = 0; zoom = 1;
height = 0; width = 0;
connect(&timer, SIGNAL(timeout()), this, SLOT(updateGL()));
timer.start(16);
}
void GLWidget::initializeGL()
{
glClearColor((GLclampf)0, (GLclampf)0, \
(GLclampf)0, (GLclampf)1);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE\
| GLUT_RGBA);
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, 0, -1, 0);
glScalef((GLfloat)zoom, (GLfloat)zoom, (GLfloat)zoom);
glPointSize(1.0);
glBegin(GL_POINTS);
for (int i = 0; i < height*width*3; i+=3)
{
glColor3f(imageRGB[i], imageRGB[i + 1], imageRGB[i + 2]);
glVertex3f(pointClouds[i], pointClouds[i + 1],
pointClouds[i + 2]);
}
glEnd();
glFlush();
}
void GLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}
void GLWidget::mousePressEvent(QMouseEvent *)
{
}
void GLWidget::mouseMoveEvent(QMouseEvent *)
{
}
void GLWidget::receiveMatandImage(cv::Mat xyz, cv::Size imageSize, \
cv::Mat rviewLeft)
{
width = imageSize.width; height = imageSize.height;
pointClouds = new float[height*width*3];
int number = 0;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
cv::Vec3f points = xyz.at<cv::Vec3f>(i,j);
pointClouds[3 * (i*width + j)] = points[0]; //x
pointClouds[3 * (i*width + j) + 1] = points[1]; //y
pointClouds[3 * (i*width + j) + 2] = points[2]; //z
if (points[2] <= 9999 && points[2]>0) //discard mismatched points
{
number += 1;
centerx += points[0]; centery += points[1];
centerz += points[2];
}
}
}
centerx /= number; centery /= number; centerz /= number;
eyez0 = -10.0f; eyex0 = centerx; eyey0 = centery;
r = centerz - eyez0; eyex = eyex0; eyey = eyey0;
eyez = eyez0;
/*save the RGB Info*/
rviewLeft.convertTo(rviewLeft, CV_8UC3);
imageRGB = new float[height*width * 3];
for (int i = 0; i < height; i++)
{
const uchar* ptr = rviewLeft.ptr<uchar>(i);
for (int j = 0; j < width * 3; j++)
{
int tempIdx = i*width * 3 + j;
switch (tempIdx % 3)
{
case 0:
imageRGB[tempIdx] = ptr[j + 2] / 255.0f;
break;
case 1:
imageRGB[tempIdx] = ptr[j] / 255.0f;
break;
case 2:
imageRGB[tempIdx] = ptr[j - 2] / 255.0f;
break;
default:
break;
}
}
}
}
行void GLWidget::receiveMatandImage(cv::Mat xyz, cv::Size imageSize, \
cv::Mat rviewLeft)
是接收点信息的插槽,相应的信号来自此部分,
void MainWindow::on_pushButton_4_clicked()
{
t = time(0);
localtime_s(&timeInfo, &t);
asctime_s(timeNow,&timeInfo);
len = strlen(timeNow);
timeNow[len-1] = 0;
ui->textBrowser->append(timeNow);
ui->textBrowser->append("computing the points cloud...");
cv::reprojectImageTo3D(disp, xyz, Q, true);
ui->textBrowser->append("computing completed");
connect(this, SIGNAL(sendMatandImage(cv::Mat,cv::Size,cv::Mat)), \
ui->widget, SLOT(receiveMatandImage(cv::Mat,cv::Size,cv::Mat)));
emit sendMatandImage(xyz, imageSize, rviewLeft);
}
问题是当我第一次点击按钮(按钮4)时,小部件中没有显示任何内容,但是当我第二次点击按钮时,这些点显示得非常好。
Qt愿景:5.1.1
答案 0 :(得分:0)
我不太确定,但可能会拨打QObject::connect()
当你拨打emit
时,异步长并且尚未完成。
如果合适,尝试将QObject::connect()
放入构造函数中,如下所示:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//...
connect(this, SIGNAL(sendMatandImage(cv::Mat,cv::Size,cv::Mat)), \
ui->widget, SLOT(receiveMatandImage(cv::Mat,cv::Size,cv::Mat)));
}
假设信号函数已在头文件中声明。
希望它有所帮助。