dataChanged信号不适用于ComboBoxDelegate

时间:2015-05-13 13:16:42

标签: c++ qt combobox qtableview qstyleditemdelegate

我的问题如下:

以这种方式使用了QTableViewQStandardItemModel

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

和一个comboboxdelegate:

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


   }

}

1 个答案:

答案 0 :(得分:1)

您的委托实现的问题在于,当组合索引发生更改时,您不会发出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信号。