2017-12-18 1 views
-1

먼저 여러 개의 이미지를 저장하는 폴더가 있다고 가정합니다. 그런 다음 UI에서 버튼을 클릭하여 폴더를 열고 그 폴더에있는 이미지의 모든 파일 경로를 QList (필터링 된 이미지 파일 만)에 저장하려고합니다. 그러나 QList는 아무 것도 저장하지 않습니다. 도와주세요.QT에서 QFileSystemModel에서 파일 경로를 얻는 방법 (C++)

의견에서 언급 한 바와 같이
void MainWindow::on_pushButton_clicked() 
{ 
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), 
               "/home", 
               QFileDialog::ShowDirsOnly 
               | QFileDialog::DontResolveSymlinks); 
    model = new QFileSystemModel(); 
    filesPath = dir; 
    model->setRootPath(dir); 
    QStringList filter; 
    filter <<"*.png" <<"*.jpg" <<"*.bmp" <<"*.gif"; 
    model->setNameFilters(filter); 
    model->setNameFilterDisables(false); 

    ui->treeView->setModel(model); 
    ui->treeView->setRootIndex(model->index(dir)); 
    ui->treeView->setAnimated(false); 
    ui->treeView->setSortingEnabled(true); 

    QList<QString> path_list; 
    QModelIndex parentIndex = model->index(dir); 
    int numRows = model->rowCount(parentIndex); 

    for (int row = 0; row < numRows; ++row) { 
     QModelIndex childIndex = model->index(row, 0, parentIndex); 
     QString path = model->data(childIndex).toString(); 
     if(!QFileInfo(path).isDir()) 
      path_list.append(path); 
    } 
} 
+0

Qt 문서를 읽어야합니다. http://doc.qt.io/qt-5/qfilesystemmodel.html#details –

+0

@ md612 왜 QFileSystemModel을 사용하고 있습니까? –

+0

@Dmitry Sazonov QtreeView – md612

답변

1

적합한 필터를 사용하여 QDir::entryList()을 사용하면 원하는 코드와 오버 헤드를 줄일 수 있습니다.

+0

이것은 유용한 답변입니다. OP가 트리보기를 채우기위한 항목 모델이 필요하다는 것입니다. 그리고 질문은'QFileSystemModel'이리스트를 채우는 데 왜 작동하지 않는가하는 것이 었습니다. 오 잘 ... : P –

1

QFileSystemModel는 그렇게 사용하도록 설계되지 않았습니다. 이 파일 시스템으로 정지 할 수있는 메인 스레드가 발생하지 않도록

QDirModel 달리

이 QFileSystemModel가 조회되는 자체를 채우기 위해 별도의 스레드를 사용한다 : 여기서 핵심은 (강조 광산)에 지적 된 문서에 . rowCount()를 호출하면 모델이 디렉토리를 채울 때까지 0이 반환됩니다. 당신이 절대적으로 목록을 작성하는 QFileSystemModel을 사용하고자하는 경우

, 당신은 directoryLoaded(const QString &path) 신호에 함수를 연결하고가로드 된 후 각 폴더에있는 파일을 추가 할 필요가 ...하지만 아마 많이 있습니다 것 당신이 필요로하는 것을 성취 할 수있는 더 좋은 방법.

관련 문제