본문 바로가기
QT and Symbian

QFileSystemModel을 이용해서 Directory만 QTreeView에 표시하는 방법

by leo21c 2015. 5. 28.
SMALL

http://doc.qt.io/qt-4.8/qt-itemviews-dirview-example.html

위의 예제를 이용하면 QFileSystemModel을 이용해서 간단하게 Dir view를 만들수 있다.

그러나 이미지를 봐서 알겠지만 Dir만 표시되지 않고 부가적인 것까지 표시가 된다.

그래서 간단하게 수정을 했다.

class DirSystemModel : public QFileSystemModel
{
	Q_OBJECT

public:
	DirSystemModel(QObject *parent) : QFileSystemModel(parent)
	{
		this->setRootPath("");
		this->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
		this->sort(0, Qt::SortOrder::AscendingOrder);
	}
	//~DirSystemModel();

	int columnCount(const QModelIndex & /* parent */) const {return 1;}
	QVariant data(const QModelIndex &index, int role) const {
		return QFileSystemModel::data(index, role);;
	}	
};

아래에 있는 예제 소스에서 QFileSystemModel 대신 DirSystemModel을 사용하면 된다.

#include 

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

    DirSystemModel model;
    model.setRootPath("");
    QTreeView tree;
    tree.setModel(&model);

    // Demonstrating look and feel features
    tree.setAnimated(false);
    tree.setIndentation(20);
    //tree.setSortingEnabled(true);

    tree.setWindowTitle(QObject::tr("Dir View"));
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
    tree.showMaximized();
#else
    tree.resize(640, 480);
    tree.show();
#endif

    return app.exec();
}
LIST