Well to make a final conclusion this is what needs to be done:
1) Register the custom struct:
struct MyObject {
typedef QPair<QString, QString> MyPair;
typedef QList<MyPair> MyList;
MyList myList;
};
Q_DECLARE_METATYPE(MyObject)
struct MyObject {
typedef QPair<QString, QString> MyPair;
typedef QList<MyPair> MyList;
MyList myList;
};
Q_DECLARE_METATYPE(MyObject)
To copy to clipboard, switch view to plain text mode
2) Inserting items to the model:
MyObject myObject;
myObject.myList.push_back(
);
myObject.myList.push_back(
);
model->setData(index, qVariantFromValue(myObject));
tableView->openPersistentEditor(index);
QModelIndex index = model->index(row, column);
MyObject myObject;
myObject.myList.push_back(
MyObject::MyPair(QString("default"),QString("hello.png"))
);
myObject.myList.push_back(
MyObject::MyPair(QString("active"),QString("world.png"))
);
model->setData(index, qVariantFromValue(myObject));
tableView->openPersistentEditor(index);
To copy to clipboard, switch view to plain text mode
3) Create the item delegate like the star delegate example:
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if(qVariantCanConvert<MyObject>(index.data())){
editor->setAutoFillBackground(true);
return editor;
}else{
}
}
void MyDelegate::setEditorData(
{
if(qVariantCanConvert<MyObject>(index.data())){
QComboBox* comboBox
= static_cast<QComboBox
*>
(editor
);
MyObject auxObject = qVariantValue<MyObject>(index.data());
foreach(MyObject::MyPair par, auxObject.myList){
comboBox->addItem(par.first+"\t"+par.first);
}
}else{
}
}
QWidget* MyDelegate::createEditor(
QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if(qVariantCanConvert<MyObject>(index.data())){
QComboBox* editor = new QComboBox(parent);
editor->setAutoFillBackground(true);
return editor;
}else{
return QItemDelegate::createEditor(parent, option, index);
}
}
void MyDelegate::setEditorData(
QWidget *editor,
const QModelIndex &index) const
{
if(qVariantCanConvert<MyObject>(index.data())){
QComboBox* comboBox = static_cast<QComboBox*>(editor);
MyObject auxObject = qVariantValue<MyObject>(index.data());
foreach(MyObject::MyPair par, auxObject.myList){
comboBox->addItem(par.first+"\t"+par.first);
}
}else{
return QItemDelegate::setEditorData(editor, index);
}
}
To copy to clipboard, switch view to plain text mode
4) Set the item delegate to the QTableView:
MyDelegate* myDelegate = new MyDelegate();
tableView->setItemDelegate(myDelegate);
MyDelegate* myDelegate = new MyDelegate();
tableView->setItemDelegate(myDelegate);
To copy to clipboard, switch view to plain text mode
Thats it.
But I have some last question, By default items that are not "MyObject" are editable, this is because I'm making them with QItemDelegate::createEditor I think. I have tried customizing this widget but I cant see a setEditable method. So, what is the real default widget of a QTableView item?
Bookmarks