- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题如下:
有一个QTableView
和一个QStandardItemModel
是这样使用的:
ui->tableView->setModel(model);
model->setItem(myrow, mycolumn, myQStandardItem);
和一个组合框委托(delegate):
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]);
}
最佳答案
您的委托(delegate)实现的问题是您没有发出 commitData组合索引更改时发出信号。它在 Qt 文档中说明:
This signal must be emitted when the editor widget has completed editing the data, and wants to write it back into the model.
您可以将组合框作为委托(delegate)类的成员,并将组合框的 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
信号会在组合框的索引改变时发出。
关于c++ - dataChanged 信号不适用于 ComboBoxDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30216009/
我是一名优秀的程序员,十分优秀!