0

I am referring to the code in this answer and this to create a tableview with checkboxes.

I am trying to create a table, that shows a list of student in my database with checkbox alongside.

Now that i have done that,

Question is:

  1. How do I save the value of the checkboxes to my sql database?

  2. How do I present the initial state of checkboxes from the value in database? (Eg: 1 for checkbox is checked, 0 for unchecked)

(I am open to any ways of implementing the above requirement, so feel free to provide your solution other than the codes below) Here is my code:

mysqlquerymodel.h

class MySqlQueryModel : public QSqlQueryModel
{
    Q_OBJECT
public:
    explicit MySqlQueryModel(QObject *parent = 0);
    Qt::ItemFlags flags(const QModelIndex & index) const;
    QVariant data(const QModelIndex & index, int role) const;
    bool setData(const QModelIndex & index, const QVariant & value, int role);
    QMap<int, Qt::CheckState> check_state_map;
};

mysqlquerymodel.cpp (ignore the qdebug its for checking)

MySqlQueryModel::MySqlQueryModel(QObject *parent) : QSqlQueryModel(parent), check_state_map()
{
}

Qt::ItemFlags MySqlQueryModel::flags(const QModelIndex & index) const
{
    if (!index.isValid()) {
        qDebug()<<("item1");
        return nullptr;
    }
    if (index.column() == 2){
        qDebug()<<("item2");
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
    }
    qDebug()<<("item3");
    return  Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant MySqlQueryModel::data(const QModelIndex & index, int role) const
{
    if (!index.isValid()){
        qDebug()<<("data1");
        return QVariant();
    }
    if(role== Qt::CheckStateRole)
    {
        if(index.column() == 2)
        {
            if (check_state_map.contains(index.row())){
                qDebug()<<("data2");
                return check_state_map[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked;
            }
            qDebug()<<("data3");
            return Qt::Unchecked;
        }
    }
    return QSqlQueryModel::data(index,role);//!!!!!
}

bool MySqlQueryModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
    if(!index.isValid()){
        qDebug()<<("set1");
        return false;
    }
    if (role == Qt::CheckStateRole && index.column() == 2)
    {
        qDebug()<<("set2");
        check_state_map[index.row()] = (value == Qt::Checked ? Qt::Checked : Qt::Unchecked);
    }
    qDebug()<<("set3");
    return true;
}

I am trying to implement it in tawindow.ui

tawindow.cpp

//load table
void TAWindow::on_loadBtn_clicked(){
            conn.connOpen();
            QSqlQuery* qry = new QSqlQuery(conn.mydb);

            qry->prepare("select * from students");
            qry->exec();
            tablemodel->setQuery(*qry);
            ui->tableView->setModel(tablemodel);

            conn.connClose();
        }

    //save table and update database
    void TAWindow::on_saveBtn_clicked()
    {

    }

Thanks in advance.

Chi Siong
  • 1
  • 1

1 Answers1

0

you can use ui->combobox->ischecked(); to check if the comboBox is checked or not.

and to store the state of the comboBox you can use a variable.

Something like this :

QString var;

if(ui->comboBox->isChecked())
{
    var = "Checked";
}
else
{
    var = "Not Checked";
}

and store var in you database.

Abdelbaki Boukerche
  • 1,602
  • 1
  • 8
  • 20