Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Problem with QTableView model setData
Forum Updated to NodeBB v4.3 + New Features

[Solved]Problem with QTableView model setData

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 9.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sebastian90
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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);
        }@

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sebastian90
          wrote on last edited by
          #4

          [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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sebastian90
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              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.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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. ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sebastian90
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved