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. QSqlRelationalTable join with own derived QItemDelegate class
Qt 6.11 is out! See what's new in the release blog

QSqlRelationalTable join with own derived QItemDelegate class

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.2k 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.
  • jerome_isAviableJ Offline
    jerome_isAviableJ Offline
    jerome_isAviable
    wrote on last edited by jerome_isAviable
    #1

    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 ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      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
      0
      • jerome_isAviableJ Offline
        jerome_isAviableJ Offline
        jerome_isAviable
        wrote on last edited by jerome_isAviable
        #3

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

        1 Reply Last reply
        0
        • jerome_isAviableJ Offline
          jerome_isAviableJ Offline
          jerome_isAviable
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Happy coding !

            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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved