【问题标题】:Qt - Triggering an event when selection of abstract item delegate changesQt - 在选择抽象项目委托更改时触发事件
【发布时间】:2014-07-04 07:58:22
【问题描述】:

我正在尝试制作一个表格视图,其中的每一列都有一个单独的下拉列表。用户只能选择值的组合。也就是说,如果用户从第一个下拉列表中选择“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); }

【问题讨论】:

  • 您能粘贴您的代理的代码 sn-p 吗?只需编辑您的问题并将其放在那里。

标签: qt qtableview qitemdelegate


【解决方案1】:

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

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

【讨论】:

  • 我想更新其他索引处的列表,而不仅仅是单个项目:/
  • @user2852500,很好,但是您可以对多个索引执行相同的操作。实际上,您几乎可以使用FillComboBox::setModelData() 中的表格完成所有操作。
  • 我可以更改其他“项目”的编辑器列表吗?
  • @user2852500,“编辑列表”是什么意思?你是说组合框吗?您可以在打开下拉菜单时执行此操作。您不能同时打开两个下拉菜单。
  • 是的。一开始,每个 ComboBox 都有一个它显示的值列表,我想在更改一个组合框的选择时修改其他组合框的列表。我不想同时打开两个组合框,但是如果用户在第一个组合框中选择了一些东西,那么当打开第二个组合框时,该下拉列表中的值是不同的。
猜你喜欢
  • 2021-06-27
  • 2021-08-28
  • 1970-01-01
  • 2020-10-27
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
相关资源
最近更新 更多