-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seperate class for checkbox delegate.
- Loading branch information
Showing
9 changed files
with
193 additions
and
83 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <QtWidgets> | ||
#include <QSql> | ||
#include <QSqlQuery> | ||
#include "checkbox_model.h" | ||
|
||
checkbox_model::checkbox_model(QObject *parent) : QSqlQueryModel(parent) | ||
{ | ||
} | ||
|
||
Qt::ItemFlags checkbox_model::flags(const QModelIndex &index) const | ||
{ | ||
Qt::ItemFlags flags = QSqlQueryModel::flags(index); | ||
if(index.column() == 2) | ||
return QSqlQueryModel::flags(index) | Qt::ItemIsUserCheckable; | ||
return QSqlQueryModel::flags(index); | ||
} | ||
|
||
QVariant checkbox_model::data(const QModelIndex &index, int role) const | ||
{ | ||
QVariant value = QSqlQueryModel::data(index, role); | ||
//if(index.column() == 2 && role == Qt::CheckStateRole) | ||
if(index.column() == 2 && role == Qt::DisplayRole) | ||
{ | ||
//implement your logic to return the check state | ||
//.... | ||
//if (index.column() == 2) //add a checkbox to cell(1,0) | ||
if(value.toInt()==1){ | ||
return Qt::Checked; | ||
} | ||
else if(value.toInt()==0){ | ||
return Qt::Unchecked; | ||
} | ||
} | ||
} | ||
|
||
//setData() is called each time the user edits the table i.e. save data refresh view | ||
bool checkbox_model::setData(const QModelIndex &index, const QVariant &value, int /*role*/ ) | ||
{ | ||
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0); | ||
int id = QSqlQueryModel::data(primaryKeyIndex).toInt(); | ||
clear(); | ||
//if(index.column() == 2 && role == Qt::CheckStateRole) | ||
if(index.column() == 2) | ||
{ | ||
bool ok; | ||
ok = setCheckbox(id, value.toInt()); | ||
return ok; | ||
} | ||
else | ||
{ | ||
return false; | ||
//QSqlQueryModel::setData(index, value, role); | ||
} | ||
return true; | ||
} | ||
|
||
|
||
bool checkbox_model::setCheckbox(int task_id, int checkbox_value) | ||
{ | ||
QSqlQuery query; | ||
query.prepare("UPDATE task_list SET finished = :finished WHERE id = :id"); | ||
query.bindValue(":finished", checkbox_value); | ||
query.bindValue(":id", task_id); | ||
return query.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef CHECKBOX_MODEL_H | ||
#define CHECKBOX_MODEL_H | ||
|
||
#include <QSqlQueryModel> | ||
|
||
class checkbox_model : public QSqlQueryModel | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
checkbox_model(QObject *parent = 0); | ||
|
||
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; | ||
QVariant data(const QModelIndex &item, int role) const Q_DECL_OVERRIDE; | ||
bool setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE; | ||
//~checkbox_model(); | ||
private: | ||
bool setCheckbox(int task_id, int checkbox_value); | ||
}; | ||
|
||
#endif // CHECKBOX_MODEL_H | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.