You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

csvmodelwriter.h 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (c) 2011-2014 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_QT_CSVMODELWRITER_H
  5. #define STARWELS_QT_CSVMODELWRITER_H
  6. #include <QList>
  7. #include <QObject>
  8. QT_BEGIN_NAMESPACE
  9. class QAbstractItemModel;
  10. QT_END_NAMESPACE
  11. /** Export a Qt table model to a CSV file. This is useful for analyzing or post-processing the data in
  12. a spreadsheet.
  13. */
  14. class CSVModelWriter : public QObject
  15. {
  16. Q_OBJECT
  17. public:
  18. explicit CSVModelWriter(const QString &filename, QObject *parent = 0);
  19. void setModel(const QAbstractItemModel *model);
  20. void addColumn(const QString &title, int column, int role=Qt::EditRole);
  21. /** Perform export of the model to CSV.
  22. @returns true on success, false otherwise
  23. */
  24. bool write();
  25. private:
  26. QString filename;
  27. const QAbstractItemModel *model;
  28. struct Column
  29. {
  30. QString title;
  31. int column;
  32. int role;
  33. };
  34. QList<Column> columns;
  35. };
  36. #endif // STARWELS_QT_CSVMODELWRITER_H