why cant I draw points on the Qt Widget at the first time I click the button
I wanna draw some points on the Widget, so I promote the Widget to class. And the points' informations(eg,position, color, quantity) need to be acquired from outside the class, so I set a slots to receive the informations. Before acquiring the quantity, I set it to zero.
Here is the class' cpp file,
#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;
}
}
}
}
the line void GLWidget::receiveMatandImage(cv::Mat xyz, cv::Size imageSize, cv::Mat rviewLeft)
is the slot that receive the points informations, and the corresponding signal is from this part,
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);
}
the problem is when I click the button(button 4) at the first time, nothing is displayed in the widget, but when I click the button at the second time, the points are displayed very well.
the first time I click the button,
the second time I click the button,
Qt Vision : 5.1.1
I am not quite sure, but probably the call to QObject::connect()
goes asynchronously long and is not yet finised when you call emit
.
If appropriate, try to put the QObject::connect()
into the constructor, something like this:
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)));
}
asuming that the signal function was declared in the the header file.
Hope it helps.
链接地址: http://www.djcxy.com/p/39300.html