Results 1 to 9 of 9

Thread: Encoding in the QTableView

  1. #1
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Encoding in the QTableView

    Hi! I really need help.

    My program is a SQL query area codes

    Qt Code:
    1. select city-code from country
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Encoding in the QTableView

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

  4. #4
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Encoding in the QTableView

    Quote Originally Posted by anda_skoa View Post
    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

    Qt Code:
    1. QHash< int, QString> hash;
    2. hash.insert(100, "Jackson");
    To copy to clipboard, switch view to plain text mode 
    and it doesn't change nothing in my tableview

    Quote Originally Posted by ChrisW67 View Post
    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

  5. #5
    Join Date
    Mar 2016
    Posts
    5
    Qt products
    Platforms
    Symbian S60

    Default 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 !

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Encoding in the QTableView

    Quote Originally Posted by ro12man3 View Post
    I have no idea how to make that. I wrote

    Qt Code:
    1. QHash< int, QString> hash;
    2. hash.insert(100, "Jackson");
    To copy to clipboard, switch view to plain text mode 
    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,
    _

  7. #7
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Encoding in the QTableView

    this is the code
    Qt Code:
    1. void MainWindow::on_pushButton_clicked() // I click and the quiery result is showing in the qtableview
    2. {
    3. QSqlQueryModel * model = new QSqlQueryModel(0);
    4. QHash< int, QString> hash;
    5. hash.insert(100, "Jackson");
    6. model->setQuery("select city-codes from country");
    7. ui->tableView->setModel(model);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 16th March 2016 at 09:43. Reason: changed [qtclass] to [code]

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. UTF-7 encoding support
    By #Dragon in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2015, 18:22
  2. encoding problem
    By lexqqq in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2010, 23:41
  3. QTextCodec bad encoding?
    By ComaWhite in forum Newbie
    Replies: 0
    Last Post: 18th September 2009, 06:15
  4. QTableView encoding problem
    By SudaNix in forum Newbie
    Replies: 7
    Last Post: 26th February 2008, 14:19
  5. Encoding a QString to No.?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 30th August 2007, 19:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.