How did you put combo boxes in table view, did you use setIndexWidget()?
How did you put combo boxes in table view, did you use setIndexWidget()?
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Via combo box delegate classinherited by "QStyledItemDelegate"
So how is that you are doing? Painting the combo box from delegate and when clicked creating an editor for the item using delegate?
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
No, I am creating combo box in QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option , const QModelIndex &index ) const method while i can check the "QModelIndex &index" in prder to check the specific column. Everything is working fine but only problem is that i have to make some logic that when i change something in column 4 then respectively columns 5 combo box has to be changed also.
When the column 4 QComboBox is edited and data is submitted to the model, the model internally (in setData()) has to adjust the data for the column 5 and emit dataChanged() signal for the column 5 model index.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Well i am setting the model via setData() by void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const function but when i send the data to the model it was currently having the pointer of column 4 not column 5. QcomboBox is working
e.g. see below Model
Qt Code:
{ int row = index.row(); int col = index.column(); QMap<QString, QVariant> qMapPair = p_tagSimItems->getItemsMap().at(row); if(index.isValid() && role == Qt::EditRole) { switch(col) { case 0 : { qMapPair.insert("a",value.toString()); p_tagSimItems->setItemMap(row,qMapPair); emit (dataChanged(index , index)); return true; } break; case 1 : { qMapPair.insert("b",p_tagSimItems->isValidEnteredType(value.toString())); p_tagSimItems->setItemMap(row,qMapPair); emit (dataChanged(index , index)); return true; } break; case 2 : { qMapPair.insert("c",value.toString().toUShort(0,10)); p_tagSimItems->setItemMap(row,qMapPair); emit (dataChanged(index , index)); return true; } break; case 3 : { [B]// the problem is here when model receive the signal from delegate in column 4 then i have to change the column 5 also manually which seems not possible because model is on column 4 .[/B] if (value.toString().toUShort(0,10) < 256) { qMapPair.insert("d",value.toString().toUShort(0,10)); }else { qMapPair.insert("d",0); } p_tagSimItems->setItemMap(row,qMapPair); emit (dataChanged(index , index)); return true; } break; case 4 : { if (value.toString().toUShort(0,10) < 256) { qMapPair.insert("e",value.toString().toUShort(0,10)); }else { qMapPair.insert("e",0); } p_tagSimItems->setItemMap(row,qMapPair); emit (dataChanged(index , index)); return true; } break; default : break; } } return false; }To copy to clipboard, switch view to plain text mode
After this, model call thefunction in order to set the Index of the combo box.Qt Code:
To copy to clipboard, switch view to plain text mode
Last edited by uzairsaeed702; 3rd March 2015 at 09:30.
When inside setData() for column 4, you could call setData for column 5, you just need to create a new index (by calling QAbstractItemModel::createIndex) for column 5 and then pass it to the setData();// the problem is here when model receive the signal from delegate in column 4 then i have to change the column 5 also manually which seems not possible because model is on column 4 .
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Here you go, run this code.
I used delegate (because I used standard model) but you could do the similar in the your custom model.
Qt Code:
#include <QtWidgets> class Delegate : public QStyledItemDelegate { public: QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { widget->addItems(index.data(Qt::UserRole+1).toStringList()); return widget; } { if(index.column() == 0) { if(widget) { QVariant russia = QStringList() << "Moscow" << "Saint Petersburg" << "Novosibirsk" << "Yekaterinburg"; if(country == "Russia") { model->setData(nextColumnIndex, russia, Qt::UserRole+1); } else if(country == "Canada") { model->setData(nextColumnIndex, canada, Qt::UserRole+1); } else if(country == "China") { model->setData(nextColumnIndex, china, Qt::UserRole+1); } else if(country == "United States") { model->setData(nextColumnIndex, states, Qt::UserRole+1); } model->setData(index, country); model->setData(nextColumnIndex, "Select City Again"); } widget->addItems(index.data(Qt::UserRole+1).toStringList()); } else { QStyledItemDelegate::setModelData(editor, model, index); } } }; int main(int argc, char *argv[]) { QTableView table; for (int row = 0; row < 4; row++) { model.setItem(row, 0, item); model.setItem(row, 1, item); } table.setModel(&model); table.setItemDelegate(new Delegate(&model)); table.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Santosh Reddy thanks alot for the code but in custom model its difficult to examine the problem .Your hint for AbstractItemModel::createIndex worked like charm !!! . I have just inserted two line of code in model and few lines of code in my delegate class and work done.
Qt Code:
setData(changeRequest ,Qt::EditRole);To copy to clipboard, switch view to plain text mode
Above was my modification ,
Thanks again Santosh
Problem Solved (Y)
Bookmarks