至少有两种方法。
- 对 Qt 的模型
itemChanged 信号使用自然。
- 从您的代理发出信号并在您的主窗口中捕获它。
如果你的委托是标准的,这意味着在 setModelData() 方法中你有类似的东西:
QComboBox *line = static_cast<QComboBox*>(editor);
QString data = line->currentText();
//...
model->setData(index, data);
那么我认为你应该使用自然的方式。例如:
connect(model,&QStandardItemModel::itemChanged,[=](QStandardItem * item) {
if(item->column() == NEEDED_COLUMN)
{
//you found, just get data and use it as you want
qDebug() << item->text();
}
});
我在这里使用了C++11(CONFIG += c++11 到.pro 文件)和new syntax of signals and slots,当然你可以根据需要使用旧语法。
我已经复制了您的代码(使用组合框进行委托),如果我在组合框中选择某些内容并通过输入点击确认,我的解决方案就可以工作。但是,如果您想获得自动更改数据的解决方案,当您在组合框中选择另一个项目(不按 Enter)时,请参阅下一个案例:
在委托上创建特殊信号:
signals:
void boxDataChanged(const QString & str);
在createEditor()方法内创建连接:
QWidget *ItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
connect(editor,SIGNAL(currentIndexChanged(QString)),this,SIGNAL(boxDataChanged(QString)));
return editor;
}
并使用它!
ItemDelegate *del = new ItemDelegate;
ui->tableView->setItemDelegate( del);
ui->tableView->setModel(model);
connect(del,&ItemDelegate::boxDataChanged,[=](const QString & str) {
//you found, just get data and use it as you want
qDebug() << str;
});