Add hash function to saving in csv
-
wrote on 29 Mar 2016, 15:14 last edited by ro12man3
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?
-
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) -
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)wrote on 29 Mar 2016, 16:38 last edited by 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()]
-
@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()]
- 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.
- Why I can't just paste that code with little transformation
-
- 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.
wrote on 29 Mar 2016, 17:44 last edited by@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 - Why I can't just paste that code with little transformation
-
@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 insufficientlyYou 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.
-
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.
-
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)wrote on 29 Mar 2016, 19:35 last edited by@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.
-
@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.
@ro12man3
oh the test had only one COL so didn't checkyou do
for( int c = 0; c < ui->tableView->horizontalHeader()->count()
...
so you should only do it when c == the_right_colyou could
... if (c == 3) strList << "\"" + mapped + "\""; else strList << "\"" + value + "\"";
-
@ro12man3
oh the test had only one COL so didn't checkyou do
for( int c = 0; c < ui->tableView->horizontalHeader()->count()
...
so you should only do it when c == the_right_colyou could
... if (c == 3) strList << "\"" + mapped + "\""; else strList << "\"" + value + "\"";
-
@ro12man3 said:
super :)
1/11