QSqlRelationalTable join with own derived QItemDelegate class

  • Hi, how to join two differents "delegate" class ?
    For example, i have my own "MyDelegate" class inherited from QItemDelegate.
    And i have a view with setItemDelegate(new QSqlRelationalDelegate(this).
    BUt i would like the same view to setItemDelegate(myForm). Where MyDelegate *myform; myForm = new MyDelegate(); view->setItemDelegate(myForm); but also: view->setItemDelegate(new QSqlRelationalDelegate(this);

    which way could join QSqlRelationalDelegate and also myForm together in the same view ?

  • Lifetime Qt Champion


    One way would be to inherit QSqlRelationalDelegate and call the base implementation of createEditor and setModelData only if the underlaying model is QSqlRelationalTableModel.

  • Hi SGaist, glad to read you, and thank you for your help.

    I try this way to, but at compil time, i have some problems about confused inherited between QItemDelegate and QSqlRelationalDelegate (because i inherited together in same own MyDelegate class).
    So when i do like you said (only QSqlRelationalDelegate... right ?)...
    I see that i have to implement not only "setModelData" and "createEditor", but also "paint" (for be able to first print could form the text), and "setEditorData" and "updateEditorGeometry".
    But at this time, if i do that, i loose this same use of virtual functions by QSqlRelationalDelegate when override these functions.
    At this time, i arrived by the heavy way who consist to:
    1/ create a new function inside the inherited MyDelegate class (from QItemDelegate) defined like this:
    void addRelation(const int &viewColumn, const QString &foreignTable, const QString &idForeignName, const QString &printColumn);
    also define in my provate part of the header file: QHash<int, QStringList> relations;
    where int is the column to be related, and QStringList are the relations of these column (foreign table, name of id column, and name of column where to find data to show instead of the id number).
    2/ add a variable QSqlDatabase inside constructor to share database acces:
    MyDelegate::MyDelegate(QSqlDatabase database, QObject *parent) : QStyledItemDelegate(parent), db(database) { }
    where db is defined as: QSqlDatabase db; in private part of header.
    3/ add code inside each function i told you by:
    if(!relations.value(index.column()).isEmpty()) { ... }
    4/ inside these part of codes, use of QSqlQuery and apply SQL query for do what QSqlRelationalTable and QSqlRelationnalDelegate do.
    5/ use of QSqlTableModel instead of QSqlRelationalTableModel.

    but it is not a subtil way i think (that is why i not write [resolved] on title.
    i would prefer to be able to really join allready existed mechanism of QSqlRelationalTableModel/Delegate and an other one QItemDelegate. I feel it could technicaly be possible, but maybe there is not.
    If you find a way who works, could you please explain more, maybe test it and maybe share code example please ?
    I think resolve these question could help many more QT users like us.
    If i find a way, i will share an example code also.
    thank you again for try to help (and happy new year also).

  • someone on irc (alias peppe) give me the bes way to do it easily:
    just use the function "setItemDelegateForColumn()".

    This is from far the more elegant soution to use the perfect mechanism allready exist with QT.

    thanks for reading, thanks for try to help.

  • Lifetime Qt Champion

    I misunderstood the problem. Indeed, that's the best solution :)

    Happy coding !

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.