簡體   English   中英

將數據從模型發送到委托

[英]Send data from a model to a delegate

我有一個tableView ,其中有一列正在使用comboBox 我需要使用委托類填充來自模型類的數據的comboBox 我當時正在使用信號和插槽來執行此任務,但我知道有一種使用data的方法。

這就是我創建和填充comboBox 我需要直接從模型類中獲取文件行,而無需將其存儲在委托中。

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參數使您可以訪問此模型。 尋找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;
    }
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM