Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to use void QDir::setPath(const QString &path) to assign a relative path?



  • hi

    i got puzzle about this piece of code :```

        QWidget(parent),
        ui(new Ui::Widget),
        musicDir(new QDir),
        musicFileInfo(new QList<QFileInfo>),
        mediaPlayer(new QMediaPlayer),
        mediaPlaylist(new QMediaPlaylist),
        imageDir(new QDir),
        imageFileInfo(new QList<QFileInfo>),
        imageTimer(new QTimer),
        imageRefreshTimer(new QTimer)
    {
        ui->setupUi(this);
        ui->image->setAlignment(Qt::AlignCenter);
    
    //    musicDir->setPath("F:/project/WriteToMyDear-master/WriteToMyDear/music1");
        musicDir->setPath("WriteToMyDear/music1");
        music_path_exist = musicDir->exists();
    
    

    i got "music_path_exist" false when i use relative path "musicDir->setPath("WriteToMyDear/music1");" and absolute path works fine.

    can someone give some suggesstions?

    thanks


  • Qt Champions 2019

    From the documentation ( http://doc.qt.io/qt-5/qdir.html#setPath ):

    Relative file names begin with a directory name or a file name and specify a path relative to the current directory.

    And your current directory can be anything depending on where you start your application or if you switched it somewhere in your app.



  • hi

    thanks for your help.

    but i actually begin with a directory name:
    0_1534666842633_无标题.png

    and the " music1" directory is there:
    0_1534666991570_捕获.PNG

    and can you give me some suggestion?

    thanks


  • Lifetime Qt Champion

    @Mengmeng_Xiaoxiannv
    Hi
    Try
    qDebug() << QDir::currentPath()
    and see where current dir is.
    On many platform it will be in the build folder where .exe is :)
    And not the project folder.


Log in to reply