Search code examples
c++qtqlist

Access List created in GlWidget in Mainwindow to write a file


I am a novice in programming. Probably, this question is very simple to answer. I am generating a list for points in glwidget.cpp:

void GLWidget::setPoint(double px, double py, double pz)
{
          pc[0] = px;
          pc[1] = py;
          pc[2] = pz;
         pointList.append(pc[0]);
         pointList.append(pc[1]);
         pointList.append(pc[2]);
}
void GLWidget::plotPoint()
{
        glPointSize(7.0);
        glColor3f (0, 0, 0);
        for (int i=0; i<pointList.count()-2; i=i+3)
        {
          glBegin(GL_POINTS);
          glVertex3f(pointList.at(i), pointList.at(i+1), pointList.at(i+2));
          glEnd();
        }
        update();
}

The list is defined in glwidget.h:

class GLWidget : public QOpenGLWidget
{
    Q_OBJECT
public:
    GLWidget(QWidget *parent = 0);
    ~GLWidget();

    QList<double> pointList;
}

and I am trying to write a file in the mainwindow.cpp:

void MainWindow::on_actionSave_triggered()
{
    QFile file(filePath);
    if(!file.open(QFile::WriteOnly | QFile::Text)) {
    QMessageBox::warning(this,"..","file not open");
    return;
  }
    QTextStream out(&file);

    out << "// point:\n";
    for (int i=0; i<pointList.count()-2; i=i+3)
    {
    out << pointList;
    }
}

It seems that mainwindow.cpp does not see the list as I am getting an error message that pointList is not declared. How to fix it?


Solution

  • Probably it will help to other novices and I am not sure if it is the best solution. At least it works without any problems.

    connection in mainwindow.cpp:

    connect(this, SIGNAL(sendPoint(QList<double>)),
                     ui->openGLWidget, SLOT(getPoint(QList<double>)));
    

    emit signal:

    void MainWindow::on_pushButton_add_clicked()
    {
        ...
         emit sendPoint(QList<double> (pointList));
    }
    

    getting in slot:

    void GLWidget::getPoint(QList<double> (pointList))
    {
        pointGList = pointList;
    }