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
Forum Updated to NodeBB v4.3 + New Features

Encoding in the QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
47 Posts 3 Posters 15.4k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 16 Mar 2016, 22:01 last edited by
    #26

    hi
    it sounds like your best way would to read in
    the text file to a hash.
    and use that hash in displayText
    I agree, using if for 40 values would be nasty :)

    How does your text file look like?

    R 1 Reply Last reply 16 Mar 2016, 22:08
    0
    • M mrjj
      16 Mar 2016, 22:01

      hi
      it sounds like your best way would to read in
      the text file to a hash.
      and use that hash in displayText
      I agree, using if for 40 values would be nasty :)

      How does your text file look like?

      R Offline
      R Offline
      ro12man3
      wrote on 16 Mar 2016, 22:08 last edited by
      #27

      @mrjj I didn't create that file, cause I have no idea. Well, every variant will be good. The main is the working code :D

      Do you think that *.txt file should looks so:

      hash.insert("123", "Jack Sparrow");
      hash.insert("134", "Elvis Presley");
      hash.insert("201", "Walter White");
      ........
      

      ?

      M 1 Reply Last reply 16 Mar 2016, 22:18
      0
      • R ro12man3
        16 Mar 2016, 22:08

        @mrjj I didn't create that file, cause I have no idea. Well, every variant will be good. The main is the working code :D

        Do you think that *.txt file should looks so:

        hash.insert("123", "Jack Sparrow");
        hash.insert("134", "Elvis Presley");
        hash.insert("201", "Walter White");
        ........
        

        ?

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 16 Mar 2016, 22:18 last edited by
        #28

        @ro12man3
        Hi
        For me it sounds like you would have a text file with
        1000, NAME1
        1001, NAME2

        so when u see int 1000, u want "NAME" etc.

        So your task is to look at QFile and read in a text file with your format and stuff it into a hash.
        Then use it.

        R 1 Reply Last reply 16 Mar 2016, 22:30
        0
        • M mrjj
          16 Mar 2016, 22:18

          @ro12man3
          Hi
          For me it sounds like you would have a text file with
          1000, NAME1
          1001, NAME2

          so when u see int 1000, u want "NAME" etc.

          So your task is to look at QFile and read in a text file with your format and stuff it into a hash.
          Then use it.

          R Offline
          R Offline
          ro12man3
          wrote on 16 Mar 2016, 22:30 last edited by
          #29

          @mrjj ok, I understand how to read the file and search the values. I don't understand another thing.
          On example=, the function will read the .txt file and it will find some value. Let it be 2900.
          In txt file it will be looking:
          2900, "Jackson"

          How to convert to "Jackson"? I don't know how to write that in a hash. How the hash will look the "convert value"?

          M 1 Reply Last reply 16 Mar 2016, 22:36
          0
          • R ro12man3
            16 Mar 2016, 22:30

            @mrjj ok, I understand how to read the file and search the values. I don't understand another thing.
            On example=, the function will read the .txt file and it will find some value. Let it be 2900.
            In txt file it will be looking:
            2900, "Jackson"

            How to convert to "Jackson"? I don't know how to write that in a hash. How the hash will look the "convert value"?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 16 Mar 2016, 22:36 last edited by mrjj
            #30

            @ro12man3
            say
            u define the hash like
            QHash<int, QString> Hash;
            it means we use int as key and get string as result
            so
            QString Name=hash[2900];
            would give us "Jackson" in name;

            please read doc on how it works.
            http://doc.qt.io/qt-5/qhash.html

            so you would read all lines from text file and insert into hash.

            R 1 Reply Last reply 16 Mar 2016, 22:38
            1
            • M mrjj
              16 Mar 2016, 22:36

              @ro12man3
              say
              u define the hash like
              QHash<int, QString> Hash;
              it means we use int as key and get string as result
              so
              QString Name=hash[2900];
              would give us "Jackson" in name;

              please read doc on how it works.
              http://doc.qt.io/qt-5/qhash.html

              so you would read all lines from text file and insert into hash.

              R Offline
              R Offline
              ro12man3
              wrote on 16 Mar 2016, 22:38 last edited by
              #31

              @mrjj woooow thank you! You are the master! :) I understand, very clever idea!

              I will try that in the morning.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 16 Mar 2016, 22:44 last edited by mrjj
                #32

                well for 40 lookups its more fun that tons of ifs
                if u make a text file with
                ID, NAME
                like
                1000, TOWN
                1001, TOWN2
                1002, TOWN3

                so u need to ( code handwritten fast, not tested. u need to add includes)

                QFile file("c:/mynames.txt");
                if(!file.open(QIODevice::ReadOnly)) {
                QMessageBox::information(0, "error", file.errorString());
                }

                QTextStream in(&file);
                while(!in.atEnd()) {
                QString line = in.readLine(); // get a line from file
                QStringList fields = line.split(","); <<< KEY point. we ask it to split the text at "," to a list
                /// now fields[0] is first part and fields[1] is rest
                // so
                hash[fields[0].toInt()] = fields[1] ; // store name under key
                }

                file.close();

                R 1 Reply Last reply 16 Mar 2016, 22:49
                1
                • M mrjj
                  16 Mar 2016, 22:44

                  well for 40 lookups its more fun that tons of ifs
                  if u make a text file with
                  ID, NAME
                  like
                  1000, TOWN
                  1001, TOWN2
                  1002, TOWN3

                  so u need to ( code handwritten fast, not tested. u need to add includes)

                  QFile file("c:/mynames.txt");
                  if(!file.open(QIODevice::ReadOnly)) {
                  QMessageBox::information(0, "error", file.errorString());
                  }

                  QTextStream in(&file);
                  while(!in.atEnd()) {
                  QString line = in.readLine(); // get a line from file
                  QStringList fields = line.split(","); <<< KEY point. we ask it to split the text at "," to a list
                  /// now fields[0] is first part and fields[1] is rest
                  // so
                  hash[fields[0].toInt()] = fields[1] ; // store name under key
                  }

                  file.close();

                  R Offline
                  R Offline
                  ro12man3
                  wrote on 16 Mar 2016, 22:49 last edited by
                  #33

                  @mrjj thank you very much!! I will try that. If I wiil edit this, I will write here the correct code.
                  Thank you for your helping!

                  M 1 Reply Last reply 16 Mar 2016, 22:54
                  0
                  • R ro12man3
                    16 Mar 2016, 22:49

                    @mrjj thank you very much!! I will try that. If I wiil edit this, I will write here the correct code.
                    Thank you for your helping!

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 16 Mar 2016, 22:54 last edited by
                    #34

                    @ro12man3
                    You are very welcome.
                    One note is that if u read in text lines then
                    it will be text so u need to convert to int for the key if you
                    define the hash like that.
                    QHash<int, QString> Hash;
                    something like
                    hash[ fields[0].toInt() ]=xxx
                    If it gives u grief , let me know as im not 100% sure of syntax as its free writing :)

                    R 1 Reply Last reply 18 Mar 2016, 20:49
                    0
                    • M mrjj
                      16 Mar 2016, 22:54

                      @ro12man3
                      You are very welcome.
                      One note is that if u read in text lines then
                      it will be text so u need to convert to int for the key if you
                      define the hash like that.
                      QHash<int, QString> Hash;
                      something like
                      hash[ fields[0].toInt() ]=xxx
                      If it gives u grief , let me know as im not 100% sure of syntax as its free writing :)

                      R Offline
                      R Offline
                      ro12man3
                      wrote on 18 Mar 2016, 20:49 last edited by ro12man3
                      #35

                      @mrjj Well, I can extract the second field, but it will be all second fields.

                      And I have edited the code:

                      QString MyDelegate::displayText(const QVariant& value, const QLocale& locale) const {
                             QString line;
                              QFile file("C:/QT/Test text/1.txt");
                              if(!file.open(QIODevice::ReadOnly))
                                     qDebug() << "Error opening file";
                              QTextStream in(&file);
                      
                              QHash<int, QString> hash;
                      
                               while(!in.atEnd()) {
                               QString line = in.readLine(); 
                               QStringList fields = line.split(","); 
                               hash[fields[0].toInt()] = fields[1] ; 
                        if (value.toString().compare(fields[0]) == 0)
                          return fields[1];
                        else
                          return value.toString(); 
                      }
                      }
                      

                      And it doesn't work. How to solve that?
                      It was working when I wrote

                       if (value.toString().compare("1000") == 0)
                          return "Jackson";
                      

                      If I write

                      QMessageBox::information(this, fields[0], fields[1] );
                      

                      it is working, but it shows ALL values.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 18 Mar 2016, 21:36 last edited by mrjj
                        #36

                        @ro12man3 said:

                        And it doesn't work. How to solve that?

                        • You really have to provide more info that that :)
                          if you insert qDebug() << "f1=" << fields[0] << "f2=" << fields[1];
                          does it read in the data correctly ? this is critical.

                        • Also you build the hash
                          hash[fields[0].toInt()] = fields[1] ;
                          but don't use it. ? You dont use it to look up the text.
                          QString CityName = hash[value.toInt()];
                          return CityName;

                        • Also you mix reading in the file with compare value. that is wrong.
                          You should not read it in displayText as it will then read file each time
                          it draw that column. that is not so good.
                          Make Hash a member of the class. (in .h) and just use it
                          in displayText. then u can read it once and use it over and over.

                        • Read in the text file in the constructor of the delegate;

                        R 1 Reply Last reply 18 Mar 2016, 22:43
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 18 Mar 2016, 21:56 last edited by
                          #37

                          In addition to what @mrjj wrote: make the hash a member of your delegate.

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

                          1 Reply Last reply
                          0
                          • M mrjj
                            18 Mar 2016, 21:36

                            @ro12man3 said:

                            And it doesn't work. How to solve that?

                            • You really have to provide more info that that :)
                              if you insert qDebug() << "f1=" << fields[0] << "f2=" << fields[1];
                              does it read in the data correctly ? this is critical.

                            • Also you build the hash
                              hash[fields[0].toInt()] = fields[1] ;
                              but don't use it. ? You dont use it to look up the text.
                              QString CityName = hash[value.toInt()];
                              return CityName;

                            • Also you mix reading in the file with compare value. that is wrong.
                              You should not read it in displayText as it will then read file each time
                              it draw that column. that is not so good.
                              Make Hash a member of the class. (in .h) and just use it
                              in displayText. then u can read it once and use it over and over.

                            • Read in the text file in the constructor of the delegate;

                            R Offline
                            R Offline
                            ro12man3
                            wrote on 18 Mar 2016, 22:43 last edited by
                            #38

                            @mrjj said:

                            • You really have to provide more info that that :)
                              if you insert qDebug() << "f1=" << fields[0] << "f2=" << fields[1];
                              does it read in the data correctly ? this is critical.

                            Yes, I can extract this values. I tested that in QMessageBox.
                            More info... http://rghost.ru/7wFZPWC4v This is the project.
                            Download link(http://rghost.ru/download/7wFZPWC4v/ccf2cf56d3bbd353e8db12f377907d858f261e23/ccf2cf56d3bbd353e8db12f377907d858f261e23/myfirstviewEDITED.rar)

                            It's not very difficult code structure, I don't know why it's not working.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 18 Mar 2016, 23:25 last edited by SGaist
                              #39

                              Hi,

                              Because it's not buildable as is.

                              MyDelegate::MyDelegate(QObject *parent)
                                  : QStyledItemDelegate(parent)
                              {
                                  QString line;
                                  QFile file("1.txt");
                                  if(file.open(QIODevice::ReadOnly)) {
                                      QTextStream in(&file);
                                      while(!in.atEnd()) {
                                          QString line = in.readLine(); // get a line from file
                                          QStringList fields = line.split(","); // KEY point. we ask it to split the text at "," to a list
                                          hash[fields[0].toInt()] = fields[1] ; // store name under key
                                      }
                                  } else {
                                      qDebug() << "Error opening file";
                                  }
                              }
                              
                              // this is the key delegate function!
                              QString MyDelegate::displayText(const QVariant& value, const QLocale& locale) const {
                                   return hash.value(value.toInt());
                              }
                              

                              [edit: code cleanup SGaist]

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

                              1 Reply Last reply
                              2
                              • M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 18 Mar 2016, 23:39 last edited by
                                #40

                                oh a tad to late :)
                                https://www.dropbox.com/s/3ajogwsoc7n1zjg/myfirstviewEDITED_hash.zip?dl=0

                                R 2 Replies Last reply 18 Mar 2016, 23:52
                                1
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 18 Mar 2016, 23:43 last edited by
                                  #41

                                  Note that the build problem isn't with the delegate but with the MainWindow missing slot implementation.

                                  @mrjj :-D

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

                                  1 Reply Last reply
                                  1
                                  • M mrjj
                                    18 Mar 2016, 23:39

                                    oh a tad to late :)
                                    https://www.dropbox.com/s/3ajogwsoc7n1zjg/myfirstviewEDITED_hash.zip?dl=0

                                    R Offline
                                    R Offline
                                    ro12man3
                                    wrote on 18 Mar 2016, 23:52 last edited by ro12man3
                                    #42

                                    @mrjj @SGaist oooohhh guys, thank you!! You are amazing!!!

                                    It's working. !!!

                                    The problem is SOLVED! THANK YOU VERY MUCH!!

                                    1 Reply Last reply
                                    0
                                    • M mrjj
                                      18 Mar 2016, 23:39

                                      oh a tad to late :)
                                      https://www.dropbox.com/s/3ajogwsoc7n1zjg/myfirstviewEDITED_hash.zip?dl=0

                                      R Offline
                                      R Offline
                                      ro12man3
                                      wrote on 19 Mar 2016, 09:39 last edited by
                                      #43

                                      @mrjj @SGaist
                                      I started to work and I found one bag.
                                      My table is:
                                      Roberto
                                      Jackson
                                      1002
                                      1

                                      And that's my txt file:
                                      1002,Hello
                                      1,How are you

                                      So the result in the table is
                                      Roberto
                                      Jackson
                                      Hello
                                      How are you

                                      It's good. Correct work.
                                      But if I change 1002 to 10--02 then all const values in the table will become like a second value for 10--02. The result will become:
                                      Hello
                                      Hello
                                      Hello
                                      How are you

                                      You can see that, just edit in table Maria to 10--02 and add 10--02 to txt file. What is that?

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 19 Mar 2016, 10:21 last edited by
                                        #44

                                        @ro12man3 said:

                                        10--02

                                        hi
                                        If you change the first field in file to 10--02
                                        it it no longer a valid integer.
                                        Our hash is Int, String
                                        so that mean use int as key and get back string
                                        when we read "10--02", and say toInt() we get 0 or something undefined.

                                        If you need to use a key that is not really a integer. like 10--02, then
                                        you must change the hash to be Qstring, QString and NOT use toInt() when inserting
                                        into hash.
                                        So make sure your text format fits the Hash or strange things will happen :)

                                        R 1 Reply Last reply 19 Mar 2016, 11:48
                                        2
                                        • M mrjj
                                          19 Mar 2016, 10:21

                                          @ro12man3 said:

                                          10--02

                                          hi
                                          If you change the first field in file to 10--02
                                          it it no longer a valid integer.
                                          Our hash is Int, String
                                          so that mean use int as key and get back string
                                          when we read "10--02", and say toInt() we get 0 or something undefined.

                                          If you need to use a key that is not really a integer. like 10--02, then
                                          you must change the hash to be Qstring, QString and NOT use toInt() when inserting
                                          into hash.
                                          So make sure your text format fits the Hash or strange things will happen :)

                                          R Offline
                                          R Offline
                                          ro12man3
                                          wrote on 19 Mar 2016, 11:48 last edited by
                                          #45

                                          @mrjj Ok. It was

                                          // In .h file:
                                          QHash<int, QString> hash;
                                          ......
                                            hash[fields[0].toInt()] = fields[1] ;
                                          ......
                                          // In .cpp file:
                                          QString mapped = hash.value(value.toInt());
                                          

                                          After editing it's:

                                          // In .h file:
                                          QHash<QString, QString> hash;
                                          ......
                                            hash[fields[0]] = fields[1] ;
                                          ......
                                          // In .cpp file:
                                          QString mapped = hash.value(value);
                                          

                                          Is it correct?

                                          1 Reply Last reply
                                          1

                                          35/47

                                          18 Mar 2016, 20:49

                                          • Login

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