gpt4 book ai didi

c++ - 将数据从模型发送到委托(delegate)

转载 作者:行者123 更新时间:2023-11-28 05:32:55 27 4
gpt4 key购买 nike

我有一个 tableView,其中有一列正在使用 comboBox。我需要使用委托(delegate)类将模型类中的数据填充到 comboBox 中。我为此任务使用了信号和槽,但我知道有一种方法使用 data

这就是我创建和填充 comboBox 的方式。我需要直接从模型类中获取文件行,而无需将其存储在委托(delegate)中。

QWidget *CDelegate :: createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex & index) const
{

if(index.column() == COL_ComboBox)
{
QComboBox *editor = new QComboBox(parent);

for(int i=0; i<file.at(index.row()).size(); i++)
editor -> addItem(file.at(index.row()).at(i))

return editor;
}
...
}

最佳答案

据我所知,您想用QTableView 模型中的数据填充QComboBox。如您所见,createEditor 函数中的const QModelIndex & index 参数使您可以访问此模型。找方法model QModelIndex 类。这就是为什么,您的 createEditor 函数可能是这样的:

QWidget *CDelegate :: createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex & index) const
{

if(index.column() == COL_ComboBox)
{
QComboBox *editor = new QComboBox(parent);

const QAbstractItemModel *model = index.model();

while(/*condition*/)
{
// take data from model
// QVariant dt = model->data(...);

// fill editor with data from dt
// editor->addItem(...)
}

return editor;
}
...
}

关于c++ - 将数据从模型发送到委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39010546/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com