Unsolved regarding role in setData method of QAbstractItemModel
-
I have a function in which I show a image in first column of each row . I return a QIcon in ::data . My requirement is to edit the image when I double click on this image
class myView : public QTreeView{
private:
myModel d_m;
};class myModel:public QAbstractItemmodel
};myView::myview() {
myModel m;
setModel(m);
d_m =m;
}void myInstView::clickInstance(const QModelIndex& index)
{
d_m->setData(setData(currentIndex(),color,Qt::DecorationRole);;
}boo myModel::setData(const QModelIndex& index,const QVariant& variant, const int role)
{
if (role == Qt::DecorationRole && index.column() == 1) {
QColor color = variant.value<QColor>();
myMap[pointer] = image;
addIamgeToMap(image);
/* set color for all the visible children
}
}QVariant myModel::data(const QModelIndex& index, int role) const
{
if(index.coumn ==1) && role== Decoration role {
return myMap[pointer] ;
}
}My Query is is sending Decoration in setData is fine and is the logic is also fine ?
-
Doing this kind of things is exactly what delegates are made for. take a look at http://doc.qt.io/qt-5/model-view-programming.html#delegate-classes and QStyledItemDelegate
-
I took help from this example
http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
what is the problem in my code
-
@Qt-Enthusiast said:
I have a function in which I show a image in first column of each row
you are checking for
index.coumn()==1
so that's the second columnclass myView : public QTreeView{
private:
myModel d_m;
};Usually it's not a great idea to make the view own the model. you can do it (that's what the Q*Widgets do) but it's often not the best way
class myModel:public QAbstractItemmodel
};myView::myview() {
myModel m;
setModel(m);
d_m =m;how can you do this? QObjects can't be copied also the logic here is wrong as you store a copy of the model but then use the original one as base for the view
void myInstView::clickInstance(const QModelIndex& index)
what is myInstView?
return myMap[pointer] ;
What is pointer?
the logic is also fine ?
using the decoration role to display the image is 100% correct but to edit the image you should use a delegate
-
I am using qt.4.3.3 and qstyleditemdelegate.html is available fro,m qt4.4 so I cannot used qstyleditemdelegat please let me know if I impilimentation is ok
-
you can use QItemDelegate, the only difference is that QStyledItemDelegate uses stylesheets while QItemDelegate doesn't http://doc.qt.io/qt-4.8/qitemdelegate.html
-
can u point me exact example where QtreeView , customodel and a deletegate is used so that I understand the things better
Since storing a model pointer in View is my legacy code can I call
class myView::QTreeView {
}void myView::clicik() {
d_m->setData
} -
create a class that inherits from QItemDelegate and reimplement
virtual QWidget* createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const; virtual void setEditorData(QWidget * editor, const QModelIndex & index) const; virtual void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const; virtual QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const; virtual void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;
the widget returned from
createEditor
is displayed inside the view cell during editing,setEditorData
is used to load data from the model to the editor,setModelData
is used to save data from the editor to the model.updateEditorGeometry
is used to resize the editor in case the view gets resized during editing andsizeHint
tells the view how much space your image should takefinally you use QTreeView::setItemDelegateForColumn to set the delegate on the view and voilà
you can use QTreeView::setEditTriggers to chose when your editor should pop up ( QAbstractItemView::DoubleClicked I think in your case)
-
My question is what is advantage of using delegate since my legacy code my View owns the model pointer
is there any advandathe
-
not knowing what
myInstView
is I can't provide a definitive answer but handling your editor widget lifetime, positioning it correctly, resizing it correctly and handling when the editing starts and finishes all out of the box are pretty big advantages for me -
it is a object of QTreeVIew
-
That's not particularly helpful... what class is it? a button? a lineedit?
-
a QTreeView
-
I don't get the difference between myInstView and myView but it does not matter. Delegate is the answer.
My requirement is to edit the image when I double click on this image
if you want any more details you need to describe better how the image should be changed. is it selected from a combobox, is it a color button, is it loaded from file?