1

I am trying to make a tableview with a row which has a separate dropdown for each column. The user can only select a combination of values. That is, if the user selects "A" from the first drop down, the values in the other drop downs should be updated to that which can match "A".

I have made my AbsractItemDelegate class and the values are being assigned fine. But I am stuck up at how I can trigger an event when a value changes in one of the drop downs.

Thanks.

The following is my delegate class implementation:

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); }
jm.
  • 179
  • 1
  • 3
  • 13

1 Answers1

1

You can update the data of the "other" item as soon as the current item's data is updating, i.e. in FillComboBox::setModelData(). Please find the pseudo code that implements desired behavior (see comments):

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);
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • I want to update the list at the other indices, not just a single item :/ – jm. Jul 04 '14 at 12:52
  • @user2852500, fine, but you can do the same for multiple indexes. Actually, you can do almost everything with your table in `FillComboBox::setModelData()`. – vahancho Jul 04 '14 at 13:10
  • I can change the list of the editor of other "items" ? – jm. Jul 04 '14 at 13:44
  • @user2852500, what means "list of the editor"? Do you mean combo box? You can do that when you open the drop down. You can not have two drop downs opened in the same time. – vahancho Jul 04 '14 at 13:48
  • Yes. In the beginning each ComboBox has a list of values that it shows, and I want to modify the list of the other combo boxes as the selection of one combo box is changed. I don't want to open two combo boxes at the same time, but if the user selects something in first combo box, then when the second combo box is opened, the values in that drop down list are different. – jm. Jul 04 '14 at 14:03