Question about QItemDelegate methods



  • Hi all -

    I'm doing a tutorial on using the QItemDelegate class. I've created my own class based on QItemDelegate, and overloaded four functions:

        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        void setEditorData(QWidget *editor, const QModelIndex &index);
        void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index);
        void updateEditorGeometry(QWidget *editor, QStyleOptionViewItem &option, const QModelIndex &index);
    

    From what I discern, none of these are explicitly called by my code (at least, per the tutorial). But only createEditor() actually runs. The others are never called in my example.
    Here's my use of the delegate in my widget:

        delegate = new Delegate(this);
    .
    .
    .
        ui->tableView->setItemDelegate(delegate);
    

    The tutorial goes rather fast (due to YouTube constraints) and it's hard to see everything the author is coding, but I think I've got my copy agreeing with it. I'm not really sure what's supposed to happen here, but I'd expect those other methods would be invoked when I edit an item. Can someone give me some idea of what I'm doing wrong?

    Oh: the tutorial also hails from 4.7 days; not sure if that matters.

    Thanks.


  • Lifetime Qt Champion

    Hi,

    createEditor is called when you start editing a cell by e.g. double-clicking on it.
    setEditorData will be called to load the content of the model matching the cell you started to edit.
    setModelData will be called once you close the editor by e.g. clicking on another cell.
    updateEditroGeomtry should be called when your editor is shown in order to resize it to fit the cell.

    .You might want to rather take a look a QStyledItemDelegate.

    Hope it helps.



  • Well, then...I'm definitely doing something wrong, because I have qDebug statements in all of my delegate routines as well as breakpoints; no evidence that any are called.

    I did read that we're supposed to use QStyledItemDelegate now, but I figured I'd first get this tutorial running, and then migrate over.

    Could there be something wrong in my signatures that's causing Qt to use the default routines instead of my overloaded ones?



  • This may be happening due to function signature issue. Recheck the function signature one more time and try. Just check if & is missing or * is missing or case sensitive issue of the function name etc.



  • Hi, dheerendra -

    Thanks for the reply. That was indeed the problem -- I was missing a few "const" declarations.

    Good reminder of how fussy C++ is, particularly when overriding existing functions.

    I appreciate it.



  • @mzimmers

    Start from c++11, you can avoid some subtle bugs of override by the keyword "override".

    class parent
    {
      public:
        parent() = default;
        virtual ~parent();
        virtual void i_am_override_function(QString const &input);
    }
    
    class whatever : public parent
    {
      public:
        //fine, you override the function with same signature
        void i_am_override_function(QString const &input) override;
        //oops, compile time error will occur, because they do not have same signature
        void i_am_override_function(QString &input) override;
    }
    

    Let compiler help you avoid the bugs, human hardly beat compiler for routine tasks

    By the way, I like const :)



  • Hi, Tham -

    Thanks for reminding me of override.

    Let compiler help you avoid the bugs, human hardly beat compiler for routine tasks

    This is good advice.


Log in to reply
 

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