Results 1 to 8 of 8

Thread: setModelData for subclassed QItemDelegate

  1. #1
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default setModelData for subclassed QItemDelegate

    In setModelData for my delegate I perform this code

    Qt Code:
    1. void ShipComponentDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    2. {
    3. QString name = index.model()->data(index, Qt::DisplayRole).toString();
    4. QSize size(0,0);
    5. QVariant var = QVariant(size);
    6. model->setData(index, var,Qt::SizeHintRole);
    7. size = model->data(index, Qt::SizeHintRole).toSize();
    8. }
    To copy to clipboard, switch view to plain text mode 
    size taken from model after setting should be the same as above. But it isn't. I thought first time that I made some error with values inserted into size and it is invalid. But no - in every other case with all other values - it doesn't want to change internal data.
    To clarify all - in setEditorData() I fill this index with my size - then after changes I want to bring this size to previous value.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: setModelData for subclassed QItemDelegate

    I suggest you set an empty QVariant as the value.

  3. #3
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setModelData for subclassed QItemDelegate

    Tried - doesn't work.
    It's like model doesn't want to fill with new value. But it has to change, because this value is needed somewhere else in delegate.
    I've checked internal setData code - it isn't setting only when index is invalid or data is invalid. But index.isValid() returns TRUE and QVariant which is being set - also returns true for validity test.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setModelData for subclassed QItemDelegate

    What model are you using?

  5. #5
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setModelData for subclassed QItemDelegate

    QStandardItemModel. In setModelData it's downcasted to QAbstractItemModel. Only one think interest me - whole method is const. But - if I remeber correctly - const correctness is being tested only during compilation time.
    SetData is virtual function - so even when I get QAbstractItemModel pointer to QStandardItemModel - then setData should use method from QStandardItemModel.

    During setData processed by QStandardItemModel it calls setData for QStandardItem which uses QVector<QWidgetItemData> values - there should be no time delays.
    The only think that bothers me is signal sent after data set - maybe there's a problem.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setModelData for subclassed QItemDelegate

    No, I thought you were using a custom model and had setData broken.

    What about this? ( from QStandardItem::setData )
    Qt Code:
    1. QVariant oldValue = data(role);
    2. if (value == oldValue)
    3. return;
    To copy to clipboard, switch view to plain text mode 

    As you can see no value is set if it is equal to the old one.

    What values are in the model?
    You are always setting QSize(0,0) and get it back.


    (I'm using Qt 4.2.2 ).

    Regards

  7. #7
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setModelData for subclassed QItemDelegate

    Good point - this condition return no error value.
    But - when I setData to QVariant(QSize(10,10)), then to QVariant(QSize(20,20)) and then to QVariant(QSize(30,30)) - one line after another - it still returns only first data, set at the begin.

  8. #8
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setModelData for subclassed QItemDelegate

    Situation has been clarified. Each time when setData for model used in view is being used - by inside signal setEditorData for delegate is being called.
    So when I was setting SizeHintRole in setEditorData to editor size - then when in setModelData I was clearing this role - model was calling setEditorData and all was set to the previous state.
    When I moved setting SizeHintRole to createEditor - all problems were gone.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.