1

我正在尝试制作一个表格视图,其中的每一列都有一个单独的下拉列表。用户只能选择值的组合。也就是说,如果用户从第一个下拉列表中选择“A”,则其他下拉列表中的值应更新为可以匹配“A”的值。

我已经创建了我的 AbsractItemDelegate 类,并且这些值被分配得很好。但是我对如何在下拉列表中的值发生变化时触发事件感到困惑。

谢谢。

以下是我的委托类实现:

FillComboBox::FillComboBox(QStringList the_list) : QItemDelegate() {
//list = new QStringList();
list = the_list; }

QWidget* FillComboBox::createEditor(QWidget* parent,
const QStyleOptionViewItem& /* option */,
const QModelIndex& /* index */) const {
QComboBox* editor = new QComboBox(parent);
editor->addItems(list);
editor->setCurrentIndex(2);
return editor; }


void FillComboBox::setEditorData(QWidget* editor,
const QModelIndex &index) const {
QString text = index.model()->data(index, Qt::EditRole).toString();
QComboBox* combo_box = dynamic_cast<QComboBox*>(editor);
combo_box->setCurrentIndex(combo_box->findText(text)); }


void FillComboBox::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex &index) const {
QComboBox* combo_box = dynamic_cast<QComboBox*>(editor);
QString text = combo_box->currentText();
model->setData(index, text, Qt::EditRole); }

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

1 回答 1

1

您可以在当前项目的数据更新后立即更新“其他”项目的数据,即在FillComboBox::setModelData(). 请找到实现所需行为的伪代码(见评论):

void FillComboBox::setModelData(QWidget* editor, QAbstractItemModel* model,
                                const QModelIndex &index) const
{
    QComboBox* combo_box = dynamic_cast<QComboBox*>(editor);
    QString text = combo_box->currentText();
    model->setData(index, text, Qt::EditRole);

    // Find the model index of the item that should be changed and its data too
    int otherRow = ...; // find the row of the "other" item
    int otherColumn = ...; // find the column of the "other" item
    QModelIndex otherIndex = model->index(otherRow, otherColumn);
    QString newText = text + "_new";
    // Update other item too
    model->setData(otherIndex, newText, Qt::EditRole);
}
于 2014-07-04T09:23:47.843 回答