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. Add hash function to saving in csv

Add hash function to saving in csv

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 2.1k Views
  • 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.
  • R Offline
    R Offline
    ro12man3
    wrote on 29 Mar 2016, 15:14 last edited by ro12man3
    #1

    Hi!
    A week ago, I was able to solve one problem. ( https://forum.qt.io/topic/65110/encoding-in-the-qtableview ). Thanks to @mrjj and @SGaist .

    In the previous function I wrote:

    ui->tableView_3->setModel(model);
    ui->tableView_3->setItemDelegateForColumn(3, new MyDelegate(this));
    

    But now I want to save that table in csv format with the hash function.
    This is the working code that can save table into csv file without transforming.

    void MainWindow::on_pushButton3_2_clicked()
    { QSqlQueryModel * model = new QSqlQueryModel(0);
    QString filename=QFileDialog::getSaveFileName(
                this,
                tr("Save as"),
                "C://",
                "CSV files (*.csv)"        );
        QFile f(filename);
            if( f.open( QIODevice::WriteOnly ) )
            {
                QTextStream ts( &f );
                QStringList strList;
                strList << "\" \"";
                for( int c = 0; c < ui->tableView_3->horizontalHeader()->count(); ++c )
                    strList << "\""+ui->tableView_3->model()->headerData(c, Qt::Horizontal).toString()+"\"";
                ts << strList.join( ";" )+"\n";
                for( int r = 0; r < ui->tableView_3->verticalHeader()->count(); ++r )
                {
                    strList.clear();
                    strList << "\""+ui->tableView_3->model()->headerData(r, Qt::Vertical).toString()+"\"";
                    for( int c = 0; c < ui->tableView_3->horizontalHeader()->count(); ++c )
                    {
                        strList << "\""+ui->tableView_3->model()->data(ui->tableView_3->model()->index(r, c), Qt::DisplayRole).toString()+"\"";
                    }
                    ts << strList.join( ";" )+"\n";
                }
                f.close();
            }
    }
    
    

    It's working, but how to add here hash function that will transform values?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Mar 2016, 15:23 last edited by
      #2

      hi
      you just need to call like before
      QString CityName = hash[value];
      so u need the same hash here.
      U can either make a new one here.
      (reading 40 values is no big deal)
      or re-arrange the code so this function/class can also use the existing hash.

      QString CityName = hash[value];
      value is
      ui->tableView_3->model()->data(ui->tableView_3->model()->index(r, c), Qt::DisplayRole).toString()
      when "c" is the correct column. ( u must use if to test for that)

      R 2 Replies Last reply 29 Mar 2016, 16:38
      0
      • M mrjj
        29 Mar 2016, 15:23

        hi
        you just need to call like before
        QString CityName = hash[value];
        so u need the same hash here.
        U can either make a new one here.
        (reading 40 values is no big deal)
        or re-arrange the code so this function/class can also use the existing hash.

        QString CityName = hash[value];
        value is
        ui->tableView_3->model()->data(ui->tableView_3->model()->index(r, c), Qt::DisplayRole).toString()
        when "c" is the correct column. ( u must use if to test for that)

        R Offline
        R Offline
        ro12man3
        wrote on 29 Mar 2016, 16:38 last edited by ro12man3
        #3

        @mrjj said:

        or re-arrange the code so this function/class can also use the existing hash.

        QString CityName = hash[value];
        value is
        ui->tableView_3->model()->data(ui->tableView_3->model()->index(r, c), Qt::DisplayRole).toString()
        when "c" is the correct column. ( u must use if to test for that)

        I think I don't understand you well. Why I can't just paste that code with little transformation?(if it need to use) .

        ui->tableView_2->setItemDelegateForColumn(3, new MyDelegate(this));
        

        And do you mean that I should write that? But it won't work( or not)?

        QString CityName = hash[ ui->tableView_3->model()->data(ui->tableView_3->model()->index(r, c), Qt::DisplayRole).toString()]
        
        M 1 Reply Last reply 29 Mar 2016, 17:12
        0
        • R ro12man3
          29 Mar 2016, 16:38

          @mrjj said:

          or re-arrange the code so this function/class can also use the existing hash.

          QString CityName = hash[value];
          value is
          ui->tableView_3->model()->data(ui->tableView_3->model()->index(r, c), Qt::DisplayRole).toString()
          when "c" is the correct column. ( u must use if to test for that)

          I think I don't understand you well. Why I can't just paste that code with little transformation?(if it need to use) .

          ui->tableView_2->setItemDelegateForColumn(3, new MyDelegate(this));
          

          And do you mean that I should write that? But it won't work( or not)?

          QString CityName = hash[ ui->tableView_3->model()->data(ui->tableView_3->model()->index(r, c), Qt::DisplayRole).toString()]
          
          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Mar 2016, 17:12 last edited by mrjj
          #4

          @ro12man3

          • Why I can't just paste that code with little transformation
            Because the delegate replace the text (data) when showing it.
            Now you are reading data directly to save it and
            ui->tableView_3->model()->data return the original data, there is no delegate in between to
            translate.
            So you must do it. (again)
            And yes like you show, but ONLY when c is the COL you want to convert.
            and then write out cityname and not directly from data.
          R 1 Reply Last reply 29 Mar 2016, 17:44
          0
          • M mrjj
            29 Mar 2016, 17:12

            @ro12man3

            • Why I can't just paste that code with little transformation
              Because the delegate replace the text (data) when showing it.
              Now you are reading data directly to save it and
              ui->tableView_3->model()->data return the original data, there is no delegate in between to
              translate.
              So you must do it. (again)
              And yes like you show, but ONLY when c is the COL you want to convert.
              and then write out cityname and not directly from data.
            R Offline
            R Offline
            ro12man3
            wrote on 29 Mar 2016, 17:44 last edited by
            #5

            @mrjj I really don't understand the method for solving that problem.

            This is the project with all functions: saving in csv(after clicking on the button) and the hash function(thanks :D)
            http://rghost.ru/62XsMLmDk
            Can you watch what I should to change? My skills are better now, but insufficiently

            M 1 Reply Last reply 29 Mar 2016, 18:14
            0
            • R ro12man3
              29 Mar 2016, 17:44

              @mrjj I really don't understand the method for solving that problem.

              This is the project with all functions: saving in csv(after clicking on the button) and the hash function(thanks :D)
              http://rghost.ru/62XsMLmDk
              Can you watch what I should to change? My skills are better now, but insufficiently

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 29 Mar 2016, 18:14 last edited by
              #6

              @ro12man3

              You must use the hash again,
              so i moved the code into function.
              that way we can also create a hash when exporting.

              https://www.dropbox.com/s/6oqbcvcz006kzeh/myfristview_hash3.zip?dl=0

              Note I copy the hash, so if u had 10000000 items, it would not be the way to do it.

              R 1 Reply Last reply 29 Mar 2016, 18:57
              1
              • M mrjj
                29 Mar 2016, 18:14

                @ro12man3

                You must use the hash again,
                so i moved the code into function.
                that way we can also create a hash when exporting.

                https://www.dropbox.com/s/6oqbcvcz006kzeh/myfristview_hash3.zip?dl=0

                Note I copy the hash, so if u had 10000000 items, it would not be the way to do it.

                R Offline
                R Offline
                ro12man3
                wrote on 29 Mar 2016, 18:57 last edited by
                #7

                @mrjj Thank you, now I understand.

                1 Reply Last reply
                0
                • M mrjj
                  29 Mar 2016, 15:23

                  hi
                  you just need to call like before
                  QString CityName = hash[value];
                  so u need the same hash here.
                  U can either make a new one here.
                  (reading 40 values is no big deal)
                  or re-arrange the code so this function/class can also use the existing hash.

                  QString CityName = hash[value];
                  value is
                  ui->tableView_3->model()->data(ui->tableView_3->model()->index(r, c), Qt::DisplayRole).toString()
                  when "c" is the correct column. ( u must use if to test for that)

                  R Offline
                  R Offline
                  ro12man3
                  wrote on 29 Mar 2016, 19:35 last edited by
                  #8

                  @mrjj I found the bag. It is changing ALL columns, how to do it only for one column?
                  I have edited the code on that:

                    QString value = uimodel->data(uimodel->index(r, 3), Qt::DisplayRole).toString();
                  

                  but then it will duplicate that column to all another.

                  M 1 Reply Last reply 29 Mar 2016, 19:54
                  0
                  • R ro12man3
                    29 Mar 2016, 19:35

                    @mrjj I found the bag. It is changing ALL columns, how to do it only for one column?
                    I have edited the code on that:

                      QString value = uimodel->data(uimodel->index(r, 3), Qt::DisplayRole).toString();
                    

                    but then it will duplicate that column to all another.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 29 Mar 2016, 19:54 last edited by
                    #9

                    @ro12man3
                    oh the test had only one COL so didn't check

                    you do
                    for( int c = 0; c < ui->tableView->horizontalHeader()->count()
                    ...
                    so you should only do it when c == the_right_col

                    you could

                    ...
                         if (c == 3)
                            strList << "\"" + mapped + "\"";
                            else
                            strList << "\"" + value + "\"";
                    
                    R 1 Reply Last reply 29 Mar 2016, 19:59
                    1
                    • M mrjj
                      29 Mar 2016, 19:54

                      @ro12man3
                      oh the test had only one COL so didn't check

                      you do
                      for( int c = 0; c < ui->tableView->horizontalHeader()->count()
                      ...
                      so you should only do it when c == the_right_col

                      you could

                      ...
                           if (c == 3)
                              strList << "\"" + mapped + "\"";
                              else
                              strList << "\"" + value + "\"";
                      
                      R Offline
                      R Offline
                      ro12man3
                      wrote on 29 Mar 2016, 19:59 last edited by
                      #10

                      @mrjj yeeeep! it's working! thank you!

                      M 1 Reply Last reply 29 Mar 2016, 20:02
                      0
                      • R ro12man3
                        29 Mar 2016, 19:59

                        @mrjj yeeeep! it's working! thank you!

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 29 Mar 2016, 20:02 last edited by
                        #11

                        @ro12man3 said:
                        super :)

                        1 Reply Last reply
                        0

                        1/11

                        29 Mar 2016, 15:14

                        • Login

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