2012-06-28 3 views
3

아이콘과 체크 박스가있는 QListView에 주어진 경로로 디렉토리를 나열한 다음, 표시된 폴더의 이름을 프로그램으로 전송해야합니다. 목록 디렉토리의 경우 코드를 사용합니다 :QListView 파일 시스템보기를위한 체크 박스가있는 것

#include <QtGui/QApplication> 
#include <QFileSystemModel> 
#include <QListView> 


    int main(int argc, char *argv[]) 
    { 
     QApplication a(argc, argv); 

     QFileSystemModel model; 

     QListView listView; 
     listView.setModel(&model); 
     listView.setRootIndex(model.setRootPath("C:\\Program Files")); 
     listView.show(); 

     return a.exec(); 
    } 

버튼을 누른 후 체크 박스를 추가하여 전송하는 방법은 무엇입니까?

감사합니다.

+0

+1 : // sscce. org)! –

답변

1

몇 줄에 맞을 수있는 방법은 없습니다. QFileSystemModel에서 파생되고 확인란 열을 추가하거나 동일한 작업을 수행 할 프록시 모델을 만들 수 있습니다. 당신이 내장 된 선택 메커니즘을 사용할 수 있습니다

참고 여러 항목에 선택을 확장 ⌘가 클릭/Ctrl 키를 클릭하십시오 [sscce] (HTTP에 대한

//main.cpp 
#include <QApplication> 
#include <QFileSystemModel> 
#include <QGridLayout> 
#include <QListView> 
#include <QPushButton> 
#include <QMessageBox> 

class Win : public QWidget 
{ 
    Q_OBJECT 
    QListView * view; 
    QPushButton * button; 
public: 
    Win(QAbstractItemModel * model, const QModelIndex & idx) : 
     view(new QListView(this)), button(new QPushButton("List Selection", this)) 
    { 
     QGridLayout * lay = new QGridLayout; 
     lay->addWidget(view, 0, 0, 1, 2); 
     lay->addWidget(button, 1, 0); 
     setLayout(lay); 
     view->setSelectionMode(QAbstractItemView::MultiSelection); 
     view->setModel(model); 
     view->setRootIndex(idx); 
     connect(button, SIGNAL(clicked()), SLOT(showSelection())); 
    } 
public slots: 
    void showSelection() { 
     QString str; 
     foreach (QModelIndex i, view->selectionModel()->selectedIndexes()) { 
      str.append(i.data().toString()); 
      str.append("\n"); 
     } 
     QMessageBox::information(this, "Selected items", str); 
    } 
}; 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    QFileSystemModel model; 
    Win v(&model, model.setRootPath("/")); 
    v.show(); 
    return a.exec(); 
} 

#include "main.moc" 
+0

자세히 설명해 주시겠습니까? –

+0

프록시 모델 코드도 표시됩니다. 몇 분만 기다려주십시오 - –

+0

답장을 보내 주셔서 감사합니다. 이것은 내가 필요로하는 것이 거의 있지만, 체크 박스가 필요한 항목을 선택하는 것입니다. –

관련 문제