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 16.2k 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.
  • 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
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #23

                    In your QStyledItemDelegate, read your file and build the hash with its content. Then in your displayText reimplementation return _hash.value(value.toInt(), tr("Not found"));

                    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
                    • ro12man3R ro12man3

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

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

                      @ro12man3
                      hi
                      Starting with models and view are not best intro to Qt.
                      They are very powerful but does demand some c++ skills and reading the docs.

                      Sometimes code is easier to understand so I made u sample with @SGaist help.

                      It has a list of names and if it sees Lars, it replaced it with BANNED.
                      For your code, you will use the hash to look up the name from the code.

                      https://www.dropbox.com/s/vsiqjrr41jhafqo/myfirstview.zip?dl=0

                      the key points to understand is:
                      we make a new class to override the default behaviour

                      class MyDelegate : public QStyledItemDelegate {
                      public:
                      MyDelegate(QObject* parent)
                      : QStyledItemDelegate(parent) {}
                      virtual QString displayText(const QVariant& value, const QLocale& locale) const;
                      };

                      We set this delegate ONLY to the column we need.

                      • ui->tableView->setItemDelegateForColumn(0, new MyDelegate(this));

                      the displayText is the key function.:

                      QString MyDelegate::displayText(const QVariant& value, const QLocale& locale) const {
                      if (value.toString().compare("Lars") == 0) // if found
                      return "BANNED";
                      else
                      return value.toString(); // else just return data unchanged
                      }

                      Here u will replace the city-code with names using the hash.

                      Hope this helps you.

                      ro12man3R 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @ro12man3
                        hi
                        Starting with models and view are not best intro to Qt.
                        They are very powerful but does demand some c++ skills and reading the docs.

                        Sometimes code is easier to understand so I made u sample with @SGaist help.

                        It has a list of names and if it sees Lars, it replaced it with BANNED.
                        For your code, you will use the hash to look up the name from the code.

                        https://www.dropbox.com/s/vsiqjrr41jhafqo/myfirstview.zip?dl=0

                        the key points to understand is:
                        we make a new class to override the default behaviour

                        class MyDelegate : public QStyledItemDelegate {
                        public:
                        MyDelegate(QObject* parent)
                        : QStyledItemDelegate(parent) {}
                        virtual QString displayText(const QVariant& value, const QLocale& locale) const;
                        };

                        We set this delegate ONLY to the column we need.

                        • ui->tableView->setItemDelegateForColumn(0, new MyDelegate(this));

                        the displayText is the key function.:

                        QString MyDelegate::displayText(const QVariant& value, const QLocale& locale) const {
                        if (value.toString().compare("Lars") == 0) // if found
                        return "BANNED";
                        else
                        return value.toString(); // else just return data unchanged
                        }

                        Here u will replace the city-code with names using the hash.

                        Hope this helps you.

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

                        @mrjj ohhh thank you! It works!! Thank you very much.

                        But I have 40 values that I will need to change(if it will be in quiery result).

                        1. I can't write 40 "else if".. "else if"... "else if".... That's unlogic.
                        2. I need to write all that values in support *.txt file(or another, the main is a simple format for editing), cause if I will write all that values in the program and then I will need to add new values...That's will be very uncomfortable.

                        I see the method of the working the function so:
                        The function is searching the values(from *.txt file) that has a quiery result. If it will find, then it will change to another every value.

                        But...how make that in reality?

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 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?

                          ro12man3R 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            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?

                            ro12man3R Offline
                            ro12man3R Offline
                            ro12man3
                            wrote on 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");
                            ........
                            

                            ?

                            mrjjM 1 Reply Last reply
                            0
                            • ro12man3R ro12man3

                              @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");
                              ........
                              

                              ?

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 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.

                              ro12man3R 1 Reply Last reply
                              0
                              • mrjjM mrjj

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

                                ro12man3R Offline
                                ro12man3R Offline
                                ro12man3
                                wrote on 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"?

                                mrjjM 1 Reply Last reply
                                0
                                • ro12man3R ro12man3

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

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 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.

                                  ro12man3R 1 Reply Last reply
                                  1
                                  • mrjjM mrjj

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

                                    ro12man3R Offline
                                    ro12man3R Offline
                                    ro12man3
                                    wrote on 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
                                    • mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 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();

                                      ro12man3R 1 Reply Last reply
                                      1
                                      • mrjjM mrjj

                                        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();

                                        ro12man3R Offline
                                        ro12man3R Offline
                                        ro12man3
                                        wrote on 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!

                                        mrjjM 1 Reply Last reply
                                        0
                                        • ro12man3R ro12man3

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

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 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 :)

                                          ro12man3R 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