Does not open file in Qt



  • Hi!
    I'm trying to open and read an .obj file but i am unable to do so.
    My code is
    0_1512208577863_Captura de pantalla 2017-12-02 a las 10.53.35.png
    My problem is that in the first If it returns false and I don't know why.
    My file is in the .qrc and also in the path ("Users/carolina/unchocolatito2").
    If someone could help it would be great


  • Qt Champions 2017

    Hi
    Are you sure path is correct ?

    I feel it should be "/Users/carolina/unchocolatito2"



  • Hi! Thank you for replying me. I have changed what you said but it still doesn't work.


  • Qt Champions 2017

    @Carolinabustillo
    Ok not sure what linux you are on, my Users folder is called home

    If you open shell
    and do
    cd /Users/carolina/unchocolatito2
    does that work? ( meaning it change to there)

    If you put file in a qres file. then find file in Creator, and right click it. You can
    the copy path.

    alt text

    And use that ":/xxxx" path where you have todojuntolabio.obj now



  • Thank you so much. My computer is a macOs but I just did what you said about copying the path and now it works.
    Now I have this problem.
    0_1512212995342_Captura de pantalla 2017-12-02 a las 12.07.36.png
    When I try to click in qvector.h it says that the file doesn't exist or I don't have permission


  • Qt Champions 2017

    @Carolinabustillo
    Hi
    It says you are using a vector.at(XXX) where xxx is larger than the numbers of items in the list
    (or list is empty )

    Can you paste the code ? ( the image only shows top of it)
    ( you can just use the </> button in editor and paste the real code )



  • @mrjj here it is

    void QObj3dReader::parseObjFile(const QString &fileName,
                                    QStringList &comments,
                                    QVector<QOpenGLTriangle3D> &triangles)
    {
        comments.clear();
        triangles.clear();
    
        QFile file(fileName);
        if(file.exists())
        {
            if(file.open(QFile::ReadOnly | QFile::Text))
            {
                QVector<QVector3D> v, vn;
                 qDebug() << "se abre";
    
                while(!file.atEnd())
                {
                    QString line = file.readLine().trimmed();
                    QStringList lineParts = line.split(QRegularExpression("\\s+"));
                    if(lineParts.count() > 0)
                    {
    
                        // if it's a comment
                        if(lineParts.at(0).compare("#", Qt::CaseInsensitive) == 0)
                        {
                            comments.append(line.remove(0, 1).trimmed());
                        }
    
                        // if it's a vertex position (v)
                        else if(lineParts.at(0).compare("v", Qt::CaseInsensitive) == 0)
                        {
                            v.append(QVector3D(lineParts.at(1).toFloat(),
                                               lineParts.at(2).toFloat(),
                                               lineParts.at(3).toFloat()));
                        }
    
    
    
                        // if it's face data (f)
                        // there's an assumption here that faces are all triangles
                        else if(lineParts.at(0).compare("f", Qt::CaseInsensitive) == 0)
                        {
                            QOpenGLTriangle3D triangle;
    
                            // get points from v array
                            triangle.p1 = v.at(lineParts.at(1).split("/").at(0).toInt() - 1);
                            triangle.p2 = v.at(lineParts.at(2).split("/").at(0).toInt() - 1);
                            triangle.p3 = v.at(lineParts.at(3).split("/").at(0).toInt() - 1);
    
    
    
                            // get normals from vn array
                            triangle.p1Normal = vn.at(lineParts.at(1).split("/").at(2).toInt() - 1);
                            triangle.p2Normal = vn.at(lineParts.at(2).split("/").at(2).toInt() - 1);
                            triangle.p3Normal = vn.at(lineParts.at(3).split("/").at(2).toInt() - 1);
    
                            triangles.append(triangle);
                        }
    
                    }
                }
    
                file.close();
            }
        }
    }
    

  • Qt Champions 2017

    Hi
    From a quick look
    It dont seem you append anything to the vn vector and then later do
    triangle.p1Normal = vn.at(xxx)

    alt text

    So its empty and u try to get element.



  • Thank you so much!!
    I was trying different ways to try to open a file that doing copy and paste I forgot some lines of the code. Now it works.


  • Qt Champions 2017

    @Carolinabustillo
    Super :)
    Notice that
    lines such at
    lineParts.at(1).split("/").at(2)
    will give same kind of error if format is different and split returns an empty list.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.