Encoding in the QTableView
Hi! I really need help.
My program is a SQL query area codes
Code:
select city-code from country
The results are displayed in QTableView.
For example, the result code will be printed 1132.
1.What I want: is it possible to to do the encoding of these codes? At the some point it should be specified 1132 = 'Toronto', and QTableView display text value corresponding to this code?
That is, in another related file these codes do variable and QTableView not displayed codes and their meanings. How to do that? I really have no idea...
2.How to make a text value assignment of numbers? I apologize for not knowing such a moment, but never met with such a task) And Google is not particularly give the correct answers.
110 int = 'Toronto'; ?
3.How do these variables would be concerned only this widget?
Re: Encoding in the QTableView
You could do this either in a subclass of the model that does the SQL query, i.e. return the string in data() instead of the number, or with a QIdentityProxyModel subclass that does that.
In either case you need to load this mapping and store it in an associative container, e.g. a QHash, so that you can do a lookup from number to string.
Cheers,
_
Re: Encoding in the QTableView
Take a look at QSqlRelationalTableModel and put the city-to-int mapping in a table.
Re: Encoding in the QTableView
Quote:
Originally Posted by
anda_skoa
You could do this either in a subclass of the model that does the SQL query, i.e. return the string in data() instead of the number, or with a QIdentityProxyModel subclass that does that.
In either case you need to load this mapping and store it in an associative container, e.g. a QHash, so that you can do a lookup from number to string.
Cheers,
_
I have no idea how to make that. I wrote
Code:
QHash< int, QString> hash;
hash.insert(100, "Jackson");
and it doesn't change nothing in my tableview
Quote:
Originally Posted by
ChrisW67
I need to make int-to-city
Int value to text value
110 to Toronto
Re: Encoding in the QTableView
hi i am a beginner, i didn't see any solution to that problem. i also want this question to be answered !
Re: Encoding in the QTableView
Quote:
Originally Posted by
ro12man3
I have no idea how to make that. I wrote
Code:
QHash< int, QString> hash;
hash.insert(100, "Jackson");
and it doesn't change nothing in my tableview
Where to you do the lookup?
Did you go for a subclass of the model class you are currently using or for the proxy model approach?
Cheers,
_
Re: Encoding in the QTableView
this is the code
Code:
void MainWindow::on_pushButton_clicked() // I click and the quiery result is showing in the qtableview
{
QHash< int, QString> hash;
hash.insert(100, "Jackson");
model->setQuery("select city-codes from country");
ui->tableView->setModel(model);
}
Re: Encoding in the QTableView
Lets try this differently: what do you expect this to do?
You are creating a local variable called "hash", insert a data pair into it, then don't use the hash for anything.
Also, since it is on the stack, it's life time ends in line 8.
Cheers,
_
Re: Encoding in the QTableView
Let me try to explain how I would do this, my database table would have two columns:
city_code (int)
city_desc (text)
I'd then fill the table with city_code and city_desc values and change my select to something like "select city_code, city_desc from countries" which would cause your QTableView to have both columns of information displayed.
Trying to do this association outside of the database makes no sense to me. Is there a reason why you can't or haven't done this?