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. QListView with custom QStyledItemDelegate not working
Forum Updated to NodeBB v4.3 + New Features

QListView with custom QStyledItemDelegate not working

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 2 Posters 1.1k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #7

    @JonB said in QListView with custom QStyledItemDelegate not working:

    OK, so at least we have discovered the QSFPM is not relevant! So you are saying I can set up a model (SQL), view it in a QListView, make an edit, and at the end of the edit it will not longer show anything for the edited item. But the item is there, if you click to re-edit it.

    Yes that's right.

    @JonB said in QListView with custom QStyledItemDelegate not working:

    Can you temporarily try your editing on a simple QSTM instead, does that behave the same or differently? :)

    I tried it with a QSTM and see volla my delegate works as I expect!

    @JonB said in QListView with custom QStyledItemDelegate not working:

    You have not said what column you are actually showing from your QSRTM? Is it the foreign-key-mapped one?

    Yes I mapped it to another table. That's to QSRTM Model:

    QSqlRelationalTableModel *DatabaseManager::germanFungusNameDataModel() const
    {
        QSqlRelationalTableModel *model = new QSqlRelationalTableModel;
        model->setTable("Deutscher_Pilzname");
        model->setRelation(model->fieldIndex("Deutscher_name_id"), QSqlRelation("Deutscher_Name", "Id", "Name"));
        model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
        model->setSort(0, Qt::AscendingOrder);
        model->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
        model->select();
        while (model->canFetchMore()) {
            model->fetchMore();
            model->relationModel(0)->fetchMore();
        }
        return model;
    }
    

    @JonB said in QListView with custom QStyledItemDelegate not working:

    Not sure, but I think QSRTM may have issues if you edit that, you're only supposed to change which one by picking from e.g. a combo box, not editing

    Okay...but then it is not clear to me how to edit the RelationModel? Which ways are there to display the data in the QListview? The QSRTM is a N:M relation and I don't know how to implement the QT / C++...

    @JonB said in QListView with custom QStyledItemDelegate not working:

    Are your edits ever committed to the database or never (during what you show)?

    Later, I will save all changes via a button and call submitAll()

    JonBJ 1 Reply Last reply
    0
    • ? A Former User

      @JonB said in QListView with custom QStyledItemDelegate not working:

      OK, so at least we have discovered the QSFPM is not relevant! So you are saying I can set up a model (SQL), view it in a QListView, make an edit, and at the end of the edit it will not longer show anything for the edited item. But the item is there, if you click to re-edit it.

      Yes that's right.

      @JonB said in QListView with custom QStyledItemDelegate not working:

      Can you temporarily try your editing on a simple QSTM instead, does that behave the same or differently? :)

      I tried it with a QSTM and see volla my delegate works as I expect!

      @JonB said in QListView with custom QStyledItemDelegate not working:

      You have not said what column you are actually showing from your QSRTM? Is it the foreign-key-mapped one?

      Yes I mapped it to another table. That's to QSRTM Model:

      QSqlRelationalTableModel *DatabaseManager::germanFungusNameDataModel() const
      {
          QSqlRelationalTableModel *model = new QSqlRelationalTableModel;
          model->setTable("Deutscher_Pilzname");
          model->setRelation(model->fieldIndex("Deutscher_name_id"), QSqlRelation("Deutscher_Name", "Id", "Name"));
          model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
          model->setSort(0, Qt::AscendingOrder);
          model->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
          model->select();
          while (model->canFetchMore()) {
              model->fetchMore();
              model->relationModel(0)->fetchMore();
          }
          return model;
      }
      

      @JonB said in QListView with custom QStyledItemDelegate not working:

      Not sure, but I think QSRTM may have issues if you edit that, you're only supposed to change which one by picking from e.g. a combo box, not editing

      Okay...but then it is not clear to me how to edit the RelationModel? Which ways are there to display the data in the QListview? The QSRTM is a N:M relation and I don't know how to implement the QT / C++...

      @JonB said in QListView with custom QStyledItemDelegate not working:

      Are your edits ever committed to the database or never (during what you show)?

      Later, I will save all changes via a button and call submitAll()

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

      @Gabber said in QListView with custom QStyledItemDelegate not working:

      I tried it with a QSTM and see volla my delegate works as I expect!

      So the QSqlRelationalModel/QSqlRelations are (somehow) the issue!

      You still have not said which column you are editing? The Deutscher_name_id, which is a foreign key?? That column in the Deutscher_Pilzname table? Or the Id/name column in Deutscher_Name table?? I am not a mind-reader!

      I don't know what you are trying to do or what you expect. I don't know what you think you are typing into? Which table are you trying to update? If you think you are updating the foreign key table, Deutscher_Name, you will be sorely disappointed. With a QSqlRelationalTable/Delegate you can only update the referencing table, you can't alter the referenced table, if you think you are doing that.

      Basically, QSqlRelationalTable, QSqlRelation and QSqlRelationalDelegate are really pretty simple/limited in Qt. All the delegate gives you is the ability to display the "mapped name" from the foreign key table where there is an id reference in the referencing table. And to pick that from a combobox of the available mapped names instead of the raw id. [Not even sure you are using a QSqlRelationalDelegate.] That's about it. I suspect whatever you are trying to do might be beyond its abilities. I don't know if the initial non-appearance of the altered string is to do with this or just some refresh bug since it appears later when you click on it. You'll have to play....

      Finally, if you have a a N:M relation, where I presume N & M can both be greater than 1, then I don't know what you think a QListView, which can only display a list of single (column) items, is going to do for you.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #9

        @JonB said in QListView with custom QStyledItemDelegate not working:

        You still have not said which column you are editing? The Deutscher_name_id, which is a foreign key?? That column in the Deutscher_Pilzname table? Or the Id/name column in Deutscher_Name table?? I am not a mind-reader!

        I'm sorry, I forgot. I am trying to edit the "Name" column in Deutscher_Name.

        @JonB said in QListView with custom QStyledItemDelegate not working:

        I don't know what you are trying to do or what you expect.

        I don't want to do anything more than the following: Each mushroom has a unique ID and with this ID I want to be able to find the associated German names, display them in a QListView and edit them. And later I will save it via a QPushButton back to database.

        @JonB said in QListView with custom QStyledItemDelegate not working:

        If you think you are updating the foreign key table, Deutscher_Name, you will be sorely disappointed. With a QSqlRelationalTable/Delegate you can only update the referencing table, you can't alter the referenced table, if you think you are doing that.

        Too bad, that's actually what I expected to be able to do with this QSqlRelationTableModel. Do you have a tip how I can implement the whole thing then? What I want to achieve I wrote one sentence above.

        JonBJ 1 Reply Last reply
        0
        • ? A Former User

          @JonB said in QListView with custom QStyledItemDelegate not working:

          You still have not said which column you are editing? The Deutscher_name_id, which is a foreign key?? That column in the Deutscher_Pilzname table? Or the Id/name column in Deutscher_Name table?? I am not a mind-reader!

          I'm sorry, I forgot. I am trying to edit the "Name" column in Deutscher_Name.

          @JonB said in QListView with custom QStyledItemDelegate not working:

          I don't know what you are trying to do or what you expect.

          I don't want to do anything more than the following: Each mushroom has a unique ID and with this ID I want to be able to find the associated German names, display them in a QListView and edit them. And later I will save it via a QPushButton back to database.

          @JonB said in QListView with custom QStyledItemDelegate not working:

          If you think you are updating the foreign key table, Deutscher_Name, you will be sorely disappointed. With a QSqlRelationalTable/Delegate you can only update the referencing table, you can't alter the referenced table, if you think you are doing that.

          Too bad, that's actually what I expected to be able to do with this QSqlRelationTableModel. Do you have a tip how I can implement the whole thing then? What I want to achieve I wrote one sentence above.

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

          @Gabber
          You and your German mushrooms! ;-) I have seen you asking about this program for a while now, and have been thoroughly lost in your table cross references!

          I know you think your explanations are clear, because you're familiar with it, but I'm afraid I'm not, and the more I have seen you try to explain previously the more confused I get! So let me take a step back, and tell you what I know you can do.

          • You have a referencing table. I think that is Deutscher_Pilzname.

          • You have a foreign table. I think that is Pilze. Or maybe it is Deutscher_Name. I think either of those are treated in the same way, so probably does not matter which. I suppose you have two QSqlRelations, one for each of these (though you don't show this).

          • Only the referencing table is a QSqlRelationalTableModel. The other(s) are just QSqlTableModel.

          • The only thing you can do in the referencing table is pick which id(s) you want it to have stored in a row from the foreign key table(s).

          • You can edit the referencing table's own columns. Type whatever into its own, non-foreign-key fields. Select (from a combobox or similar) which item in a foreign key table it points to, but not "type it in".

          • If you are trying to do something like edit the referencing table (QSqlRelationalTableModel) and from that make a change to a foreign key table, that is not the way to do it.

          • If you are trying to make a change in a foreign key table you can/should only do that when editing that table itself. Which is not a QSqlRelationalTableModel, it's some other plain QSqlTableModel.

          So all in all I don't know what your editing delegate named GermanNameRelationalDelegate is about. It sounds like a QSqlRelationalDelegate, but it is not. If you are editing a foreign key table you don't want to go through/have any mention of a QSqlRelationalTableModel.

          Like I said, I think you're going to have to try to understand what I am saying and apply it to your case, because I think you're only going to dumbfound me if you try to explain again! Does what I have said help/relate at all?

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #11

            @JonB
            I would also try to explain it to you a hundred times. I am happy when I get stuck and someone voluntarily tries to help others.

            In summary, I understand it like this:
            If I have a QSqlRelationalTableModel, I can edit the custom table, but I can only view and select the foreign-key table. Editing does not work here. For this I need a QSqlTableModel.

            Maybe you can explain me one more thing (you do this really good :) ).

            Mushroompng.png

            //Mushroom - Table
            +----+--------------+
            | ID | MushroomName |
            +----+--------------+
            |  1 | MushroomA    |
            |  2 | MushroomB    |
            |  3 | MushroomC    |
            |  4 | MushroomD    |
            +----+--------------+
            
            //GermanNames - Table
            +----+----------+
            | ID |   Name   |
            +----+----------+
            |  1 | GerNameA |
            |  2 | GerNameB |
            |  3 | GerNameC |
            |  4 | GerNameD |
            +----+----------+
            
            //GermanMushroomNames - Table
            
            +------------+---------------+
            | MushroomID | GermanNamesID |
            +------------+---------------+
            |          1 |             2 |
            |          1 |             3 |
            |          2 |             1 |
            |          3 |             1 |
            +------------+---------------+
            

            First of all: With the Mushroom - Table ID I can always find the corresponding data (this is how my database is built)

            Assume the following example:
            I want to find all German names that belong to Mushroom - ID 1, then I get back from the database the names "GerNameB" and "GerNameC".

            Now if I want to edit my 3 tables (Mushroom, GermanNames and GermanMushroomNames) I need a QSqlTableModel. Let's assume the 3 QSqlTableModels are named exactly the same as the SQL tables names.

            And exactly now my problem starts, that I don't know how to filter the data so that I can edit the QSqlModel GermanNames later.

            To just show the data you can use the QSqlRelationModel and work with the function setFilter. But how does that work if the data should be edited as well?

            Do you have a tip or even better an example? (I hope I could explain what I want ;D

            JonBJ 1 Reply Last reply
            0
            • ? A Former User

              @JonB
              I would also try to explain it to you a hundred times. I am happy when I get stuck and someone voluntarily tries to help others.

              In summary, I understand it like this:
              If I have a QSqlRelationalTableModel, I can edit the custom table, but I can only view and select the foreign-key table. Editing does not work here. For this I need a QSqlTableModel.

              Maybe you can explain me one more thing (you do this really good :) ).

              Mushroompng.png

              //Mushroom - Table
              +----+--------------+
              | ID | MushroomName |
              +----+--------------+
              |  1 | MushroomA    |
              |  2 | MushroomB    |
              |  3 | MushroomC    |
              |  4 | MushroomD    |
              +----+--------------+
              
              //GermanNames - Table
              +----+----------+
              | ID |   Name   |
              +----+----------+
              |  1 | GerNameA |
              |  2 | GerNameB |
              |  3 | GerNameC |
              |  4 | GerNameD |
              +----+----------+
              
              //GermanMushroomNames - Table
              
              +------------+---------------+
              | MushroomID | GermanNamesID |
              +------------+---------------+
              |          1 |             2 |
              |          1 |             3 |
              |          2 |             1 |
              |          3 |             1 |
              +------------+---------------+
              

              First of all: With the Mushroom - Table ID I can always find the corresponding data (this is how my database is built)

              Assume the following example:
              I want to find all German names that belong to Mushroom - ID 1, then I get back from the database the names "GerNameB" and "GerNameC".

              Now if I want to edit my 3 tables (Mushroom, GermanNames and GermanMushroomNames) I need a QSqlTableModel. Let's assume the 3 QSqlTableModels are named exactly the same as the SQL tables names.

              And exactly now my problem starts, that I don't know how to filter the data so that I can edit the QSqlModel GermanNames later.

              To just show the data you can use the QSqlRelationModel and work with the function setFilter. But how does that work if the data should be edited as well?

              Do you have a tip or even better an example? (I hope I could explain what I want ;D

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

              @Gabber said in QListView with custom QStyledItemDelegate not working:

              Now if I want to edit my 3 tables (Mushroom, GermanNames and GermanMushroomNames) I need a QSqlTableModel.

              Indeed, one for each, and that is where the editing needs to be done.

              But how does that work if the data should be edited as well?

              You never explain what you (think you) want to do for "editing"?! You can INSERT, DELETE and UPDATE on, say, the GermanNames table/QSqlTableModel. What other than that do you want to do?

              And exactly now my problem starts, that I don't know how to filter the data so that I can edit the QSqlModel GermanNames later.

              I just don't know what you mean by this.

              To just show the data you can use the QSqlRelationModel and work with the function setFilter. But how does that work if the data should be edited as well?

              You can edit the referencing table, which is the QSqlTableModel/QSqlRelationalTableModel. You cannot edit the foreign key table via this table.

              I just don't know what it is you are trying to do that you cannot figure? What is the simplest example of an "edit" which you would like to do that you cannot? What is the SQL query you think should be executed for it?

              I have one thought/guess/possibility that just might be of interest to you. Once you have set up a QSqlRelation, and you have filled the QSqlRelationalModel and the foreign key QSqlTableModel. If you look at the foreign key column in the referencing table in a row, data(QModelIndex(row, fkc), Qt::EditRole) returns the value actually stored in the table (i.e. an ID) while data(QModelIndex(row, fkc), Qt::DisplayRole) returns the mapped value of the ID in the foreign table (e.g. a name). Are you aware of this, does it help with your desired "edits"?

              Also, did you see this statement in https://doc.qt.io/qt-6/qsqlrelationaltablemodel.html#details

              The reference table name is aliased. The alias is the word "relTblAl" and the relationed column index joined by an underscore (e.g. relTblAl_2). The alias can be used to filter the table (For example, setFilter("relTblAl_2='Oslo' OR relTblAl_3='USA'")).

              ? 1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #13

                @JonB said in QListView with custom QStyledItemDelegate not working:

                You never explain what you (think you) want to do for "editing"?! You can INSERT, DELETE and UPDATE on, say, the GermanNames table/QSqlTableModel. What other than that do you want to do?

                Yes, you are right. For me, editing was always clear, but it can be insert, update or delete. I would insert with insertRows that already works with another QSqlTableModel, delete I have not yet thought about only seen that there is also a function removeRows. In this case it is about updating an existing value e.g. renaming "GerNameA" to "GerNameAAA".

                @JonB said in QListView with custom QStyledItemDelegate not working:

                I have one thought/guess/possibility that just might be of interest to you. Once you have set up a QSqlRelation, and you have filled the QSqlRelationalModel and the foreign key QSqlTableModel. If you look at the foreign key column in the referencing table in a row, data(QModelIndex(row, fkc), Qt::EditRole) returns the value actually stored in the table (i.e. an ID) while data(QModelIndex(row, fkc), Qt::DisplayRole) returns the mapped value of the ID in the foreign table (e.g. a name). Are you aware of this, does it help with your desired "edits"?

                Yes I think that is the info where I had missed.

                @JonB said in QListView with custom QStyledItemDelegate not working:

                You can edit the referencing table, which is the QSqlTableModel/QSqlRelationalTableModel. You cannot edit the foreign key table via this table.

                I understood that. But how this works in combination with a QListView I don't know yet. When I say:

                QSqlRelationTableModel *myModel = new QSqlRelationTableModel(this);
                myModel->setRelation(0, QSqlRelation("Deutscher_Name", "Id", "Name"));
                listView->setModel(myModel)
                listView->setModelColumn(0) 
                

                And model column 0 is the foreign-key table. How can I edit (update) these values then? I know I can't edit via this table. Can you give me an example pls? Do I need a separate delegate in which I perform a qobjectcast until I have QSqlTableModel to update the values there, for example?

                    const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel*>(index.model());
                    QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                

                Or how does that work then?

                JonBJ 1 Reply Last reply
                0
                • ? A Former User

                  @JonB said in QListView with custom QStyledItemDelegate not working:

                  You never explain what you (think you) want to do for "editing"?! You can INSERT, DELETE and UPDATE on, say, the GermanNames table/QSqlTableModel. What other than that do you want to do?

                  Yes, you are right. For me, editing was always clear, but it can be insert, update or delete. I would insert with insertRows that already works with another QSqlTableModel, delete I have not yet thought about only seen that there is also a function removeRows. In this case it is about updating an existing value e.g. renaming "GerNameA" to "GerNameAAA".

                  @JonB said in QListView with custom QStyledItemDelegate not working:

                  I have one thought/guess/possibility that just might be of interest to you. Once you have set up a QSqlRelation, and you have filled the QSqlRelationalModel and the foreign key QSqlTableModel. If you look at the foreign key column in the referencing table in a row, data(QModelIndex(row, fkc), Qt::EditRole) returns the value actually stored in the table (i.e. an ID) while data(QModelIndex(row, fkc), Qt::DisplayRole) returns the mapped value of the ID in the foreign table (e.g. a name). Are you aware of this, does it help with your desired "edits"?

                  Yes I think that is the info where I had missed.

                  @JonB said in QListView with custom QStyledItemDelegate not working:

                  You can edit the referencing table, which is the QSqlTableModel/QSqlRelationalTableModel. You cannot edit the foreign key table via this table.

                  I understood that. But how this works in combination with a QListView I don't know yet. When I say:

                  QSqlRelationTableModel *myModel = new QSqlRelationTableModel(this);
                  myModel->setRelation(0, QSqlRelation("Deutscher_Name", "Id", "Name"));
                  listView->setModel(myModel)
                  listView->setModelColumn(0) 
                  

                  And model column 0 is the foreign-key table. How can I edit (update) these values then? I know I can't edit via this table. Can you give me an example pls? Do I need a separate delegate in which I perform a qobjectcast until I have QSqlTableModel to update the values there, for example?

                      const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel*>(index.model());
                      QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                  

                  Or how does that work then?

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

                  @Gabber said in QListView with custom QStyledItemDelegate not working:

                  Do I need a separate delegate in which I perform a qobjectcast until I have QSqlTableModel to update the values there, for example?

                  No, you won't get anywhere trying that. Just look at everything that is in QSqlRelation Class and think about the only constructor QSqlRelation::QSqlRelation(const QString &tableName, const QString &indexColumn, const QString &displayColumn. Do you see any mention of the foreign key table as a QSqlTableModel? No, you do not. QSqlRelationaltableModel does not hold a reference to or require you to have a QSqlTableModel for it. It takes only arguments for the foreign table name things:

                  Constructs a QSqlRelation object, where tableName is the SQL table name to which a foreign key refers, indexColumn is the foreign key, and displayColumn is the field that should be presented to the user.

                  And uses that in constructing the SQL queries. So if you have a QSqlTableModel for the foreign table, which you'd like to update, you have to provide whatever code you like which tells you from foreign table name which QSqlTableModel you have created for this. QSqlRelationalTableModel knows nothing about this. You keep assuming it is cleverer than it is.

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Gabber said in QListView with custom QStyledItemDelegate not working:

                    Now if I want to edit my 3 tables (Mushroom, GermanNames and GermanMushroomNames) I need a QSqlTableModel.

                    Indeed, one for each, and that is where the editing needs to be done.

                    But how does that work if the data should be edited as well?

                    You never explain what you (think you) want to do for "editing"?! You can INSERT, DELETE and UPDATE on, say, the GermanNames table/QSqlTableModel. What other than that do you want to do?

                    And exactly now my problem starts, that I don't know how to filter the data so that I can edit the QSqlModel GermanNames later.

                    I just don't know what you mean by this.

                    To just show the data you can use the QSqlRelationModel and work with the function setFilter. But how does that work if the data should be edited as well?

                    You can edit the referencing table, which is the QSqlTableModel/QSqlRelationalTableModel. You cannot edit the foreign key table via this table.

                    I just don't know what it is you are trying to do that you cannot figure? What is the simplest example of an "edit" which you would like to do that you cannot? What is the SQL query you think should be executed for it?

                    I have one thought/guess/possibility that just might be of interest to you. Once you have set up a QSqlRelation, and you have filled the QSqlRelationalModel and the foreign key QSqlTableModel. If you look at the foreign key column in the referencing table in a row, data(QModelIndex(row, fkc), Qt::EditRole) returns the value actually stored in the table (i.e. an ID) while data(QModelIndex(row, fkc), Qt::DisplayRole) returns the mapped value of the ID in the foreign table (e.g. a name). Are you aware of this, does it help with your desired "edits"?

                    Also, did you see this statement in https://doc.qt.io/qt-6/qsqlrelationaltablemodel.html#details

                    The reference table name is aliased. The alias is the word "relTblAl" and the relationed column index joined by an underscore (e.g. relTblAl_2). The alias can be used to filter the table (For example, setFilter("relTblAl_2='Oslo' OR relTblAl_3='USA'")).

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #15

                    @JonB said in QListView with custom QStyledItemDelegate not working:

                    If you look at the foreign key column in the referencing table in a row, data(QModelIndex(row, fkc), Qt::EditRole) returns the value actually stored in the table (i.e. an ID) while data(QModelIndex(row, fkc), Qt::DisplayRole) returns the mapped value of the ID in the foreign table (e.g. a name).

                    Sorry that I ask again here, but somehow I do not manage to get what you wrote. I post times briefly code.

                    QSqlRelationalTableModel *DatabaseManager::germanFungusNameDataModel() const
                    {
                        QSqlRelationalTableModel *model = new QSqlRelationalTableModel;
                        model->setTable("Deutscher_Pilzname");
                        model->setRelation(model->fieldIndex("Deutscher_name_id"), QSqlRelation("Deutscher_Name", "Id", "Name"));
                        model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
                        model->setSort(0, Qt::AscendingOrder);
                        model->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
                        model->select();
                        while (model->canFetchMore()) {
                            model->fetchMore();
                            model->relationModel(0)->fetchMore();
                        }
                        return model;
                    }
                    

                    My foreign column is zero. Now I can do that:

                    //mGermanFungusNameDataModel -> the above QSqlRelationTableModel
                    mGermanFungusNameDataModel->index(row, 0).data(Qt::DisplayRole).toString(); // returns the Name
                    mGermanFungusNameDataModel->index(row, 0).data(Qt::EditRole); //Returns a QVariant but also with the name instead of the ID.
                    

                    What am I doing wrong here? Can you please go into this in more detail?

                    JonBJ 1 Reply Last reply
                    0
                    • ? A Former User

                      @JonB said in QListView with custom QStyledItemDelegate not working:

                      If you look at the foreign key column in the referencing table in a row, data(QModelIndex(row, fkc), Qt::EditRole) returns the value actually stored in the table (i.e. an ID) while data(QModelIndex(row, fkc), Qt::DisplayRole) returns the mapped value of the ID in the foreign table (e.g. a name).

                      Sorry that I ask again here, but somehow I do not manage to get what you wrote. I post times briefly code.

                      QSqlRelationalTableModel *DatabaseManager::germanFungusNameDataModel() const
                      {
                          QSqlRelationalTableModel *model = new QSqlRelationalTableModel;
                          model->setTable("Deutscher_Pilzname");
                          model->setRelation(model->fieldIndex("Deutscher_name_id"), QSqlRelation("Deutscher_Name", "Id", "Name"));
                          model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
                          model->setSort(0, Qt::AscendingOrder);
                          model->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
                          model->select();
                          while (model->canFetchMore()) {
                              model->fetchMore();
                              model->relationModel(0)->fetchMore();
                          }
                          return model;
                      }
                      

                      My foreign column is zero. Now I can do that:

                      //mGermanFungusNameDataModel -> the above QSqlRelationTableModel
                      mGermanFungusNameDataModel->index(row, 0).data(Qt::DisplayRole).toString(); // returns the Name
                      mGermanFungusNameDataModel->index(row, 0).data(Qt::EditRole); //Returns a QVariant but also with the name instead of the ID.
                      

                      What am I doing wrong here? Can you please go into this in more detail?

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

                      @Gabber said in QListView with custom QStyledItemDelegate not working:

                      mGermanFungusNameDataModel->index(row, 0).data(Qt::EditRole); //Returns a QVariant but also with the name instead of the ID.

                      Hmm, I (admit I) made an assumption about what EditRole would hold.

                      You know more than I now. I did say look at the documentation. Please do so. Because I just did for you, and actually I discover:
                      QSqlTableModel *QSqlRelationalTableModel::relationModel(int column) const

                      Returns a QSqlTableModel object for accessing the table for which column is a foreign key, or nullptr if there is no relation for the given column.

                      The returned object is owned by the QSqlRelationalTableModel.

                      However, I am confused, because searching woboq I do not find any implementation for this method anywhere, only its declaration (as virtual and with no body) in the .h file. In 2009(!) one guy claimed in https://www.qtcentre.org/archive/index.php?t-22222.html

                      I finally got it to work. I used the table model returned by relationModel to insert into the lookup table. I also had to call submitAll on the QSqlRelationalTableModel itself (after calling it on the relation tableModel) to get that table to notice that lookup table had changed. I don't know if calling submitAll on the relation tableModel is actually rquired, but it had to be talled on the QSqlRelationalTableModel.

                      Meanwhile I think you should read through https://forum.qt.io/topic/68212/qsqlrelationaltablemodel-get-relation-foreign-key-value

                      I might have a look at the EditRole/DisplayRole sometime/tomorrow, but really I don't know much you can't discover for yourself.

                      Finally, it still is not clear to me just what you are doing with that list, what you are doing binding it to a QListView, why you don't use a QSqlRelationalDelegate instead, what you want to happen when you "edit" in the list. All I see is you overtyping one German name with another (brand now one?), maybe you want to update that in your Deutscher_Name table, I don't know.

                      ? 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Gabber said in QListView with custom QStyledItemDelegate not working:

                        mGermanFungusNameDataModel->index(row, 0).data(Qt::EditRole); //Returns a QVariant but also with the name instead of the ID.

                        Hmm, I (admit I) made an assumption about what EditRole would hold.

                        You know more than I now. I did say look at the documentation. Please do so. Because I just did for you, and actually I discover:
                        QSqlTableModel *QSqlRelationalTableModel::relationModel(int column) const

                        Returns a QSqlTableModel object for accessing the table for which column is a foreign key, or nullptr if there is no relation for the given column.

                        The returned object is owned by the QSqlRelationalTableModel.

                        However, I am confused, because searching woboq I do not find any implementation for this method anywhere, only its declaration (as virtual and with no body) in the .h file. In 2009(!) one guy claimed in https://www.qtcentre.org/archive/index.php?t-22222.html

                        I finally got it to work. I used the table model returned by relationModel to insert into the lookup table. I also had to call submitAll on the QSqlRelationalTableModel itself (after calling it on the relation tableModel) to get that table to notice that lookup table had changed. I don't know if calling submitAll on the relation tableModel is actually rquired, but it had to be talled on the QSqlRelationalTableModel.

                        Meanwhile I think you should read through https://forum.qt.io/topic/68212/qsqlrelationaltablemodel-get-relation-foreign-key-value

                        I might have a look at the EditRole/DisplayRole sometime/tomorrow, but really I don't know much you can't discover for yourself.

                        Finally, it still is not clear to me just what you are doing with that list, what you are doing binding it to a QListView, why you don't use a QSqlRelationalDelegate instead, what you want to happen when you "edit" in the list. All I see is you overtyping one German name with another (brand now one?), maybe you want to update that in your Deutscher_Name table, I don't know.

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #17

                        @JonB
                        I have good news for you, I finally got it right. So what have I done now?

                        @Gabber said in QListView with custom QStyledItemDelegate not working:

                        //Mushroom - Table
                        +----+--------------+
                        | ID | MushroomName |
                        +----+--------------+
                        | 1 | MushroomA |
                        | 2 | MushroomB |
                        | 3 | MushroomC |
                        | 4 | MushroomD |
                        +----+--------------+

                        //GermanNames - Table
                        +----+----------+
                        | ID | Name |
                        +----+----------+
                        | 1 | GerNameA |
                        | 2 | GerNameB |
                        | 3 | GerNameC |
                        | 4 | GerNameD |
                        +----+----------+

                        //GermanMushroomNames - Table

                        +------------+---------------+
                        | MushroomID | GermanNamesID |
                        +------------+---------------+
                        | 1 | 2 |
                        | 1 | 3 |
                        | 2 | 1 |
                        | 3 | 1 |
                        +------------+---------------+

                        I created a QSqlTableModel from each of these 3 tables. Then using the Mushroom ID in the GermanMushroomNames with the setFilter function return the corresponding GermanName ID. With the help of this ID I have then filtered in GermanNames with setFilter for the corresponding name. In the QListView I then set the GermanName model.

                        Now I can see and edit the correct data.

                        I would like to thank you again sincerely for the support. Without you and your tip, that QSqlRelationTable the Foreign-Key Column are not editable, I think I would never have come to the solution.

                        Top, many thanks!

                        JonBJ 1 Reply Last reply
                        0
                        • ? A Former User

                          @JonB
                          I have good news for you, I finally got it right. So what have I done now?

                          @Gabber said in QListView with custom QStyledItemDelegate not working:

                          //Mushroom - Table
                          +----+--------------+
                          | ID | MushroomName |
                          +----+--------------+
                          | 1 | MushroomA |
                          | 2 | MushroomB |
                          | 3 | MushroomC |
                          | 4 | MushroomD |
                          +----+--------------+

                          //GermanNames - Table
                          +----+----------+
                          | ID | Name |
                          +----+----------+
                          | 1 | GerNameA |
                          | 2 | GerNameB |
                          | 3 | GerNameC |
                          | 4 | GerNameD |
                          +----+----------+

                          //GermanMushroomNames - Table

                          +------------+---------------+
                          | MushroomID | GermanNamesID |
                          +------------+---------------+
                          | 1 | 2 |
                          | 1 | 3 |
                          | 2 | 1 |
                          | 3 | 1 |
                          +------------+---------------+

                          I created a QSqlTableModel from each of these 3 tables. Then using the Mushroom ID in the GermanMushroomNames with the setFilter function return the corresponding GermanName ID. With the help of this ID I have then filtered in GermanNames with setFilter for the corresponding name. In the QListView I then set the GermanName model.

                          Now I can see and edit the correct data.

                          I would like to thank you again sincerely for the support. Without you and your tip, that QSqlRelationTable the Foreign-Key Column are not editable, I think I would never have come to the solution.

                          Top, many thanks!

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

                          @Gabber said in QListView with custom QStyledItemDelegate not working:

                          I created a QSqlTableModel from each of these 3 tables. Then using the Mushroom ID in the GermanMushroomNames with the setFilter function return the corresponding GermanName ID. With the help of this ID I have then filtered in GermanNames with setFilter for the corresponding name. In the QListView I then set the GermanName model.

                          That's just the kind of thing I was trying to say to look for, well done :)

                          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