Encoding in the QTableView
-
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.- 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...
-
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'; ? -
How do these variables would be concerned only this widget?
-
hi
Normally u would use SQL to return a query result where the
country ID would be used to look up the name from another table.
such as this
http://www.w3schools.com/sql/sql_join.asp
where customer ID is used to get the customer name.Its not really the job of QTableView as it just to view the data so its
better to do before.Im not really sure what u ask in 3:
sorry -
hi
Normally u would use SQL to return a query result where the
country ID would be used to look up the name from another table.
such as this
http://www.w3schools.com/sql/sql_join.asp
where customer ID is used to get the customer name.Its not really the job of QTableView as it just to view the data so its
better to do before.Im not really sure what u ask in 3:
sorry@mrjj all this codes(10012, 10013 etc) are not in my DB. I will write them in the program or in the support *.txt file.
I make sql query, then I see the result in QTableView. On example, smth like that:
Toronto-----10012
New York-----10013And I need convert that into the words. For example, 10012=Jackson, 10013=White, so the result must look so:
Toronto-----Jackson
New Yourk-----White -
ok. then you should look into a
QAbstractItemDelegate custom class.
like
http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html
(here a value is mapped to some stars)the delegate can change what is displayed.
So in ur case look up in some table the city name and display that instead.
You must read about
http://doc.qt.io/qt-5.5/model-view-programming.htmlto understand the roles of model, view and delegate.
it's not an option to include the names in the DB?
-
ok. then you should look into a
QAbstractItemDelegate custom class.
like
http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html
(here a value is mapped to some stars)the delegate can change what is displayed.
So in ur case look up in some table the city name and display that instead.
You must read about
http://doc.qt.io/qt-5.5/model-view-programming.htmlto understand the roles of model, view and delegate.
it's not an option to include the names in the DB?
-
@ro12man3
ok. the first delegate can be slightly trick if u are not deeply into c++
as it involved subclassing and virtual function override.
looking at samples is a good idea. -
@mrjj I have seen the link about star delegate, but I'm not sure how to edit that for me. Can you write simple code, please?
And what to do if I have 40 codes that I will need to change?@ro12man3
hi
Delegates are a bit involved to code so small samples is not so easy.'
The main concept for you would to be override
http://doc.qt.io/qt-5.5/qstyleditemdelegate.html#displayText
so when view ask how to show data , you can then
replace the ID with the correct text.for 40 codes , you might need a lookup table as pure ifs
if ( ID == 1000) return "CITY";
will be a bit verbose :) -
Hi,
You should build a hash of your data and use it to map your numbers to your city.
-
@ro12man3
hi yes QHash would be fine
http://www.bogotobogo.com/Qt/Qt5_QHash.php -
@ro12man3
hi yes QHash would be fine
http://www.bogotobogo.com/Qt/Qt5_QHash.php -
@ro12man3
well when u create your delegate object
you also give it a Hash list with the town names.
so in displayText()
it can look up in the list the name to use from the number from DB. -
@ro12man3 said:
How to change the numbers in that variant?
Hi Im not sure what you ask about ?
QTableWidgetItem is not a variant.
-
Where's that hash located ?
Did you set your custom QStyledItem delegate on the correct column of your QTableView ?
Are you sure that the volume contains the correct values ? -
Where's that hash located ?
Did you set your custom QStyledItem delegate on the correct column of your QTableView ?
Are you sure that the volume contains the correct values ?@SGaist 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); }
-
That hash is destroyed at the end of on_pushButton_clicked