2014-06-14 6 views
0

내 응용 프로그램에 QListView이 있고 QT 문서에 표시된대로 해당 아이콘이있는 파일 목록을 표시하려고합니다.
QListView 나는 다음과 같은 코드를 가지고있는 Icon mode 에 - 이제QListView에서 파일 아이콘을 볼 수없는 이유는 무엇입니까?

std::vector<std::string>::iterator it = result.begin() ; // got the results, now tie them to the StandardItemModel. 
     RespPara::stringList = new QStringList ; 
     RespPara::model = new QStringListModel ; 
     while(it!=result.end()) 
      { 
     std::cout<<*it<<std::endl ; 
     RespPara::stringList->append((*it).c_str()) ; 
     it++ ; 
      } 
     RespPara::model->setStringList(*(RespPara::stringList)) ; 
     RespPara::mainWindow->listView->setModel(RespPara::model) ; 

파일 목록이 기본 응용 프로그램에서 볼 수 있지만, 아이콘이 표시되지 않습니다.
여기서 내가 뭘 잘못하고 있니? 이 문제를 어떻게 해결할 수 있습니까?

편집 : - :

여기
while(!in.eof()) 
    { 
     getline(in, buff) ; 
     QFileInfo fileInfo(buff.c_str()) ; 
     QFileIconProvider iconProvider ; 
     QIcon icon = iconProvider.icon(fileInfo) ; 
     QStandardItem* standardItem = new QStandardItem(icon, buff.c_str()) ; 
     myModel->appendRow(standardItem) ; 
    } 
    win.listView->setModel(myModel) ; 

스크린 샷 : - -이

enter image description here

enter image description here

다음은 모든 유형의 파일에 대해 동일한 아이콘을주고 새로운 코드
+0

당신은 파일 이름에 확장자를 포함나요? – Tay2510

+0

예, 포함 시켰습니다 –

+0

"같은 아이콘"은 무엇입니까? 아마도 스크린 샷이 있을까요? – Tay2510

답변

3

QListView은 파일 아이콘을 인식하는 데 그리 강력하지 않습니다. 단지 목록보기 일뿐입니다. 당신이 QListView에 아이콘을 표시 할 경우, 기존의 방법은 예를 들어, QIcon를 인스턴스화하고 모델로 설정하는 것입니다

코드에서 설정에는 어떤 아이콘이 없습니다
QIcon icon(":/myIcons/theIcon.png"); 
model->setItem(0,0, new QStandardItem(icon, "Text next to the icon")); 

, 그건 왜 당신이 할 수 있어요 그들을 보지 마라.

귀하의 경우 QIcon파일 아이콘으로 제공되어야하며 QFileIconProvider 클래스의 도움을 받아야합니다. 다음 코드는 시스템에서 파일 아이콘을 얻을 :

QFileInfo fileinfo("C:/cat/is/lovely/Test.txt"); // Provides the information of file type 
QFileIconProvider iconprovider; 
QIcon icon = iconprovider.icon(fileinfo); // return QIcon according to the file type 

그 후, 당신은 모델에 QIcon을 설정합니다. buff``에서 : (.txt 인 예)

enter image description here

+1

또한 표시 할 수있는 파일 아이콘 (캐시 할 수 있음)을 가져와야 만 사용자 모델 클래스를 구현할 가치가 있습니다. – Pete

+0

그래도 한 가지 문제가 있습니다. ListView는 표시하지 않고 모든 파일 형식에 대해 동일한 아이콘을 표시합니다 다른 파일 형식에 대한 다른 아이콘 –

+0

@ ps06756 코드를 보여줄 수 있습니까? 내 데모에서는 다른 파일 유형의 다른 아이콘을 표시했습니다. – Tay2510

관련 문제