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. Dynamically change delegate QTableView
Forum Updated to NodeBB v4.3 + New Features

Dynamically change delegate QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 3 Posters 8.1k Views 2 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.
  • E Offline
    E Offline
    EagleWatch
    wrote on last edited by
    #3

    Hi @SGaist,

    What's a flexible delegate ?
    I'm not sure to understand correctly.

    mrjjM 1 Reply Last reply
    0
    • E EagleWatch

      Hi @SGaist,

      What's a flexible delegate ?
      I'm not sure to understand correctly.

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

      @EagleWatch
      Its just a more feature full version of the Delegate. Or you could call it data driven.

      When its time to edit the data, the frameworks calls
      QWidget *MyDelegate::createEditor(QWidget *parent,
      const QStyleOptionViewItem &option,
      const QModelIndex &index) const

      Here you can use a flag, a user role or whatever fits to instruct to create a combobox
      and not LineEdit or what is needed.

      Same applies when writing data back, it must also handle the various Widgets that could be created.
      That way same delegate can be used in various conditions.
      There is ofcourse a fine line of having a monster Delegate or splitting it to multiple ones but in most cases
      handling a few different editors etc is super fine.

      1 Reply Last reply
      2
      • E Offline
        E Offline
        EagleWatch
        wrote on last edited by EagleWatch
        #5

        @mrjj
        Here is how my program looks like.
        0_1525943271361_editor.png

        So I can create an editor for each filter I want ?
        When can I call the editor I want ? The combobox used to filter what the editor shows is edited dynamically. I'm afraid I have to create an editor each time I select an item, but what about the memory ?

        EDIT : With the code I provided, here is what's happening
        0_1525944018845_Capture.JPG
        When the I change the filter (by choosing another item in the combobox), the editor is fine.

        Howerver, when I double click to change the value, the editor is filtered with the first filter I initialized.
        0_1525944231511_Capture.JPG

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

          Something is not completely clear with your setup.

          Do you mean the content of the combo box above the table view depends on the what you selected in one of the rows of said tableview ?

          Or that the content of the cell editor depends on what you selected in the combo box above the table view ?

          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
          1
          • E Offline
            E Offline
            EagleWatch
            wrote on last edited by
            #7

            The content of the cell editor should depends on what I select in the combobox above the table view.

            And the content in the combobox above the table view depends on another table view not important here.

            mrjjM 1 Reply Last reply
            0
            • E EagleWatch

              The content of the cell editor should depends on what I select in the combobox above the table view.

              And the content in the combobox above the table view depends on another table view not important here.

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

              @EagleWatch
              hi
              but why you need a new Delegate for that ?
              Could you not just change what ever combobox is using ?
              Like via a slot in Delegate and a signal from mainwindow.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                EagleWatch
                wrote on last edited by
                #9

                @mrjj
                Hi,

                That would be better, but I didn't find how I could do that.

                mrjjM 1 Reply Last reply
                0
                • E EagleWatch

                  @mrjj
                  Hi,

                  That would be better, but I didn't find how I could do that.

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

                  @EagleWatch
                  well easy option is just keep Delegate pointer
                  around and simply add a new function to it to update List
                  via the instance pointer.

                  Alternatively add a new slot in Delegate
                  void ChangeList(QList<xx> list)
                  (should store list for use when editor is created)

                  and a new signal in mainwindow
                  ( that looks the same)
                  and connect them and simple use emit to update the combobox.

                  E 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @EagleWatch
                    well easy option is just keep Delegate pointer
                    around and simply add a new function to it to update List
                    via the instance pointer.

                    Alternatively add a new slot in Delegate
                    void ChangeList(QList<xx> list)
                    (should store list for use when editor is created)

                    and a new signal in mainwindow
                    ( that looks the same)
                    and connect them and simple use emit to update the combobox.

                    E Offline
                    E Offline
                    EagleWatch
                    wrote on last edited by EagleWatch
                    #11

                    @mrjj
                    That was one of my ideas but I think I don't understand how the delagate works.

                    When I study the QSqlRelationnalDelegate, I know the combobox is created with the createEditor function.
                    However, I don't understand how to modify the item in the combobox when the editor already exists.

                    I think I shoud use the function setEditorData but how to get the editor and the index I have to pass to this function ?

                    mrjjM 1 Reply Last reply
                    0
                    • E EagleWatch

                      @mrjj
                      That was one of my ideas but I think I don't understand how the delagate works.

                      When I study the QSqlRelationnalDelegate, I know the combobox is created with the createEditor function.
                      However, I don't understand how to modify the item in the combobox when the editor already exists.

                      I think I shoud use the function setEditorData but how to get the editor and the index I have to pass to this function ?

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

                      Hi
                      Sorry i miss the fact that you use a standard Delegate
                      QSqlRelationalDelegate
                      and not a custom one.
                      I think you need to make a small subclass of QSqlRelationnalDelegate
                      and override createEditor so you can adjust whats to happen.

                      Since you can call the baseclass's QWidget *QSqlRelationalDelegate::createEditor
                      and get the Editor back. the (QWidget *) you can use qobject_cast to convert to
                      combobox and hence adjust its items.

                      But Maybe this is overkill. I would wait and see if others have easier ideas.

                      E 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        Hi
                        Sorry i miss the fact that you use a standard Delegate
                        QSqlRelationalDelegate
                        and not a custom one.
                        I think you need to make a small subclass of QSqlRelationnalDelegate
                        and override createEditor so you can adjust whats to happen.

                        Since you can call the baseclass's QWidget *QSqlRelationalDelegate::createEditor
                        and get the Editor back. the (QWidget *) you can use qobject_cast to convert to
                        combobox and hence adjust its items.

                        But Maybe this is overkill. I would wait and see if others have easier ideas.

                        E Offline
                        E Offline
                        EagleWatch
                        wrote on last edited by
                        #13

                        @mrjj
                        Hi,

                        I've thinking about what you said and I tried to create a subclass of QSqlRelationnalDelegate to get Editor back via the createEditor function.

                        However the createEditor function is protected, so I cannot override it.
                        I have no clue to get Editor back without create my own delegate class.

                        As others have not easier ideas, what you suggests might the best for the moment.

                        So , how can I override the createEditor function if this is protected ?

                        mrjjM 1 Reply Last reply
                        0
                        • E EagleWatch

                          @mrjj
                          Hi,

                          I've thinking about what you said and I tried to create a subclass of QSqlRelationnalDelegate to get Editor back via the createEditor function.

                          However the createEditor function is protected, so I cannot override it.
                          I have no clue to get Editor back without create my own delegate class.

                          As others have not easier ideas, what you suggests might the best for the moment.

                          So , how can I override the createEditor function if this is protected ?

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

                          @EagleWatch
                          Hi
                          Whats wrong with

                          #include <QtSql/QSqlRelationalDelegate>
                          class MySQLDelegate : QSqlRelationalDelegate {
                          protected:
                              virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
                              {
                                  QSqlRelationalDelegate::createEditor(parent,option,index);// call base
                              }
                              virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
                              {
                              }
                          };
                          
                          E 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @EagleWatch
                            Hi
                            Whats wrong with

                            #include <QtSql/QSqlRelationalDelegate>
                            class MySQLDelegate : QSqlRelationalDelegate {
                            protected:
                                virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
                                {
                                    QSqlRelationalDelegate::createEditor(parent,option,index);// call base
                                }
                                virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
                                {
                                }
                            };
                            
                            E Offline
                            E Offline
                            EagleWatch
                            wrote on last edited by
                            #15

                            @mrjj

                            I don't get it. Maybe I need to study a little more in C++.

                            That would be what I would like to get :

                            #include <QtSql/QSqlRelationalDelegate>
                            class MySQLDelegate : QSqlRelationalDelegate {
                            protected:
                                virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
                                {
                                    editor = QSqlRelationalDelegate::createEditor(parent,option,index);// call base
                                }
                                virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
                                {
                                }
                            
                            private:
                                QWidget *editor;
                            };
                            

                            Of course, the code will not work because of the "const" word.

                            mrjjM 1 Reply Last reply
                            0
                            • E EagleWatch

                              @mrjj

                              I don't get it. Maybe I need to study a little more in C++.

                              That would be what I would like to get :

                              #include <QtSql/QSqlRelationalDelegate>
                              class MySQLDelegate : QSqlRelationalDelegate {
                              protected:
                                  virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
                                  {
                                      editor = QSqlRelationalDelegate::createEditor(parent,option,index);// call base
                                  }
                                  virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
                                  {
                                  }
                              
                              private:
                                  QWidget *editor;
                              };
                              

                              Of course, the code will not work because of the "const" word.

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

                              @EagleWatch
                              ahh, the const.
                              well you dont need to store the Editor as its given in
                              setEditorData(QWidget *editor, const QModelIndex &index) const {
                              QComboBox *cb=qobject_cast<QComboBox *>(editor);
                              if (cb) {
                              use combo...
                              }

                              // handle other types of widgets or send to base setEditorData

                              }

                              E 1 Reply Last reply
                              1
                              • mrjjM mrjj

                                @EagleWatch
                                ahh, the const.
                                well you dont need to store the Editor as its given in
                                setEditorData(QWidget *editor, const QModelIndex &index) const {
                                QComboBox *cb=qobject_cast<QComboBox *>(editor);
                                if (cb) {
                                use combo...
                                }

                                // handle other types of widgets or send to base setEditorData

                                }

                                E Offline
                                E Offline
                                EagleWatch
                                wrote on last edited by
                                #17

                                @mrjj
                                Hi,
                                I get it, thank you.
                                The setEditorData(QWidget *editor, const QModelIndex &index) const {} is protected so I cannot override it.

                                delegate.h:18: error : 'virtual void Delegate::setEditorData(QWidget*, const QModelIndex&) const' is protected
                                     virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
                                                  ^
                                

                                This function is not present in the QSqlRelationnalDelegate class.
                                Maybe I have to subclass QItemDelegate instead ?

                                mrjjM 1 Reply Last reply
                                0
                                • E EagleWatch

                                  @mrjj
                                  Hi,
                                  I get it, thank you.
                                  The setEditorData(QWidget *editor, const QModelIndex &index) const {} is protected so I cannot override it.

                                  delegate.h:18: error : 'virtual void Delegate::setEditorData(QWidget*, const QModelIndex&) const' is protected
                                       virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
                                                    ^
                                  

                                  This function is not present in the QSqlRelationnalDelegate class.
                                  Maybe I have to subclass QItemDelegate instead ?

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

                                  @EagleWatch

                                  Hi, im missing something. following code compiles with no
                                  remarks. its listed under protected and im not sure what goes wrong in your case.

                                  #include <QtSql/QSqlRelationalDelegate>
                                  class MySQLDelegate : QSqlRelationalDelegate {
                                  protected:
                                      virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
                                      {
                                          QSqlRelationalDelegate::createEditor(parent,option,index);// call base
                                      }
                                      virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
                                      {
                                      }
                                  };
                                  
                                  E 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @EagleWatch

                                    Hi, im missing something. following code compiles with no
                                    remarks. its listed under protected and im not sure what goes wrong in your case.

                                    #include <QtSql/QSqlRelationalDelegate>
                                    class MySQLDelegate : QSqlRelationalDelegate {
                                    protected:
                                        virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
                                        {
                                            QSqlRelationalDelegate::createEditor(parent,option,index);// call base
                                        }
                                        virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
                                        {
                                        }
                                    };
                                    
                                    E Offline
                                    E Offline
                                    EagleWatch
                                    wrote on last edited by EagleWatch
                                    #19

                                    @mrjj
                                    I get the error only when I'm using the MySqlDelegate.

                                    So, in my projectWindow.h, if I have :

                                    #include "mysqldelegate.h"
                                    private:
                                        MySqlDelegate *m_delegate;
                                    

                                    I get the issue in the MySqlDelegate class.

                                    Do you get the issue if you call your class ?

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

                                      Hi,

                                      Can you show how you are using it ?

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      E 1 Reply Last reply
                                      1
                                      • SGaistS SGaist

                                        Hi,

                                        Can you show how you are using it ?

                                        E Offline
                                        E Offline
                                        EagleWatch
                                        wrote on last edited by
                                        #21

                                        @SGaist
                                        Hi,

                                        Thank you for asking.
                                        When searching to answer you, I realized that I forgot to remove some tests with the m_delegate pointer.
                                        That's why I had the error.

                                        Now I have all necessary informations, I'll try to customize this subclass to fit with what I want.

                                        I'll keep you updated guys.

                                        Thanks.

                                        1 Reply Last reply
                                        0
                                        • E Offline
                                          E Offline
                                          EagleWatch
                                          wrote on last edited by
                                          #22

                                          Hi guys,

                                          I've some news.

                                          So after some days studying classes I was using, I discoverd that my problem was not because of the QSqlRelationalDelegate class.
                                          In fact, that was the QSqlRelationalTableModel.

                                          When I create the QSqlRelationalTableModel, I had to set a relation.
                                          When doing this, the QSqlRelationalTableModel keeps in cache the model given by the relation.

                                          Even if we change the relation, the model is still the same, that why the combobox was always showing the same data.

                                          For now, I just delete the QSqlRelationalTableModel and recreate it with the new relation and it works like a charm.

                                          1 Reply Last reply
                                          1

                                          • Login

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