[Solved]Problem with QTableView model setData



  • Hi everyone, i have a little trouble with QTableView's model setData method. I want to set a column of QTableView model with amounts, i mean data is displayed as it should, but i want to use toCurrencyString method to set the current column data with the formatted QString returned by locale().toCurrencyString (if its showing 2000 i want to set the 2.000$ data to that column) and to all rows of the column in one bucle, but it just sets the data on the first row, not on all rows of that column. Here's what i have so far :

    @
    void Principal::setColumnaMonto(QTableView *tabla, int column)
    {
    QAbstractItemModel *modeloTabla = tabla->model();

    for(int i = 0; i < modeloTabla->rowCount(); i++){
        double amount = modeloTabla->data(modeloTabla->index(i, column)).toDouble();
        QString newAmount = local.toCurrencyString(amount);
    
        if(modeloTabla->setData(modeloTabla->index(i, column), newAmount))
              qDebug() << "Data changed";
        else qDebug() << "Weird data not changed";
    
    }
    
    tabla->setModel(modeloTabla);
    

    }
    @

    Output :

    !http://k41.kn3.net/9/C/7/5/2/6/B7F.jpg(Alter)!
    I need a fast solution(im a little short of time), so i hope someone can give me a hand with this.


  • Lifetime Qt Champion

    Hi,

    Don't do it at the model level, create a QStyledItemDelegate where you reimplement the displayText function



  • Just try the sample below. Is the loop counting so many times ? Can you print the row and column values also ? This may help in troubleshooting.

    Just try the below as well.

    @ QTableView *view = new QTableView;
    QStandardItemModel *model1 = new QStandardItemModel(5,5);
    view->setModel(model1);
    view->show();

    for (int i=0;i<model1->rowCount();i++){
    QModelIndex index = model1->index(i,2);
    QString val = local.toCurrencyString(i);
    QVariant dat(val);
    model1->setData(index,dat);
    }@



  • [quote author="SGaist" date="1406367537"]Hi,

    Don't do it at the model level, create a QStyledItemDelegate where you reimplement the displayText function[/quote]

    i had no idea of the existence of the QStyledItemDelegate, i'll read the docs about this.



  • [quote author="Dheerendra" date="1406374483"]Just try the sample below. Is the loop counting so many times ? Can you print the row and column values also ? This may help in troubleshooting.

    Just try the below as well.

    @ QTableView *view = new QTableView;
    QStandardItemModel *model1 = new QStandardItemModel(5,5);
    view->setModel(model1);
    view->show();

    for (int i=0;i<model1->rowCount();i++){
    QModelIndex index = model1->index(i,2);
    QString val = local.toCurrencyString(i);
    QVariant dat(val);
    model1->setData(index,dat);
    }@
    [/quote]

    Hi Dheerendra, yes i tried to debug it that way, and it prints all the correct values and indexes, it loops the rowCount() times and the console output is this :
    @
    Data changed
    Weird data not changed -> how can i see the reason?
    @
    I just cant figure out the reason of this error, im gonna try your solution but the model for my table has no fixed rowCount, its a sqlRelationalTableModel that brings data from database, ill try the solution of SGaist as well and i’ll put the result here, thanks for the help.



  • You create seperate TableView inside the setColumnMonto and see whether it works. This is for just testing purpose. You can do as I suggested in my example above. This will help us to understand if there is any issue with data etc. Since it is simple string data, I don't see a reason to have seperate delegate for this.


  • Lifetime Qt Champion

    Because it's not the model's role to provide a specialized visualization like that. What happens if you use another view on the same model that just has to show a curve based on these number ? Should it parse the data, remove the currency etc. ?



  • I created a QStyleItemDelegate as SGaist sugested, much more cleaner way to achieve what i wanted, thanks for the help.


Log in to reply
 

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