fork download
  1. //album.h
  2. #ifndef ALBUM_H
  3. #define ALBUM_H
  4.  
  5. #include <QObject>
  6.  
  7. class Album : public QObject
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit Album(QObject *parent = nullptr,QString composer="",QString title="",double cost=0,int rating=0);
  12. QString getComposer() const;
  13. void setComposer(const QString &composer);
  14.  
  15. QString getTitle() const;
  16. void setTitle(const QString &title);
  17.  
  18. double getCost() const;
  19. void setCost(double cost);
  20.  
  21. int getRating() const;
  22. void setRating(int rating);
  23.  
  24. private:
  25. QString composer;
  26. QString title;
  27. double cost;
  28. int rating;
  29. signals:
  30.  
  31. };
  32.  
  33. #endif // ALBUM_H
  34.  
  35. //album.cpp
  36. #include "album.h"
  37.  
  38. Album::Album(QObject *parent,QString composer,QString title,double cost,int rating)
  39. : QObject{parent}
  40. {
  41. setTitle(title);
  42. setComposer(composer);
  43. setCost(cost);
  44. setRating(rating);
  45. }
  46.  
  47. QString Album::getComposer() const
  48. {
  49. return composer;
  50. }
  51.  
  52. void Album::setComposer(const QString &composer)
  53. {
  54. this->composer = composer;
  55. }
  56.  
  57. QString Album::getTitle() const
  58. {
  59. return title;
  60. }
  61.  
  62. void Album::setTitle(const QString &title)
  63. {
  64. this->title = title;
  65. }
  66.  
  67. double Album::getCost() const
  68. {
  69. return cost;
  70. }
  71.  
  72. void Album::setCost(double cost)
  73. {
  74. this->cost = cost;
  75. }
  76.  
  77. int Album::getRating() const
  78. {
  79. return rating;
  80. }
  81.  
  82. void Album::setRating(int rating)
  83. {
  84. this->rating = rating;
  85. }
  86.  
  87. //musicmodel.h
  88. #ifndef MUSICMODEL_H
  89. #define MUSICMODEL_H
  90.  
  91. #include <QObject>
  92. #include <QDebug>
  93. #include <QAbstractTableModel>
  94.  
  95. #include "album.h"
  96.  
  97. class MusicModel : public QAbstractTableModel
  98. {
  99. Q_OBJECT
  100. public:
  101. MusicModel(QObject *parent = nullptr);
  102. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  103. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  104. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  105. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
  106.  
  107. QList<Album *> getAlbums() const;
  108. void setAlbums(const QList<Album *> &newAlbums);
  109.  
  110. void append(Album *album);
  111. int albumCount() const;
  112. Album* getAlbum(int index) const;
  113.  
  114. private:
  115. QList<Album*> albums;
  116. };
  117.  
  118. #endif // MUSICMODEL_H
  119.  
  120. //musicmodel.cpp
  121. #include "musicmodel.h"
  122.  
  123. MusicModel::MusicModel(QObject *parent) : QAbstractTableModel(parent)
  124. {
  125.  
  126. }
  127.  
  128. QVariant MusicModel::data(const QModelIndex &index, int role) const
  129. {
  130. QVariant var;
  131. if (!index.isValid() || role != Qt::DisplayRole) {
  132. return QVariant();
  133. }
  134. // qInfo() << "index.row(): " << index.row();
  135. // qInfo() << "model->albumCount(): " << this->albumCount();
  136.  
  137. Album* album = this->getAlbum(index.row());
  138. if(!album)
  139. {
  140. qInfo() << "album: null";
  141. return var;
  142. }
  143.  
  144. switch (index.column())
  145. {
  146. case 0:
  147. var.setValue(QString(album->getComposer()));
  148. return var;
  149. case 1:
  150. var.setValue(QString(album->getTitle()));
  151. return var;
  152. case 2:
  153. var.setValue(album->getCost());
  154. return var;
  155. case 3:
  156. var.setValue(album->getRating());
  157. return var;
  158. default:
  159. return QVariant();
  160. }
  161.  
  162. return QVariant();
  163. }
  164.  
  165. int MusicModel::rowCount(const QModelIndex &parent) const
  166. {
  167. return this->albums.count();
  168. }
  169.  
  170. int MusicModel::columnCount(const QModelIndex &parent) const
  171. {
  172. return 4;
  173. }
  174.  
  175. QVariant MusicModel::headerData(int section, Qt::Orientation orientation, int role) const
  176. {
  177. if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
  178. if (section == 0) {
  179. return QString("Composer");
  180. } else if (section == 1) {
  181. return QString("Album Title");
  182. } else if (section == 2) {
  183. return QString("Cost");
  184. } else if (section == 3) {
  185. return QString("Rating");
  186. }
  187. }
  188. return QVariant();
  189. }
  190.  
  191. QList<Album *> MusicModel::getAlbums() const
  192. {
  193. return albums;
  194. }
  195.  
  196. void MusicModel::setAlbums(const QList<Album *> &albums)
  197. {
  198. this->albums = albums;
  199. }
  200.  
  201. void MusicModel::append(Album *album)
  202. {
  203. this->albums.append(album);
  204.  
  205. }
  206.  
  207. int MusicModel::albumCount() const
  208. {
  209. this->albums.count();
  210. }
  211.  
  212. Album *MusicModel::getAlbum(int index) const
  213. {
  214. return this->albums.at(index);
  215. }
  216.  
  217. //dialog.h
  218. #ifndef DIALOG_H
  219. #define DIALOG_H
  220.  
  221. #include <QDialog>
  222. #include "MusicModel.h"
  223.  
  224. QT_BEGIN_NAMESPACE
  225. namespace Ui { class Dialog; }
  226. QT_END_NAMESPACE
  227.  
  228. class Dialog : public QDialog
  229. {
  230. Q_OBJECT
  231.  
  232. public:
  233. Dialog(QWidget *parent = nullptr);
  234. ~Dialog();
  235. void init();
  236.  
  237. private:
  238. Ui::Dialog *ui;
  239. MusicModel *model;
  240.  
  241. private slots:
  242. void addAlbum();
  243.  
  244. };
  245. #endif // DIALOG_H
  246.  
  247. //dialog.cpp
  248. #include "dialog.h"
  249. #include "./ui_dialog.h"
  250.  
  251. Dialog::Dialog(QWidget *parent)
  252. : QDialog(parent)
  253. , ui(new Ui::Dialog)
  254. {
  255. ui->setupUi(this);
  256. init();
  257.  
  258.  
  259. }
  260.  
  261. Dialog::~Dialog()
  262. {
  263. delete ui;
  264. }
  265.  
  266. void Dialog::init()
  267. {
  268. connect(ui->btnAdd,&QPushButton::clicked,this,&Dialog::addAlbum);
  269. model = new MusicModel(this);
  270. Album *queen = new Album(this,"Freddy M","Bohemian Rapsody",12.50,10);
  271. model->append(queen);
  272. ui->tableView->setModel(model);
  273. }
  274.  
  275. void Dialog::addAlbum()
  276. {
  277. qInfo() << "addAlbum";
  278. Album *album = new Album(this);
  279. album->setComposer(ui->txtComposer->text());
  280. album->setTitle(ui->txtAlbumTitle->text());
  281. album->setCost(ui->boxCost->value());
  282. album->setRating(ui->boxRating->value());
  283. model->append(album);
  284.  
  285. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:10: fatal error: QObject: No such file or directory
 #include <QObject>
          ^~~~~~~~~
compilation terminated.
stdout
Standard output is empty