工作,我的問題是這樣的:dataChanged信號不ComboBoxDelegate
有以這種方式使用一個QTableView
和QStandardItemModel
:
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]);
}
}
您是否看到編輯器在表格視圖中設置的新值? 'connect'函數是否返回'true'? – hank