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. QDataWidgetMapper messed column indexes
Qt 6.11 is out! See what's new in the release blog

QDataWidgetMapper messed column indexes

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 3 Posters 13.8k Views 1 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.
  • Oak77O Offline
    Oak77O Offline
    Oak77
    wrote on last edited by
    #9

    OK, further testing showed that there's a value of the lost last column No. 29 located under the index 10. So at least I have all the columns. However, I still have no idea what I did to cause this nasty little mess. I wonder if there could be any sort of sort affecting the order of the columns.

    1 Reply Last reply
    0
    • mrjjM mrjj

      @Oak77
      Hi
      Im also a bit confused as I dont get why the cols dont match up.
      Do you add anything to the QSqlTableModel manually or does it all comes from the connected table ?

      Oak77O Offline
      Oak77O Offline
      Oak77
      wrote on last edited by
      #10

      @mrjj said in QDataWidgetMapper messed column indexes:

      @Oak77
      Hi
      Im also a bit confused as I dont get why the cols dont match up.
      Do you add anything to the QSqlTableModel manually or does it all comes from the connected table ?

      Thanks for looking at it. No, I didn't add anything to the data.

      I just noticed that I have sorting applied to this particular data table. It shouldn't cause this situation, because it sorts rows, not columns.. AFAIK there's no way to sort columns in the QHeaderData.

      I wonder, if this could be caused by providing a QSqlTableModel (inherits QSqlQueryModel, that in turn QAbstractTableModel and that QAbstractItemModel ), while the documentation states a QAbstractItemModel should be supplied.

      JonBJ 1 Reply Last reply
      0
      • Oak77O Oak77

        @mrjj said in QDataWidgetMapper messed column indexes:

        @Oak77
        Hi
        Im also a bit confused as I dont get why the cols dont match up.
        Do you add anything to the QSqlTableModel manually or does it all comes from the connected table ?

        Thanks for looking at it. No, I didn't add anything to the data.

        I just noticed that I have sorting applied to this particular data table. It shouldn't cause this situation, because it sorts rows, not columns.. AFAIK there's no way to sort columns in the QHeaderData.

        I wonder, if this could be caused by providing a QSqlTableModel (inherits QSqlQueryModel, that in turn QAbstractTableModel and that QAbstractItemModel ), while the documentation states a QAbstractItemModel should be supplied.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #11

        @Oak77
        No, that should be fine. Try removing whatever sorting you are applying and see where you are.

        Oak77O 1 Reply Last reply
        0
        • JonBJ JonB

          @Oak77
          No, that should be fine. Try removing whatever sorting you are applying and see where you are.

          Oak77O Offline
          Oak77O Offline
          Oak77
          wrote on last edited by
          #12

          @JonB said in QDataWidgetMapper messed column indexes:

          @Oak77
          No, that should be fine. Try removing whatever sorting you are applying and see where you are.

          OK, tested, removing model.setSort(28,Qt.AscendingOrder) didn't change anything.

          JonBJ 1 Reply Last reply
          0
          • Oak77O Oak77

            @JonB said in QDataWidgetMapper messed column indexes:

            @Oak77
            No, that should be fine. Try removing whatever sorting you are applying and see where you are.

            OK, tested, removing model.setSort(28,Qt.AscendingOrder) didn't change anything.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #13

            @Oak77
            If you have a QSqlTableModel, are you relying in the columns coming back in some order which they are not? Do you know what SELECT statement is being issued?

            Oak77O 1 Reply Last reply
            0
            • JonBJ JonB

              @Oak77
              If you have a QSqlTableModel, are you relying in the columns coming back in some order which they are not? Do you know what SELECT statement is being issued?

              Oak77O Offline
              Oak77O Offline
              Oak77
              wrote on last edited by Oak77
              #14

              @JonB said in QDataWidgetMapper messed column indexes:

              @Oak77
              If you have a QSqlTableModel, are you relying in the columns coming back in some order which they are not? Do you know what SELECT statement is being issued?

              I think the answer is No, I don't rely on any order it would figure out and I supply list of columns. I have a data access class and method which receives a list of columns and assigns them to the model:

                      for i, ColName in enumerate(ColumnList):
                          model.setHeaderData(i, QtCore.Qt.Horizontal, ColName)
                          print("index = " + str(i) + "       Col.name = " + ColName)
              

              Note, that I added the print just now and it shows the same stuff we see in the end, when listing the model from within the self.mapper, the instance of the QDataWidgetMapper.

              The main thing is though, that when listing the columns from within the self.mapper, it should be what should be used to map the columns. Or am I wrong? So it almost looks like the QDataWidgetMapper class does this to the order of the columns while processing the self.model().

              1 Reply Last reply
              0
              • Oak77O Offline
                Oak77O Offline
                Oak77
                wrote on last edited by Oak77
                #15

                OK, one more way to debug that I found: I used QDataWidgetMapper.mappedWidgetAt(int) to list columns that are mapped, like that:

                        for i in range(mapper.model().columnCount()):
                            w = mapper.mappedWidgetAt(i)
                            if w != None:
                                print(str(i) + "    " + w.objectName())
                

                And the result is:

                1   CName
                2   IsGid
                3   IsDet
                4   IsNew
                5   IsPick
                9   LL
                10   LW               # <<<<<<<< Note the last column is here
                11   GidCW
                12   DetWidth
                13   DetHeight
                14   Units
                15   IsEtb
                16   IsNL
                17   FS
                18   FV
                19   NF
                20   DP
                21   TT
                22   FTab
                23   FVC
                24   FDC
                25   FFC
                26   FFV
                27   EF
                28   GidOrder
                29   DetOrder
                

                So the mapper has a different order from what is in the self.model().

                mrjjM JonBJ 2 Replies Last reply
                0
                • Oak77O Oak77

                  OK, one more way to debug that I found: I used QDataWidgetMapper.mappedWidgetAt(int) to list columns that are mapped, like that:

                          for i in range(mapper.model().columnCount()):
                              w = mapper.mappedWidgetAt(i)
                              if w != None:
                                  print(str(i) + "    " + w.objectName())
                  

                  And the result is:

                  1   CName
                  2   IsGid
                  3   IsDet
                  4   IsNew
                  5   IsPick
                  9   LL
                  10   LW               # <<<<<<<< Note the last column is here
                  11   GidCW
                  12   DetWidth
                  13   DetHeight
                  14   Units
                  15   IsEtb
                  16   IsNL
                  17   FS
                  18   FV
                  19   NF
                  20   DP
                  21   TT
                  22   FTab
                  23   FVC
                  24   FDC
                  25   FFC
                  26   FFV
                  27   EF
                  28   GidOrder
                  29   DetOrder
                  

                  So the mapper has a different order from what is in the self.model().

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

                  Hi
                  Im a bit lost here too as i see no reason
                  that mapper.model() should have different col order than QSqlTableModel ( self.model() ?)
                  since we supply the mapper with the model as pointer so it should be same model.

                  1 Reply Last reply
                  1
                  • Oak77O Oak77

                    OK, one more way to debug that I found: I used QDataWidgetMapper.mappedWidgetAt(int) to list columns that are mapped, like that:

                            for i in range(mapper.model().columnCount()):
                                w = mapper.mappedWidgetAt(i)
                                if w != None:
                                    print(str(i) + "    " + w.objectName())
                    

                    And the result is:

                    1   CName
                    2   IsGid
                    3   IsDet
                    4   IsNew
                    5   IsPick
                    9   LL
                    10   LW               # <<<<<<<< Note the last column is here
                    11   GidCW
                    12   DetWidth
                    13   DetHeight
                    14   Units
                    15   IsEtb
                    16   IsNL
                    17   FS
                    18   FV
                    19   NF
                    20   DP
                    21   TT
                    22   FTab
                    23   FVC
                    24   FDC
                    25   FFC
                    26   FFV
                    27   EF
                    28   GidOrder
                    29   DetOrder
                    

                    So the mapper has a different order from what is in the self.model().

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #17

                    @Oak77
                    It is as @mrjj & I have said. It should all work. QDataWidgetMapper just works off the model's columns. If you want to resolve this you really need to stop with the fragments of code you have shown so far and rebuild from a small, standalone example, which will work, till you encounter whatever it is you have changed. We will not be able to say anything from the various fragments you have given here.

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

                      Hi
                      I tried to recreate it so I used your result list here to make the table

                      alt text

                      I then mapped it to a bunch a lineEdits, skipping 5,6,7 ( well 4,5,6)

                         int range = 0;
                          for (auto name : names) {
                              QLineEdit *line = new QLineEdit(this);
                              ui->vertL->addWidget(line);
                              line->setPlaceholderText(name);
                              if (range != 5 && range != 6 && range != 7 )
                                  map->addMapping(line, cc++ );
                              else
                                  cc++;
                      
                              range++;
                          }
                          map->toFirst();
                      

                      alt text

                      but the values always come as expected

                      alt text

                      Then i tried to dump the cols like you did

                      
                          for (int cc  = 0; cc < map->model()->columnCount(); cc++ ) {
                            qDebug().noquote().nospace() << cc << " = " <<  map->model()->headerData(cc, Qt::Horizontal) ;
                          }
                      

                      But they still come in the expected order.

                      0 = QVariant(QString, ID)
                      1 = QVariant(QString, CName)
                      2 = QVariant(QString, IsGid)
                      3 = QVariant(QString, IsDet)
                      4 = QVariant(QString, IsNew)
                      5 = QVariant(QString, IsPick)
                      6 = QVariant(QString, FID)
                      7 = QVariant(QString, PID)
                      8 = QVariant(QString, CT)
                      9 = QVariant(QString, LL)
                      10 = QVariant(QString, GidCW)
                      11 = QVariant(QString, DetWidth)
                      12 = QVariant(QString, DetHeight)
                      13 = QVariant(QString, Units)
                      14 = QVariant(QString, IsEtb)
                      15 = QVariant(QString, IsNL)
                      16 = QVariant(QString, FS)
                      17 = QVariant(QString, FV)
                      18 = QVariant(QString, NF)
                      19 = QVariant(QString, DP)
                      20 = QVariant(QString, TT)
                      21 = QVariant(QString, FTab)
                      22 = QVariant(QString, FVC)
                      23 = QVariant(QString, FDC)
                      24 = QVariant(QString, FFC)
                      25 = QVariant(QString, FFV)
                      26 = QVariant(QString, EF)
                      27 = QVariant(QString, GidOrder)
                      28 = QVariant(QString, DetOrder)
                      29 = QVariant(QString, LW)

                      So I really cant find a way to see what you see. Sadly.

                      Oak77O 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        Hi
                        I tried to recreate it so I used your result list here to make the table

                        alt text

                        I then mapped it to a bunch a lineEdits, skipping 5,6,7 ( well 4,5,6)

                           int range = 0;
                            for (auto name : names) {
                                QLineEdit *line = new QLineEdit(this);
                                ui->vertL->addWidget(line);
                                line->setPlaceholderText(name);
                                if (range != 5 && range != 6 && range != 7 )
                                    map->addMapping(line, cc++ );
                                else
                                    cc++;
                        
                                range++;
                            }
                            map->toFirst();
                        

                        alt text

                        but the values always come as expected

                        alt text

                        Then i tried to dump the cols like you did

                        
                            for (int cc  = 0; cc < map->model()->columnCount(); cc++ ) {
                              qDebug().noquote().nospace() << cc << " = " <<  map->model()->headerData(cc, Qt::Horizontal) ;
                            }
                        

                        But they still come in the expected order.

                        0 = QVariant(QString, ID)
                        1 = QVariant(QString, CName)
                        2 = QVariant(QString, IsGid)
                        3 = QVariant(QString, IsDet)
                        4 = QVariant(QString, IsNew)
                        5 = QVariant(QString, IsPick)
                        6 = QVariant(QString, FID)
                        7 = QVariant(QString, PID)
                        8 = QVariant(QString, CT)
                        9 = QVariant(QString, LL)
                        10 = QVariant(QString, GidCW)
                        11 = QVariant(QString, DetWidth)
                        12 = QVariant(QString, DetHeight)
                        13 = QVariant(QString, Units)
                        14 = QVariant(QString, IsEtb)
                        15 = QVariant(QString, IsNL)
                        16 = QVariant(QString, FS)
                        17 = QVariant(QString, FV)
                        18 = QVariant(QString, NF)
                        19 = QVariant(QString, DP)
                        20 = QVariant(QString, TT)
                        21 = QVariant(QString, FTab)
                        22 = QVariant(QString, FVC)
                        23 = QVariant(QString, FDC)
                        24 = QVariant(QString, FFC)
                        25 = QVariant(QString, FFV)
                        26 = QVariant(QString, EF)
                        27 = QVariant(QString, GidOrder)
                        28 = QVariant(QString, DetOrder)
                        29 = QVariant(QString, LW)

                        So I really cant find a way to see what you see. Sadly.

                        Oak77O Offline
                        Oak77O Offline
                        Oak77
                        wrote on last edited by Oak77
                        #19

                        @mrjj said in QDataWidgetMapper messed column indexes:

                        Hi
                        I tried to recreate it so I used your result list here to make the table
                        ....
                        So I really cant find a way to see what you see. Sadly.

                        Thank you very much! I'll study your example and I'll try to find a difference. In the meanwhile I set to make a simplified example width a Chinook database table, but I didn't finished it yet. It will take me probably few days to study everything.

                        mrjjM 1 Reply Last reply
                        0
                        • Oak77O Oak77

                          @mrjj said in QDataWidgetMapper messed column indexes:

                          Hi
                          I tried to recreate it so I used your result list here to make the table
                          ....
                          So I really cant find a way to see what you see. Sadly.

                          Thank you very much! I'll study your example and I'll try to find a difference. In the meanwhile I set to make a simplified example width a Chinook database table, but I didn't finished it yet. It will take me probably few days to study everything.

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

                          @Oak77
                          Hi
                          You are welcome to have my test project to play around with but
                          it's c++ so didn't include it as you seem to use python so I guess you don't have a c++ compiler
                          just like i don't have python installed.

                          Oak77O 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @Oak77
                            Hi
                            You are welcome to have my test project to play around with but
                            it's c++ so didn't include it as you seem to use python so I guess you don't have a c++ compiler
                            just like i don't have python installed.

                            Oak77O Offline
                            Oak77O Offline
                            Oak77
                            wrote on last edited by Oak77
                            #21

                            @mrjj said in QDataWidgetMapper messed column indexes:

                            @Oak77
                            it's c++ so didn't include it as you seem to use python so I guess you don't have a c++ compiler
                            just like i don't have python installed.

                            That's perfectly fine, I have zero C++ skills but I can read it a bit. This proved that I have to look for a source of the problem within my code.

                            I found I was wrong in one particular point: Setting HeaderData does not define the columns of the query. I found that coincidentally with a different example, where it clearly shown way more columns than what was set. This also means that the query itself might be already having a different order of columns. I'll have a look at it once behind the incident PC to confirm it.

                            However, it raises another question and that is how a user is supposed to define the columns without using setQuery, because the documentation clearly states using it should be avoided. I'll open another thread for this particular interesting question.

                            EDIT: Well, thinking about that, itwon't change the fact, that model shows different order from mapping, because the query is defined before the model is created and consequently used in the mapper.

                            JonBJ 1 Reply Last reply
                            0
                            • Oak77O Oak77

                              @mrjj said in QDataWidgetMapper messed column indexes:

                              @Oak77
                              it's c++ so didn't include it as you seem to use python so I guess you don't have a c++ compiler
                              just like i don't have python installed.

                              That's perfectly fine, I have zero C++ skills but I can read it a bit. This proved that I have to look for a source of the problem within my code.

                              I found I was wrong in one particular point: Setting HeaderData does not define the columns of the query. I found that coincidentally with a different example, where it clearly shown way more columns than what was set. This also means that the query itself might be already having a different order of columns. I'll have a look at it once behind the incident PC to confirm it.

                              However, it raises another question and that is how a user is supposed to define the columns without using setQuery, because the documentation clearly states using it should be avoided. I'll open another thread for this particular interesting question.

                              EDIT: Well, thinking about that, itwon't change the fact, that model shows different order from mapping, because the query is defined before the model is created and consequently used in the mapper.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #22

                              @Oak77 said in QDataWidgetMapper messed column indexes:

                              Setting HeaderData does not define the columns of the query

                              Indeed, header data is just header data. It has no effect on any query. I did wonder if you were doing something like this, but we couldn't tell from the code you gave.... I'll go and look at your new question...

                              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