Drawing rectangle by mouse shows weird shape

I am using the Qt framework to handle mouse events and other window options.
Also, I’m using OpenGL version 4.5 with nVidia 361.42 in Ubuntu 16.04.
This is my code so far:


void oglCanvas::initializeGL()
{
    initializeOpenGLFunctions();

    // setting matrices
    view.lookAt( // top view
                QVector3D(0.0, 0.0, 5.0),
                QVector3D(0.0, 0.0, 0.0),
                QVector3D(0.0, 1.0, 0.0));
    model.setToIdentity();

    // background color
    glClearColor(0.2, 0.2, 0.2, 1);

    initShaders();
    initTextures();

    // Enable back face culling
    glEnable(GL_CULL_FACE);

    shapes = new VertexEngine;
}

void oglCanvas::resizeGL(int w, int h)
{
    // Calculate aspect ratio
    qreal aspect = qreal(w) / qreal(h ? h : 1);

    // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
    const qreal zNear = 1.0, zFar = 100.0, fov = 45.0;
    // near-z far-z specifies how much your scene is projecting
    // it's like the depth your camera can reach [zh-tw: ru/3gp ]
    // set near z to 1.0f so mouse coordinates can meet the original size

    // Reset projection
    projection.setToIdentity();

    // Set perspective projection
    projection.perspective(fov, aspect, zNear, zFar);
}

void oglCanvas::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    texture->bind();

    // send the mvp matrix to GPU
    program.setUniformValue("mvp_matrix", projection * view * model);

    // use texture unit 0
    program.setUniformValue("texture", 0);

    // shapes->drawCube(&program);
    shapes->draw(&program);
}


void oglCanvas::mousePressEvent(QMouseEvent *event)
{
    float thickness = 0.16, zPos = 0; // in model coordinates
    QVector3D coord1_0, coord1_1, coord2_0, coord2_1;

    if (event->button() == Qt::LeftButton) {
        setMouseTracking(true);
        coord1 = coord2;
        //coord2 = toGLCoord(event->x(), event->y());
        coord2.setX(event->x());
        coord2.setY((event->y()));
        //coord2.setZ(zPos);
        if (coord1.x()!=0 && coord1.y()!=0) {
            qDebug()<< coord1.x() << coord1.y() <<" : "<< coord2.x()<< coord2.y();
            // transform to model coordinate system before passing
            coord1.setZ(0.0);
            coord1_0 = coord1.unproject(view, projection, this->geometry());
            coord2.setZ(0.0);
            coord2_0 = coord2.unproject(view, projection, this->geometry());

            coord1.setZ(1.0);
            coord1_1 = coord1.unproject(view, projection, this->geometry());
            coord2.setZ(1.0);
            coord2_1 = coord2.unproject(view, projection, this->geometry());

            coord1 = coord1_0 + 5/(100.0-0.1)*(coord1_1 - coord1_0);
            coord2 = coord2_0 + 5/(100.0-0.1)*(coord2_1 - coord2_0);

            qDebug()<< coord1.x() << coord1.y() << coord1.z() <<" : "<< coord2.x()<< coord2.y() << coord2.z();
            shapes->addRect(coord1, coord2, zPos, zPos + thickness);
            coord2 = QVector3D(0,0,0);
            setMouseTracking(false);
        }
    }
}


void VertexEngine::addRect(QVector3D p1, QVector3D p3, float zstart, float zstop)
{
    QVector3D center = (p1 + p3)/2;
    QVector3D p2 = QVector3D(p3.x(), p1.y(), 0);
    QVector3D p4 = QVector3D(p1.x(), p3.y(), 0);
    center.setZ(zstart);
    p1.setZ(zstart);
    p2.setZ(zstart);
    p3.setZ(zstart);
    p4.setZ(zstart);
    TexturedVertex temp_vertex[] = {
        {p1, QVector2D(0.0f, 1.0f)},
        {p2, QVector2D(0.0f, 0.0f)},
        {p3, QVector2D(1.0f, 0.0f)},
        {p4, QVector2D(1.0f, 1.0f)}
    };
    GLushort ind[4] = {
        0, 1, 2, 3
    };

    std::copy(temp_vertex,temp_vertex+3,vertex);
    std::copy(ind,ind+4,index);

    updateBuffer();
    qDebug()<< "rectangle created";
}

void VertexEngine::draw(QOpenGLShaderProgram *program)
{
    arrBuf.bind();
    indBuf.bind();

    qintptr offset = 0;

    program->enableAttributeArray(0); // a_position
    program->setAttributeBuffer(0, GL_FLOAT, offset, 3, sizeof(TexturedVertex));

    offset += sizeof(QVector3D);

    program->enableAttributeArray(1); // a_texcoord
    program->setAttributeBuffer(1, GL_FLOAT, offset, 2, sizeof(TexturedVertex));

    GLsizei count[] = {sizeof(vertex)};
    GLvoid* index_test[] = {
        BUFFER_OFFSET(0)
    };

    //! crashed because of some weird pointer issue
    //glMultiDrawElements( GL_POLYGON, count, GL_UNSIGNED_SHORT, index_test, sizeof(count) );
    glDrawElements(GL_POLYGON, sizeof(index), GL_UNSIGNED_SHORT, 0);
    //qDebug()<< "elements drawed";

    /*for (int i = 0; i < primcount; ++i) {
        glDrawElements(GL_TRIANGLE_FAN, count[i], GL_UNSIGNED_SHORT, index[i]);
    }*/
}

void VertexEngine::updateBuffer()
{
    arrBuf.bind();
    arrBuf.allocate(&vertex, sizeof(vertex) * sizeof(TexturedVertex));
    indBuf.bind();
    indBuf.allocate(&index, sizeof(index) * sizeof(GLushort));
    qDebug()<< "buffer set!";
}

As you can see above, I constructed my rectangle using a polygon in CCW order, but most of the times the shapes didn’t end up at my cursor :doh:
The editor keep giving me an error “not a valid image file” so I can’t post images right now.
can someone tell me what might be the cause?