为什么我不能在第一次点击按钮时在Qt Widget上绘制点
我想在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 Vision:5.1.1
我不太清楚,但是对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)));
}
假设信号函数是在头文件中声明的。
希望能帮助到你。
链接地址: http://www.djcxy.com/p/39299.html上一篇: why cant I draw points on the Qt Widget at the first time I click the button
下一篇: Cannot setup Inherited class's method as slot for click signal