1

からクラスを派生させましたQStyledItemDelegateQComboBoxこのデリゲートでa を使用しています。このデリゲートは で使用されQTableViewます。

私の質問は、デリゲートのコンボボックスのインデックスをプログラムで変更するにはどうすればよいですか。つまり、デリゲート クラスの外でそのウィジェットへのポインタにアクセスする方法です。CreateEditorコンボボックスをクリックするとSetEditorDataSetModelData関数 (のQStyledItemDelegate) が自動的に呼び出され、モデル内のデータを操作するために手動で呼び出すことはできないことを確認しました。

4

2 に答える 2

1

編集を開始してコンボボックスが表示されるたびに、新しいものが割り当てられます。永続的なコンボボックスが必要な場合は、次を確認する必要があります

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 に答える