1

我从QStyledItemDelegate. 我QComboBox在这个委托中使用 a 。此委托用于QTableView.

我的问题是,如何以编程方式更改委托中组合框的索引,即如何在委托类之外访问指向该小部件的指针?我已经检查过CreateEditor,当我们单击组合框时会自动调用(of) 函数,SetEditorData并且我们无法手动调用它们来操作模型中的数据。SetModelDataQStyledItemDelegate

4

2 回答 2

1

每当您开始编辑并显示组合框时,afaik 都会分配一个新的。如果你想有一个永久的组合框,你应该看看

QTableView::setIndexWidget(const QModelIndex&, QWidget*)

因此您可以使用以下代码访问组合框:

const QMoodelIndex idx = model->index(row, column);
QWidget* wid = view->indexWidget(idx);
QComboBox* box = qobject_cast<QComboBox*>(wid);
if (box)
    // do your thing
于 2015-03-20T09:55:01.253 回答
0

您可以将组合框的内容作为QStringList. 您的项目委托可以是:

#include <QStyledItemDelegate>

#include <QComboBox>

class ComboBoxDelegate: public QStyledItemDelegate
{
 Q_OBJECT
public:
    ComboBoxDelegate(QObject *parent = 0);

    QWidget *createEditor( QWidget *parent,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index ) const;

    void setEditorData( QWidget *editor,
                            const QModelIndex &index ) const;

    void setModelData( QWidget *editor,
                            QAbstractItemModel *model,
                            const QModelIndex &index ) const;

    void updateEditorGeometry( QWidget *editor,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index ) const;

    QStringList comboItems;

    mutable QComboBox *combo;

private slots:

    void setData(int val);

};

ComboBoxDelegate::ComboBoxDelegate(QObject *parent ):QStyledItemDelegate(parent)
{
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    combo = new QComboBox( parent );
    QObject::connect(combo,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));
    combo->addItems(comboItems);
    combo->setMaxVisibleItems(comboItems.count());
    return combo;
}

void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString text = index.model()->data( index, Qt::DisplayRole ).toString();

    int comboIndex = comboItems.indexOf(QRegExp(text));

    if(comboIndex>=0)
        (static_cast<QComboBox*>( editor ))->setCurrentIndex(comboIndex);
}

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    model->setData( index, static_cast<QComboBox*>( editor )->currentText() );
}


void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry( option.rect );
}

void ComboBoxDelegate::setData(int val)
{
    emit commitData(combo);
    //emit closeEditor(combo);
}

当您想在代码中的某处更新组合框中的项目时,只需通过调用itemDelegateForColumn和访问comboItems成员来获取指向项目委托的指针:

ComboBoxDelegate * itemDelegate = qobject_cast<ComboBoxDelegate *>(ui->tableView->itemDelegateForColumn(columnIndex));

//Updating combobox items
itemDelegate->comboItems.append("newItem");
...
于 2015-03-20T10:47:03.817 回答