PDA

View Full Version : Encoding in the QTableView



ro12man3
14th March 2016, 18:35
Hi! I really need help.

My program is a SQL query area codes


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?

anda_skoa
14th March 2016, 19:08
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,
_

ChrisW67
14th March 2016, 20:58
Take a look at QSqlRelationalTableModel and put the city-to-int mapping in a table.

ro12man3
15th March 2016, 20:51
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



QHash< int, QString> hash;
hash.insert(100, "Jackson");

and it doesn't change nothing in my tableview


Take a look at QSqlRelationalTableModel and put the city-to-int mapping in a table.

I need to make int-to-city
Int value to text value
110 to Toronto

mynamebilalzahid
16th March 2016, 07:16
hi i am a beginner, i didn't see any solution to that problem. i also want this question to be answered !

anda_skoa
16th March 2016, 08:42
I have no idea how to make that. I wrote



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,
_

ro12man3
16th March 2016, 08:54
this is the code

void MainWindow::on_pushButton_clicked() // I click and the quiery result is showing in the qtableview
{
QSqlQueryModel * model = new QSqlQueryModel(0);
QHash< int, QString> hash;
hash.insert(100, "Jackson");
model->setQuery("select city-codes from country");
ui->tableView->setModel(model);
}

anda_skoa
16th March 2016, 09:44
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,
_

jefftee
16th March 2016, 23:11
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?