cant read data from my model of QIdentityProxyModel [ASolved]



  • Hi everyone, im having trouble trying to fetch the data from my QTableView. I have the following structure:

    I get the data from the database with a QSqlQueryModel, then I create my model extending from QIdentityProxyModel and set as its source model the Qsqlquerymodel. Finally I set my model as the model of the tableview. The thing is i can get the rows and columns but not the data of a cell.

    Here is the code from:

    widget using the table:
    @
    model_bajo_stock = new QSqlQueryModel ();
    if(db.isOpen ()){
    model_bajo_stock->setQuery("SELECT Descripcion, Stock_unidades, fecha_modificacion, proveedor FROM TABLE_PRODUCTOS WHERE stock_unidades < 50 ORDER BY Descripcion", db);
    if (model_bajo_stock->lastError().type() != QSqlError::NoError)
    ui->label_error->setText(model_bajo_stock->lastError().text());
    else{
    myBajoStock= new MyModelView();
    myBajoStock->setSourceModel(model_bajo_stock);
    ui->tableViewBajoStock->setModel(myBajoStock);
    }
    ui->tableViewBajoStock->setAlternatingRowColors(true);
    ui->tableViewBajoStock->show();
    @

    my model:
    @#include "mymodelview.h"
    #include <QDebug>
    MyModelView::MyModelView(QObject *parent) :
    QIdentityProxyModel(parent)
    {
    }

    QVariant MyModelView::data(const QModelIndex &proxyIndex, int role) const{
    if (role == Qt::BackgroundRole && proxyIndex.column()==1){
    int value= proxyIndex.model()->data(proxyIndex.model()->index(proxyIndex.row(),1),Qt::DisplayRole).toInt();
    if (value < 6){
    return QBrush(Qt::red);
    }
    }
    return QIdentityProxyModel::data(proxyIndex, role);
    }
    @

    This code works perfect (painting the background as i want) but the problem arises when i try to fetch a value that, to my understanding is not set. For example:
    @ if (role == Qt::FontRole && proxyIndex.column()==0){
    int value= proxyIndex.model()->data(proxyIndex.model()->index(proxyIndex.row(),1),Qt::DisplayRole).toInt();
    if (value < 6){
    return QFont("Courier", 8, QFont::Bold, true);
    }
    }@

    The line trying to get the value throws an error telling me that that field doesn't exists (seg fault). I want to get the data from that line as i do on the first if but i cant.

    Thank you all in advance

    Solution:

    @QVariant MyModelView::data(const QModelIndex &proxyIndex, int role) const{
    if (role == Qt::BackgroundRole && proxyIndex.column()==1){
    int value= QIdentityProxyModel::data(index(proxyIndex.row(), 1), Qt::DisplayRole).toInt();
    if (value < 6){
    return QBrush(Qt::red);
    }
    }
    if((role == Qt::FontRole) && (proxyIndex.column() == 0))
    {
    int value = QIdentityProxyModel::data(index(proxyIndex.row(), 1), Qt::DisplayRole).toInt();
    if(value < 6)
    return QFont("Courier", 11, QFont::Bold, false);
    }
    return QIdentityProxyModel::data(proxyIndex, role);
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.