no selectionModel in c++

This commit is contained in:
Chris Cochrun 2023-02-23 14:54:43 -06:00
parent 0b438b48b3
commit 787b0eea42
2 changed files with 79 additions and 45 deletions

View file

@ -716,8 +716,8 @@ SongProxyModel::SongProxyModel(QObject *parent)
:QSortFilterProxyModel(parent) :QSortFilterProxyModel(parent)
{ {
m_songModel = new SongSqlModel; m_songModel = new SongSqlModel;
m_selectionModel = new QItemSelectionModel; // m_selectionModel = new QItemSelectionModel;
m_selectionModel->setModel(this); // m_selectionModel->setModel(this);
setSourceModel(m_songModel); setSourceModel(m_songModel);
setDynamicSortFilter(true); setDynamicSortFilter(true);
setFilterRole(Qt::UserRole + 1); setFilterRole(Qt::UserRole + 1);
@ -728,9 +728,35 @@ SongSqlModel *SongProxyModel::songModel() {
return m_songModel; return m_songModel;
} }
// QVariant SongProxyModel::data(const QModelIndex &index, int role) const {
// if (role == Qt::UserRole + 13)
// return selected(index.row());
// return m_songModel->data(index, role);
// }
// QHash<int, QByteArray> SongProxyModel::roleNames() const
// {
// QHash<int, QByteArray> names;
// names[Qt::UserRole] = "id";
// names[Qt::UserRole + 1] = "title";
// names[Qt::UserRole + 2] = "lyrics";
// names[Qt::UserRole + 3] = "author";
// names[Qt::UserRole + 4] = "ccli";
// names[Qt::UserRole + 5] = "audio";
// names[Qt::UserRole + 6] = "vorder";
// names[Qt::UserRole + 7] = "background";
// names[Qt::UserRole + 8] = "backgroundType";
// names[Qt::UserRole + 9] = "horizontalTextAlignment";
// names[Qt::UserRole + 10] = "verticalTextAlignment";
// names[Qt::UserRole + 11] = "font";
// names[Qt::UserRole + 12] = "fontSize";
// names[Qt::UserRole + 13] = "selected";
// return names;
// }
QModelIndex SongProxyModel::idx(int row) { QModelIndex SongProxyModel::idx(int row) {
QModelIndex idx = index(row, 0); QModelIndex idx = index(row, 0);
qDebug() << idx; // qDebug() << idx;
return idx; return idx;
} }
@ -745,43 +771,50 @@ void SongProxyModel::deleteSong(const int &row) {
model->deleteSong(row); model->deleteSong(row);
} }
bool SongProxyModel::selected(int row) { // bool SongProxyModel::selected(const int &row) {
QModelIndex idx = index(row, 0); // QModelIndex idx = index(row, 0);
return m_selectionModel->isSelected(idx); // return m_selectionModel->isSelected(idx);
} // }
bool SongProxyModel::setSelected(int row, bool select) { // bool SongProxyModel::setSelected(const int &row, const bool &select) {
if (selected(row) == select) // if (selected(row) == select)
return false; // return false;
QModelIndex idx = index(row, 0); // QModelIndex idx = index(row, 0);
if (select) // if (select)
m_selectionModel->select(idx, QItemSelectionModel::SelectCurrent); // m_selectionModel->select(idx, QItemSelectionModel::SelectCurrent);
else // else
m_selectionModel->select(idx, QItemSelectionModel::Deselect); // m_selectionModel->select(idx, QItemSelectionModel::Deselect);
emit selectedChanged(select); // emit selectedChanged(select);
return true; // return true;
} // }
void SongProxyModel::select(int id) { // void SongProxyModel::select(int id) {
QModelIndex idx = index(id, 0); // QModelIndex idx = index(id, 0);
//check to make sure this item isn't already selected // //check to make sure this item isn't already selected
if (m_selectionModel->isSelected(idx)) // if (m_selectionModel->isSelected(idx))
return; // return;
// deselect all items // // deselect all items
QModelIndex first = index(0, 0); // QModelIndex first = index(0, 0);
QModelIndex last = index(rowCount() - 1, 0); // QModelIndex last = index(rowCount() - 1, 0);
QItemSelection all = new QItemSelection(first, last); // QItemSelection all(first, last);
m_selectionModel->select(all, QItemSelectionModel::Deselect); // m_selectionModel->select(all, QItemSelectionModel::Deselect);
m_selectionModel->select(idx, QItemSelectionModel::SelectCurrent); // m_selectionModel->select(idx, QItemSelectionModel::SelectCurrent);
} // emit selectedChanged(true);
// }
void SongProxyModel::selectSongs(int row) { // void SongProxyModel::selectSongs(int row) {
auto model = qobject_cast<QItemSelectionModel *>(m_selectionModel); // auto model = qobject_cast<QItemSelectionModel *>(m_selectionModel);
// deselect all items // // deselect all items
QModelIndex first = index(0, 0); // QModelIndex first = index(0, 0);
QModelIndex last = index(rowCount() - 1, 0); // QModelIndex last = index(rowCount() - 1, 0);
QItemSelection all = new QItemSelection(first, last); // QItemSelection all(first, last);
m_selectionModel->select(all, QItemSelectionModel::Deselect); // m_selectionModel->select(all, QItemSelectionModel::Deselect);
}
// emit selectedChanged(true);
// }
// bool SongProxyModel::hasSelection() {
// return m_selectionModel->hasSelection();
// }

View file

@ -113,7 +113,7 @@ class SongProxyModel : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(SongSqlModel *songModel READ songModel) Q_PROPERTY(SongSqlModel *songModel READ songModel)
Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectedChanged) // Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectedChanged)
public: public:
explicit SongProxyModel(QObject *parent = nullptr); explicit SongProxyModel(QObject *parent = nullptr);
@ -121,8 +121,7 @@ public:
SongSqlModel *songModel(); SongSqlModel *songModel();
Q_INVOKABLE QModelIndex idx(int row); Q_INVOKABLE QModelIndex idx(int row);
bool selected(int row); // Q_INVOKABLE bool selected(const int &row);
bool setSelected(int row, bool select);
// QVariant data(const QModelIndex &index, int role) const override; // QVariant data(const QModelIndex &index, int role) const override;
// QHash<int, QByteArray> roleNames() const override; // QHash<int, QByteArray> roleNames() const override;
@ -130,15 +129,17 @@ public:
public slots: public slots:
Q_INVOKABLE QVariantMap getSong(const int &row); Q_INVOKABLE QVariantMap getSong(const int &row);
Q_INVOKABLE void deleteSong(const int &row); Q_INVOKABLE void deleteSong(const int &row);
Q_INVOKABLE void select(int row); // Q_INVOKABLE void select(int row);
Q_INVOKABLE void selectSongs(int row); // Q_INVOKABLE void selectSongs(int row);
// Q_INVOKABLE bool setSelected(const int &row, const bool &select);
// Q_INVOKABLE bool hasSelection();
signals: signals:
void selectedChanged(bool selected); // Q_INVOKABLE void selectedChanged(bool selected);
private: private:
SongSqlModel *m_songModel; SongSqlModel *m_songModel;
QItemSelectionModel *m_selectionModel; // QItemSelectionModel *m_selectionModel;
// bool m_selected; // bool m_selected;
}; };