Advertisement
bmahf

Adding a column to QFileSystemModel in a given position

Jan 14th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.41 KB | None | 0 0
  1. testfilesystemmodeldialog.h
  2. ---------------------------------------------------------------------------
  3. #ifndef FILESYSTEMUSBMODELDIALOG_H
  4. #define FILESYSTEMUSBMODELDIALOG_H
  5.  
  6. #include "ui_testfilesystemmodelwidget.h"
  7.  
  8. #include <QWidget>
  9. #include <QFileSystemModel>
  10. #include <QItemDelegate>
  11.  
  12. class QPushButton;
  13.  
  14. class TestItemFileDelegate : public QItemDelegate
  15. {
  16. public:
  17.   TestItemFileDelegate();
  18.   QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
  19. };
  20.  
  21. class TestFileSystemModel : public QFileSystemModel
  22. {
  23.         Q_OBJECT
  24. public:
  25.     TestFileSystemModel(QObject * parent = 0);
  26.     int columnCount(const QModelIndex& parent = QModelIndex()) const;
  27.     QVariant headerData(int section, Qt::Orientation orientation, int role) const;
  28.     virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
  29.  
  30. public slots:
  31.     void onDirectoryLoaded(const QString & path);
  32. };
  33.  
  34. class TestFileSystemModelDialog : public QWidget
  35. {
  36.     Q_OBJECT
  37.  
  38. public:
  39.     explicit TestFileSystemModelDialog(QWidget *parent);
  40.     ~TestFileSystemModelDialog() {}
  41.  
  42. private:
  43.     Ui::TestFileSystemModelWidget *ui;
  44.     TestFileSystemModel *file;
  45. };
  46.  
  47. #endif // FILESYSTEMUSBMODELDIALOG_H
  48. ---------------------------------------------------------------------------
  49.  
  50. testfilesystemmodeldialog.cpp
  51. ---------------------------------------------------------------------------
  52. #include "testfilesystemmodeldialog.h"
  53.  
  54. #include <QDebug>
  55. #include <QTimer>
  56. #include <QPushButton>
  57.  
  58. TestItemFileDelegate::TestItemFileDelegate(){}
  59.  
  60. static int num = 0;
  61.  
  62. QSize TestItemFileDelegate::sizeHint ( const QStyleOptionViewItem & /*option*/, const QModelIndex & index ) const
  63. {
  64.     const QFileSystemModel *model = reinterpret_cast<const QFileSystemModel *>(index.model());
  65.     QFileInfo info = model->fileInfo(index);
  66.     if(info.isDir())
  67.     {
  68.         return QSize(40,40);
  69.     }
  70.     else
  71.     {
  72.         return QSize(64,64);
  73.     }
  74. }
  75.  
  76. TestFileSystemModel::TestFileSystemModel(QObject * parent) :
  77.     QFileSystemModel(parent)
  78. {
  79.     // NOTE: counts before and after the insert both show the same count, and insert returns false.
  80.     int i = QFileSystemModel::columnCount();
  81.     bool b = insertColumn(1);
  82.     int j = QFileSystemModel::columnCount();
  83.  
  84.     // NOTE: connecting to directoryLoaded() signal to see if deferring the column creation will help.
  85.     bool b1 = connect(this,SIGNAL(directoryLoaded(QString)),this,SLOT(onDirectoryLoaded(QString)));
  86. }
  87.  
  88. int TestFileSystemModel::columnCount(const QModelIndex& parent) const
  89. {
  90.     return (parent.column() > 0) ? 0 : 5;
  91. }
  92.  
  93. QVariant TestFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const
  94. {
  95.   if (section == 4 && orientation == Qt::Horizontal && role == Qt::DisplayRole)
  96.     return tr("File Owner");
  97.   return QFileSystemModel::headerData(section, orientation, role);
  98. }
  99.  
  100. QVariant TestFileSystemModel::data(const QModelIndex & index, int role) const
  101. {
  102.     if( !index.isValid() )
  103.         return QVariant();
  104.     switch(role)
  105.     {
  106.     case Qt::DisplayRole:
  107.         if (index.column() == 4)
  108.             return QString("Owner %1").arg(num++);
  109.         else
  110.             return QFileSystemModel::data(index, role);
  111.         break;
  112.     case Qt::TextAlignmentRole:
  113.         return Qt::AlignHCenter | Qt::AlignVCenter;
  114.         break;
  115.     default:
  116.         return QFileSystemModel::data(index, role);
  117.         break;
  118.    }
  119.     return QFileSystemModel::data(index, role);
  120. }
  121.  
  122. void TestFileSystemModel::onDirectoryLoaded(const QString & path)
  123. {
  124.     // NOTE: counts before and after the insert both show the same count, and insert returns false.
  125.     int i = QFileSystemModel::columnCount();
  126.     bool b = insertColumn(1);
  127.     int j = QFileSystemModel::columnCount();
  128.     int o = 0;
  129. }
  130.  
  131. TestFileSystemModelDialog::TestFileSystemModelDialog(QWidget *parent) :
  132.     QWidget(parent),
  133.     ui(new Ui::TestFileSystemModelWidget)
  134. {
  135.     ui->setupUi(this);
  136.  
  137.     QString sPath("C:\\tmp");
  138.     file = new TestFileSystemModel();
  139.     QDir fcsDir(sPath);
  140.     file->setFilter(QDir::NoDotAndDotDot | QDir::Files);
  141.     QStringList filters;
  142.     filters << "*.*";
  143.     file->setNameFilters(filters);
  144.     file->setNameFilterDisables(false);
  145.     QModelIndex index = file->setRootPath(fcsDir.absolutePath());
  146.  
  147.     TestItemFileDelegate *iconItemFileDelegate = new TestItemFileDelegate();
  148.  
  149.     ui->fileTreeView->setModel(file);
  150.     ui->fileTreeView->setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
  151.     ui->fileTreeView->setItemDelegate(iconItemFileDelegate);
  152.     ui->fileTreeView->setRootIndex(index);
  153. }
  154. ---------------------------------------------------------------------------
  155.  
  156. mainwindow.h
  157. ---------------------------------------------------------------------------
  158. #ifndef MAINWINDOW_H
  159. #define MAINWINDOW_H
  160.  
  161. #include <QMainWindow>
  162.  
  163. class TestFileSystemModelDialog;
  164. class QFileSystemModel;
  165.  
  166. namespace Ui {
  167. class MainWindow;
  168. }
  169.  
  170. class MainWindow : public QMainWindow
  171. {
  172.     Q_OBJECT
  173.  
  174. public:
  175.     explicit MainWindow(QWidget *parent = 0);
  176.     ~MainWindow() {}
  177.  
  178. private:
  179.     Ui::MainWindow *ui;
  180.     TestFileSystemModelDialog *treeView;
  181.     QFileSystemModel *fileSystemModel;
  182. };
  183.  
  184. #endif // MAINWINDOW_H
  185.  
  186. ---------------------------------------------------------------------------
  187.  
  188. mainwindow.cpp
  189. ---------------------------------------------------------------------------
  190. #include "mainwindow.h"
  191. #include "testfilesystemmodeldialog.h"
  192.  
  193. #include <QFileSystemModel>
  194.  
  195. MainWindow::MainWindow(QWidget *parent) :
  196.     QMainWindow(parent)
  197. {
  198.     treeView = new TestFileSystemModelDialog(this);
  199.     setCentralWidget(treeView);
  200.     fileSystemModel = new QFileSystemModel;
  201. }
  202. ---------------------------------------------------------------------------
  203.  
  204. main.cpp
  205. ---------------------------------------------------------------------------
  206. #include "mainwindow.h"
  207. #include <QApplication>
  208.  
  209. int main(int argc, char *argv[])
  210. {
  211.     QApplication a(argc, argv);
  212.     MainWindow w;
  213.     w.show();
  214.     return a.exec();
  215. }
  216. ---------------------------------------------------------------------------
  217.  
  218. testfilesystemmodelwidget.ui
  219. ---------------------------------------------------------------------------
  220. <?xml version="1.0" encoding="UTF-8"?>
  221. <ui version="4.0">
  222.  <class>TestFileSystemModelWidget</class>
  223.  <widget class="QWidget" name="TestFileSystemModelWidget">
  224.   <property name="geometry">
  225.    <rect>
  226.     <x>0</x>
  227.     <y>0</y>
  228.     <width>468</width>
  229.     <height>602</height>
  230.    </rect>
  231.   </property>
  232.   <property name="sizePolicy">
  233.    <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
  234.     <horstretch>0</horstretch>
  235.     <verstretch>0</verstretch>
  236.    </sizepolicy>
  237.   </property>
  238.   <property name="windowTitle">
  239.    <string>Form</string>
  240.   </property>
  241.   <layout class="QHBoxLayout" name="horizontalLayout">
  242.    <item>
  243.     <layout class="QVBoxLayout" name="verticalLayout">
  244.      <item>
  245.       <widget class="QLabel" name="fileLabel">
  246.        <property name="font">
  247.         <font>
  248.          <pointsize>12</pointsize>
  249.         </font>
  250.        </property>
  251.        <property name="text">
  252.         <string>File to Copy</string>
  253.        </property>
  254.       </widget>
  255.      </item>
  256.      <item>
  257.       <widget class="QTreeView" name="fileTreeView"/>
  258.      </item>
  259.     </layout>
  260.    </item>
  261.   </layout>
  262.  </widget>
  263.  <resources/>
  264.  <connections/>
  265. </ui>
  266. ---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement