Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- testfilesystemmodeldialog.h
- ---------------------------------------------------------------------------
- #ifndef FILESYSTEMUSBMODELDIALOG_H
- #define FILESYSTEMUSBMODELDIALOG_H
- #include "ui_testfilesystemmodelwidget.h"
- #include <QWidget>
- #include <QFileSystemModel>
- #include <QItemDelegate>
- class QPushButton;
- class TestItemFileDelegate : public QItemDelegate
- {
- public:
- TestItemFileDelegate();
- QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
- };
- class TestFileSystemModel : public QFileSystemModel
- {
- Q_OBJECT
- public:
- TestFileSystemModel(QObject * parent = 0);
- int columnCount(const QModelIndex& parent = QModelIndex()) const;
- QVariant headerData(int section, Qt::Orientation orientation, int role) const;
- virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
- public slots:
- void onDirectoryLoaded(const QString & path);
- };
- class TestFileSystemModelDialog : public QWidget
- {
- Q_OBJECT
- public:
- explicit TestFileSystemModelDialog(QWidget *parent);
- ~TestFileSystemModelDialog() {}
- private:
- Ui::TestFileSystemModelWidget *ui;
- TestFileSystemModel *file;
- };
- #endif // FILESYSTEMUSBMODELDIALOG_H
- ---------------------------------------------------------------------------
- testfilesystemmodeldialog.cpp
- ---------------------------------------------------------------------------
- #include "testfilesystemmodeldialog.h"
- #include <QDebug>
- #include <QTimer>
- #include <QPushButton>
- TestItemFileDelegate::TestItemFileDelegate(){}
- static int num = 0;
- QSize TestItemFileDelegate::sizeHint ( const QStyleOptionViewItem & /*option*/, const QModelIndex & index ) const
- {
- const QFileSystemModel *model = reinterpret_cast<const QFileSystemModel *>(index.model());
- QFileInfo info = model->fileInfo(index);
- if(info.isDir())
- {
- return QSize(40,40);
- }
- else
- {
- return QSize(64,64);
- }
- }
- TestFileSystemModel::TestFileSystemModel(QObject * parent) :
- QFileSystemModel(parent)
- {
- // NOTE: counts before and after the insert both show the same count, and insert returns false.
- int i = QFileSystemModel::columnCount();
- bool b = insertColumn(1);
- int j = QFileSystemModel::columnCount();
- // NOTE: connecting to directoryLoaded() signal to see if deferring the column creation will help.
- bool b1 = connect(this,SIGNAL(directoryLoaded(QString)),this,SLOT(onDirectoryLoaded(QString)));
- }
- int TestFileSystemModel::columnCount(const QModelIndex& parent) const
- {
- return (parent.column() > 0) ? 0 : 5;
- }
- QVariant TestFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const
- {
- if (section == 4 && orientation == Qt::Horizontal && role == Qt::DisplayRole)
- return tr("File Owner");
- return QFileSystemModel::headerData(section, orientation, role);
- }
- QVariant TestFileSystemModel::data(const QModelIndex & index, int role) const
- {
- if( !index.isValid() )
- return QVariant();
- switch(role)
- {
- case Qt::DisplayRole:
- if (index.column() == 4)
- return QString("Owner %1").arg(num++);
- else
- return QFileSystemModel::data(index, role);
- break;
- case Qt::TextAlignmentRole:
- return Qt::AlignHCenter | Qt::AlignVCenter;
- break;
- default:
- return QFileSystemModel::data(index, role);
- break;
- }
- return QFileSystemModel::data(index, role);
- }
- void TestFileSystemModel::onDirectoryLoaded(const QString & path)
- {
- // NOTE: counts before and after the insert both show the same count, and insert returns false.
- int i = QFileSystemModel::columnCount();
- bool b = insertColumn(1);
- int j = QFileSystemModel::columnCount();
- int o = 0;
- }
- TestFileSystemModelDialog::TestFileSystemModelDialog(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::TestFileSystemModelWidget)
- {
- ui->setupUi(this);
- QString sPath("C:\\tmp");
- file = new TestFileSystemModel();
- QDir fcsDir(sPath);
- file->setFilter(QDir::NoDotAndDotDot | QDir::Files);
- QStringList filters;
- filters << "*.*";
- file->setNameFilters(filters);
- file->setNameFilterDisables(false);
- QModelIndex index = file->setRootPath(fcsDir.absolutePath());
- TestItemFileDelegate *iconItemFileDelegate = new TestItemFileDelegate();
- ui->fileTreeView->setModel(file);
- ui->fileTreeView->setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
- ui->fileTreeView->setItemDelegate(iconItemFileDelegate);
- ui->fileTreeView->setRootIndex(index);
- }
- ---------------------------------------------------------------------------
- mainwindow.h
- ---------------------------------------------------------------------------
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- class TestFileSystemModelDialog;
- class QFileSystemModel;
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow() {}
- private:
- Ui::MainWindow *ui;
- TestFileSystemModelDialog *treeView;
- QFileSystemModel *fileSystemModel;
- };
- #endif // MAINWINDOW_H
- ---------------------------------------------------------------------------
- mainwindow.cpp
- ---------------------------------------------------------------------------
- #include "mainwindow.h"
- #include "testfilesystemmodeldialog.h"
- #include <QFileSystemModel>
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent)
- {
- treeView = new TestFileSystemModelDialog(this);
- setCentralWidget(treeView);
- fileSystemModel = new QFileSystemModel;
- }
- ---------------------------------------------------------------------------
- main.cpp
- ---------------------------------------------------------------------------
- #include "mainwindow.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
- ---------------------------------------------------------------------------
- testfilesystemmodelwidget.ui
- ---------------------------------------------------------------------------
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>TestFileSystemModelWidget</class>
- <widget class="QWidget" name="TestFileSystemModelWidget">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>468</width>
- <height>602</height>
- </rect>
- </property>
- <property name="sizePolicy">
- <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
- <horstretch>0</horstretch>
- <verstretch>0</verstretch>
- </sizepolicy>
- </property>
- <property name="windowTitle">
- <string>Form</string>
- </property>
- <layout class="QHBoxLayout" name="horizontalLayout">
- <item>
- <layout class="QVBoxLayout" name="verticalLayout">
- <item>
- <widget class="QLabel" name="fileLabel">
- <property name="font">
- <font>
- <pointsize>12</pointsize>
- </font>
- </property>
- <property name="text">
- <string>File to Copy</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QTreeView" name="fileTreeView"/>
- </item>
- </layout>
- </item>
- </layout>
- </widget>
- <resources/>
- <connections/>
- </ui>
- ---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement