jeudi 13 septembre 2018

Adding CheckBox with QSqlQueryModel shown in QTableview and other colunms are empty

I'm using the code in this answer to add checkbox in tableview. I wanna show it in the first column.

Here is my code:

mysqlquerymodel.h

#ifndef MYSQLQUERYMODEL_H
#define MYSQLQUERYMODEL_H
#include <QObject>
#include <QMap>
#include <QModelIndex>
#include <QSqlQueryModel>

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);
private:
    QMap<int, Qt::CheckState> check_state_map;
};
#endif // MYSQLQUERYMODEL_H

mysqlquerymodel.cpp

#include "mysqlquerymodel.h"
Qt::ItemFlags MySqlQueryModel::flags(const QModelIndex & index) const
{
    if (!index.isValid())
        return 0;
    if (index.column() == 0)
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
    return  Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant MySqlQueryModel::data(const QModelIndex & index, int role) const
{
    if (!index.isValid())
        return QVariant();
    if(role== Qt::CheckStateRole)
    {
        if(index.column() == 0)
        {
            if (check_state_map.contains(index.row()))
                return check_state_map[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked;
            return Qt::Unchecked;
        }
    }
    return QVariant();
}

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

manage.cpp

void manage::on_selectBtn_clicked()
{
    QString query=QString("add_time,client_id,client_product_id,continent,country,region,city,ip,app_name,dev_os,dev_os_ver,dev_model,tusin_note,classify_note,detect_note,runtime_stats FROM ts_identify_record WHERE %1 %2 %3 %4 %5 %6 %7 ORDER BY add_time")
            .arg(timeRange()).arg(ipAddress()).arg(cellphone()).arg(product()).arg(country()).arg(province()).arg(city());
    qDebug()<<statment;
    QSqlDatabase db=QSqlDatabase::database();
    MySqlQueryModel *model=new MySqlQueryModel;
    model->setQuery(query,db);
    ui->tableView->setModel(model);
    ui->tableView->resizeColumnsToContents();
}

When i click the button , only the first column shows checkbox and other columns are empty. But the line count is correct and the checkbox can be clicked. it wouldn't be a problem when i just use QSqlQueryModel.

another weird thing is that my select result should be 16 columns but the first column is filled with checkbox . when i add model->insertColumn(0); , tableview shows 17 columns instead.

How could it happen and how to fix it?




Aucun commentaire:

Enregistrer un commentaire