QStringList to QDir



  • I am getting error cannot convert from 'QStringList' to 'const QDir' -> problem is with *QDir dir(files)*and I don't know how to fix that... I need to get path of files added with QFileDialog and send them to playlist. But QDir can't read paths from QStringList...

    @void MainWindow::on_addSong_clicked()
    {

    QString selectedFilter;
    QStringList files = QFileDialog::getOpenFileNames(
                                this, tr("QFileDialog::getOpenFileNames()"),
                                openFilesPath,
                                tr("MP3 Files (*.mp3)"),
                                &selectedFilter
                               );
    
    QDir dir(files);
    QList<QMediaContent> content;
    for(const QString& f:files)
    {
        content.push_back(QUrl::fromLocalFile&#40;dir.path(&#41;+"/" + f&#41;&#41;;
        QFileInfo fi(f&#41;;
        ui->listWidget1->addItem(fi.fileName(&#41;);
    
    }
    playlist->addMedia(content);
    if (files.count()) {
        openFilesPath = files[0];
        ui->label->setText(QString("[%1]").arg(files.join(", ")));
    
    }
    

    }@


  • Moderators

    Hi,

    QDir is used to iterate over a directory. You can't use it to parse a QStringList.
    So to get the items from the QStringList you have to iterate it.
    Eg.
    @QStringList files = QFileDialog::getOpenFileNames(this, tr("QFileDialog::getOpenFileNames()"),QDir::rootPath());

    for(int i=0;i<files.count();i++)
    {
        qDebug() << files.at(i);
    }@
    

    Also QFileDialog::getOpenFileNames returns the absolute path of the selected files.


Log in to reply
 

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