Model View Tutorial Part1 CdRack/de

From Qt Wiki
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

←Allgemeines: Klasse VirtualCdRack
↑Übersicht Model/View Tutorial:CD Verwaltung↑
Teil 1: Read Only Modell→

Die Klasse CdRack

Die Klasse CdRack stellt den Datenlayer der Applikation dar. Sie speichert die einzelnen CDs in einer Liste und auch die Genres. Mittels der üblichen File-Menüs können die Daten in einer XML-Datei gespeichert bzw. aus einer XML-Datei geladen werden.

Die Methode clear() löscht den gesamten Inhalt und löst das Signal reset aus.
Mittels addCD(…) können CDs hinzugefügt werden. Es wird das Signal inserted(int) ausgelöst.
Mittels changeDisk(int, const CdDisk&) können die Daten einer CD geändert werden.

  1. class CdRack : public QObject
  2. {
  3.     Q_OBJECT
  4. public:
  5.     // ----- construction --------------------------------------------------------------------------
  6.     CdRack();
  7.  
  8.     // ----- methods -------------------------------------------------------------------------------
  9.     void changeDisk(int nIndex, const CdDisk& newData);
  10.     void addCD(const CdDisk& newData);
  11.     void clear(void);
  12.     bool load(QIODevice *device);
  13.     bool save(QIODevice *device);
  14.  
  15.     // ----- accessors -----------------------------------------------------------------------------
  16.     int size();
  17.     const CdDisk& at(int i) const;
  18.     bool isModified() const;
  19.     void setModified(bool bModified);
  20.     const QString& errorString();
  21.  
  22. signals:
  23.     void reset();
  24.     void contentsChanged();
  25.     void inserted(int nPosition);
  26.     void inserted(int nFirst, int nLast);
  27.  
  28. private:
  29.     // ----- privat helpers ------------------------------------------------------------------------
  30.     void writeCd(QXmlStreamWriter& xmlStream, const CdDisk& disk) const;
  31.     void readCdRack(QXmlStreamReader& xmlStream);
  32.  
  33.     // ----- members -------------------------------------------------------------------------------
  34.     QList<CdDisk> m_cds;
  35.     bool m_bModified;
  36.     QString m_errorString;
  37.     QStringList m_genres;
  38. };

Die Klasse CdDisk

Die Klasse CdDisk beinhaltet die Daten einer CD.

  1. class CdDisk
  2. {
  3. public:
  4.     CdDisk();
  5.  
  6.     QString m_title;
  7.     QString m_author;
  8.     QString m_genre;
  9.     int     m_year;
  10. };