1

私の問題は次のとおりです。

このように使用されるaQTableViewと aがあります。QStandardItemModel

ui->tableView->setModel(model);
model->setItem(myrow, mycolumn, myQStandardItem);

およびコンボボックスデリゲート:

ComboBoxDelegate* mydelegate = new ComboBoxDelegate();
ui->tableView->setItemDelegateForColumn(mycolumn,mydelegate);

テーブルのセルの値が(コンボボックスによって)変更されるたびに、新しい値と変更されたセルのインデックスをキャッチする必要がありますdataChaged。モデルに関連付けられた信号を次のように使用しています。

connect(model,SIGNAL(dataChanged(QModelIndex&,QModelIndex&)),this,SLOT(GetChangedValue(QModelIndex&)));

GetChangedValueコンボボックスの値が変更されても、メソッドを呼び出すことはありません。ステップをスキップしていますか?

のコードの下にComboBoxDelegate

class ComboBoxDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:

    ComboBoxDelegate(QVector<QString>& ItemsToCopy,QObject *parent = 0);
    ~ComboBoxDelegate();
     void setItemData(QVector<QString>& ItemsToCopy);

    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;



    private:
     QVector<QString> Items;

};

ComboBoxDelegate::ComboBoxDelegate(QVector<QString>&  ItemsToCopy,QObject  *parent)
:QStyledItemDelegate(parent)
{
    setItemData(ItemsToCopy);
}


ComboBoxDelegate::~ComboBoxDelegate()
{
}


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

    QComboBox* editor = new QComboBox(parent);
    editor->setEditable(true);



    for (int i = 0; i < Items.size(); ++i)
    {
        editor->addItem(Items[i]);
    }


    editor->setStyleSheet("combobox-popup: 0;");

    return editor;
}




void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex  &index) const
{
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    QString currentText = index.data(Qt::EditRole).toString();
    int cbIndex = comboBox->findText(currentText);
    comboBox->setCurrentIndex(cbIndex);
}



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

}



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

}



void ComboBoxDelegate::setItemData(QVector<QString>&  ItemsToCopy)
{
    for (int row = 0; row < ItemsToCopy.size(); ++row)
    {

       Items.push_back(ItemsToCopy[row]);


   }

}

4

1 に答える 1

3

デリゲートの実装の問題は、コンボ インデックスが変更されたときにcommitDataシグナルを発行しないことです。Qtのドキュメントに記載されています:

このシグナルは、エディター ウィジェットがデータの編集を完了し、それをモデルに書き戻したい場合に発行する必要があります。

コンボボックスをデリゲートクラスのメンバーとして持つことができ、コンボボックスのcurrentIndexChanged信号を放出するスロットに接続できますcommitData:

#include <QItemDelegate>

#include <QComboBox>

class ComboBoxDelegate: public QItemDelegate
{
 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 ):QItemDelegate(parent)
{
        comboItems<<"Item 1"<<"Item 2"<<"Item 3";
}

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);
}

ご覧のとおりcurrentIndexChanged、コンボボックスの信号はsetData、データをモデルにコミットするスロットに接続されています。また、定数であるコンボボックスを新規作成するために可変として宣言する必要がcreateEditorあります。constデータ メンバーが変更可能であると宣言されている場合、メンバー関数からこのデータ メンバーに値を割り当てることは正当です。

dataChangedコンボボックスのインデックスが変更されると、シグナルが発せられるようになりました。

于 2015-05-14T04:25:08.920 に答える