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
Forum Updated to NodeBB v4.3 + New Features

Add hash function to saving in csv

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 2.2k 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.
  • ro12man3R Offline
    ro12man3R Offline
    ro12man3
    wrote on 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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)

      ro12man3R 2 Replies Last reply
      0
      • mrjjM mrjj

        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)

        ro12man3R Offline
        ro12man3R Offline
        ro12man3
        wrote on 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()]
        
        mrjjM 1 Reply Last reply
        0
        • ro12man3R ro12man3

          @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()]
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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.
          ro12man3R 1 Reply Last reply
          0
          • mrjjM mrjj

            @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.
            ro12man3R Offline
            ro12man3R Offline
            ro12man3
            wrote on 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

            mrjjM 1 Reply Last reply
            0
            • ro12man3R ro12man3

              @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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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.

              ro12man3R 1 Reply Last reply
              1
              • mrjjM mrjj

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

                ro12man3R Offline
                ro12man3R Offline
                ro12man3
                wrote on last edited by
                #7

                @mrjj Thank you, now I understand.

                1 Reply Last reply
                0
                • mrjjM mrjj

                  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)

                  ro12man3R Offline
                  ro12man3R Offline
                  ro12man3
                  wrote on 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.

                  mrjjM 1 Reply Last reply
                  0
                  • ro12man3R ro12man3

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

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 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 + "\"";
                    
                    ro12man3R 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @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 + "\"";
                      
                      ro12man3R Offline
                      ro12man3R Offline
                      ro12man3
                      wrote on last edited by
                      #10

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

                      mrjjM 1 Reply Last reply
                      0
                      • ro12man3R ro12man3

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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @ro12man3 said:
                        super :)

                        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