Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Encoding in the QTableView
QtWS25 Last Chance

Encoding in the QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
47 Posts 3 Posters 14.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM mrjj

    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

    ro12man3R Offline
    ro12man3R Offline
    ro12man3
    wrote on last edited by ro12man3
    #3

    @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-----10013

    And I need convert that into the words. For example, 10012=Jackson, 10013=White, so the result must look so:
    Toronto-----Jackson
    New Yourk-----White

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #4

      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.html

      to understand the roles of model, view and delegate.

      it's not an option to include the names in the DB?

      ro12man3R 1 Reply Last reply
      1
      • mrjjM mrjj

        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.html

        to understand the roles of model, view and delegate.

        it's not an option to include the names in the DB?

        ro12man3R Offline
        ro12man3R Offline
        ro12man3
        wrote on last edited by
        #5

        @mrjj thank you, I will try.
        No, I should solve the problem this way :)

        mrjjM 1 Reply Last reply
        0
        • ro12man3R ro12man3

          @mrjj thank you, I will try.
          No, I should solve the problem this way :)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @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.

          ro12man3R 1 Reply Last reply
          0
          • mrjjM mrjj

            @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.

            ro12man3R Offline
            ro12man3R Offline
            ro12man3
            wrote on last edited by
            #7

            @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?

            mrjjM 1 Reply Last reply
            0
            • ro12man3R ro12man3

              @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?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @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 :)

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #9

                Hi,

                You should build a hash of your data and use it to map your numbers to your city.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                ro12man3R 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  You should build a hash of your data and use it to map your numbers to your city.

                  ro12man3R Offline
                  ro12man3R Offline
                  ro12man3
                  wrote on last edited by
                  #10

                  @SGaist if I understand you correctly, I need to use QHash, yes?

                  mrjjM 1 Reply Last reply
                  0
                  • ro12man3R ro12man3

                    @SGaist if I understand you correctly, I need to use QHash, yes?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @ro12man3
                    hi yes QHash would be fine
                    http://www.bogotobogo.com/Qt/Qt5_QHash.php

                    ro12man3R 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @ro12man3
                      hi yes QHash would be fine
                      http://www.bogotobogo.com/Qt/Qt5_QHash.php

                      ro12man3R Offline
                      ro12man3R Offline
                      ro12man3
                      wrote on last edited by
                      #12

                      @mrjj thank you very much! But how integrate that with QTableView?

                      mrjjM 1 Reply Last reply
                      0
                      • ro12man3R ro12man3

                        @mrjj thank you very much! But how integrate that with QTableView?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @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.

                        ro12man3R 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @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.

                          ro12man3R Offline
                          ro12man3R Offline
                          ro12man3
                          wrote on last edited by
                          #14

                          @mrjj unfortunately, it doesn't work. I read that for solving that problem I need to change TableView to TableWidget and use QTableWidgetItem. How to change the numbers in that variant?

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @ro12man3 said:

                            How to change the numbers in that variant?

                            Hi Im not sure what you ask about ?

                            QTableWidgetItem is not a variant.

                            ro12man3R 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @ro12man3 said:

                              How to change the numbers in that variant?

                              Hi Im not sure what you ask about ?

                              QTableWidgetItem is not a variant.

                              ro12man3R Offline
                              ro12man3R Offline
                              ro12man3
                              wrote on last edited by
                              #16

                              @mrjj well... I talk about how to make changing the numbers to text in my tableview(or widget). I wrote

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

                              and it doesn't change nothing in the table

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                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 ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                ro12man3R 1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  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 ?

                                  ro12man3R Offline
                                  ro12man3R Offline
                                  ro12man3
                                  wrote on last edited by ro12man3
                                  #18

                                  @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);
                                  }
                                  
                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #19

                                    That hash is destroyed at the end of on_pushButton_clicked

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    ro12man3R 1 Reply Last reply
                                    0
                                    • SGaistS SGaist

                                      That hash is destroyed at the end of on_pushButton_clicked

                                      ro12man3R Offline
                                      ro12man3R Offline
                                      ro12man3
                                      wrote on last edited by
                                      #20

                                      @SGaist so what I need to do for the correct changing values in my tableview?
                                      Where I need to write the QHash function?

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #21

                                        Either in your QStyledItemDelegate or as @mrjj suggested pass it to the delegate e.g. at construction time.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        ro12man3R 1 Reply Last reply
                                        0
                                        • SGaistS SGaist

                                          Either in your QStyledItemDelegate or as @mrjj suggested pass it to the delegate e.g. at construction time.

                                          ro12man3R Offline
                                          ro12man3R Offline
                                          ro12man3
                                          wrote on last edited by
                                          #22

                                          @SGaist what will be more simple? Cause I don't know how to make that. Sorry about my not big skills in qt

                                          mrjjM 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved