Nominate our 2022 Qt Champions!

Reimplementation of QAbstractItemModel::setData

  • Hi,

    I have a QTableView that display the content of a QSqlQueryModel. But now i need to merge some rows.
    I decided to merge the content of the rows but i can't modify the QSqlQueryModel.

    I use a custom QSortFilterProxyModel.
    But I don't know how to reimplement the setData function. I also need to reimplement the data function to get the content to merge.


  • @Zoptune said in Reimplementation of QAbstractItemModel::setData:

    merge some rows

    error C2065: 'merge some rows' : undeclared identifier

    Please define what you mean

  • Hi @VRonin ,

    I have rows in my TableView that represent real objects. Special objects can be created in pair. This is why i want to merge rows.
    These special object have the same name but not the same length.

    Example :
    At the begining i have two row with name XXXXX and length 5m and 10m.
    At the end, i want to have one row with name XXXXX and length 5m + 10m.

    I hope I was clear in my explanations.


  • Would you like the data in your original model to be kept separate or merged?

    Are name and length 2 different columns?

  • @VRonin

    I don't care if the data is merged or not in the original model and yes name and length are separated columns.

  • Last question, I promise: does the model need to editable?

  • @VRonin

    If you ask me if the user can edit the model, the answer is no. I just want to display the data but i need to merge some of them before.

  • implemented here:

    example usage:

    #include <QApplication>
    #include <QStandardItemModel>
    #include <QTableView>
    #include <QHBoxLayout>
    #include "mergeproxy.h"
    int main(int argc, char **argv)
        QApplication app(argc,argv);
        QStandardItemModel baseModel;
        baseModel.insertRows(0, 5);
        baseModel.insertColumns(0, 2);
        baseModel.setHeaderData(0, Qt::Horizontal, "Col1");
        baseModel.setHeaderData(1, Qt::Horizontal, "Col2");
        baseModel.setData(baseModel.index(0, 0), "Test");
        baseModel.setData(baseModel.index(0, 1), 1);
        baseModel.setData(baseModel.index(1, 0), "Test");
        baseModel.setData(baseModel.index(1, 1), 2);
        baseModel.setData(baseModel.index(2, 0), "Test2");
        baseModel.setData(baseModel.index(2, 1), 3);
        baseModel.setData(baseModel.index(3, 0), "Test2");
        baseModel.setData(baseModel.index(3, 1), 4);
        baseModel.setData(baseModel.index(4, 0), "Alone");
        baseModel.setData(baseModel.index(4, 1), 100);
        MergeProxy baseProxy;
        QWidget mainWidget;
        QTableView* savedView=new QTableView(&mainWidget);
        QTableView* loadedView=new QTableView(&mainWidget);
        QHBoxLayout* mainLay = new QHBoxLayout(&mainWidget);
        return app.exec();

  • @VRonin

    Wow there's a lot of things i don't understand or that i have never used ^^

    Thank you very much it seems to be what i want !
    I need to work and understand now :)

  • @VRonin

    Hi, it's me again.

    I not sure i understood, in what function do you change the data ?

    From what I understood, this is the MergeProxy::data function that edit the data.

    Because now i'm working with a bigger table and more complex treatment.

    I need to merge rows using two mergeKeyColumn.

    Example :

    alt text

    Here the two last columns are the two mergeKeyColumn (first one is "pair_id" and second one is "id")
    And "AKSA" from row 2 col 3 need to go at col 4.

    Result :
    alt text


  • Please i need help ^^

  • What you need is 100% data dependant, there is no way of implementing it in a general way.
    You are better off just executing the query manually and fill a QStandardItemModel manually meging the cells when necessary instead of using QSqlQueryModel

  • Ok, i thought was an another way to merge rows.

    Thank you

Log in to reply