#include <QtWidgets>
class Delegate : public QStyledItemDelegate
{
public:
explicit Delegate
(QObject * parent
= 0) : QStyledItemDelegate
(parent
) {}
{
widget->addItems(index.data(Qt::UserRole+1).toStringList());
return widget;
}
{
if(index.column() == 0)
{
QComboBox * widget
= dynamic_cast<QComboBox
*>
(editor
);
if(widget)
{
QString country
= widget
->currentText
();
QVariant russia
= QStringList() <<
"Moscow" <<
"Saint Petersburg" <<
"Novosibirsk" <<
"Yekaterinburg";
QModelIndex nextColumnIndex
= model
->index
(index.
row(),
1);
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[])
{
for (int row = 0; row < 4; row++)
{
item
->setData
(QStringList() <<
"Russia" <<
"Canada" <<
"China" <<
"United States",Qt
::UserRole+1);
model.setItem(row, 0, item);
model.setItem(row, 1, item);
}
table.setModel(&model);
table.setItemDelegate(new Delegate(&model));
table.show();
return app.exec();
}
#include <QtWidgets>
class Delegate : public QStyledItemDelegate
{
public:
explicit Delegate(QObject * parent = 0) : QStyledItemDelegate(parent) {}
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox * widget = new QComboBox(parent);
widget->addItems(index.data(Qt::UserRole+1).toStringList());
return widget;
}
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
if(index.column() == 0)
{
QComboBox * widget = dynamic_cast<QComboBox *>(editor);
if(widget)
{
QString country = widget->currentText();
QVariant russia = QStringList() << "Moscow" << "Saint Petersburg" << "Novosibirsk" << "Yekaterinburg";
QVariant canada = QStringList() << "Toronto" << "Parliament Hill" << "Quebec" << "Montreal";
QVariant china = QStringList() << "Shanghai" << "Beijing" << "Chongqing" << "Hong Kong";
QVariant states = QStringList() << "New York City" << "Los Angeles" << "Chicago" << "Houston";
QModelIndex nextColumnIndex = model->index(index.row(), 1);
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[])
{
QApplication app(argc, argv);
QTableView table;
QStandardItemModel model(4, 2);
for (int row = 0; row < 4; row++)
{
QStandardItem *item = new QStandardItem(QString("Select Country").arg(row).arg(0));
item->setData(QStringList() << "Russia" << "Canada" << "China" << "United States",Qt::UserRole+1);
model.setItem(row, 0, item);
item = new QStandardItem(QString("Select City").arg(row).arg(1));
item->setData(QStringList());
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
Bookmarks