简体   繁体   English

为什么我第一次单击按钮时无法在Qt小部件上绘制点

[英]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. 我想在Widget上画一些点,所以我将Widget提升为类。 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, 这是课程的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;
            }
        }
    }
}

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 GLWidget::receiveMatandImage(cv::Mat xyz, cv::Size imageSize, \\ cv::Mat rviewLeft)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);
}

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. 问题是,当我第一次单击按钮(按钮4)时,小部件中什么都没有显示,但是当我第二次单击按钮时,点显示得很好。

the first time I click the button, 我第一次单击该按钮时, 在此处输入图片说明

the second time I click the button, 第二次单击按钮时, 在此处输入图片说明

Qt Vision : 5.1.1 Qt视觉: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 . 我不太清楚,但可能调用QObject::connect()去异步长,当你呼叫尚未finised emit

If appropriate, try to put the QObject::connect() into the constructor, something like this: 如果合适,尝试将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)));
}  

asuming that the signal function was declared in the the header file. 假设在头文件中声明了信号功能。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM