0
votes

I have an user interface that uses a TableView. It has 3 columns. The last column has a comboBox. All the data is inserted with the delegate. The problem is I can not find a method to send a signal to a public slot of the user interface class when the combobox index is changed. With the delegate I already know the current index. Do someone know a method to send this index to the ui? I do not think the only possible solution is with signals and slots. Is a direct solution to extract this data?

EDIT

What I understand it is I have to do something like this:

void Delegate :: setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
 if(index.column() == COL_Coordonate) // test if we are at the last column
   {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        model -> setData(index, comboBox -> currentIndex(), Qt::EditRole);
        emit dataChanged(comboBox -> currentIndex(),comboBox -> currentIndex()); // something like this you have in mind?
   }

}

And how can I receive that index in the user interface? I create my model in there something like:

QStandardItemModel *model;
Delegate *mydelegate;

And use them like:

mydelegate = new Delegate(this);
model = new QStandardItemModel(0, 3, this); // I add rows dynamically
ui -> tableView -> setModel(model);
ui -> tableView -> setItemDelegate (mydelegate);

I add data with the delegate when I press a button. Do I need do trigger a slot from this interface? If so someone can please provide a sample of code about how do I do this?

1
The delegate must never emit any of the model's signals. It's the model's job to do that: it already emits these signals. You need to connect your code (a slot or a functor) to the model: connect(model, &QAbstractItemModel::dataChanged, this, [=](const QModelIndex & index){ /* your code here */ });Kuba hasn't forgotten Monica
If you want examples, search e.g. on my user for QAbstractItemModel or QStandardItemModel or QListView or QTableView.Kuba hasn't forgotten Monica

1 Answers

2
votes

You have a QComboBox instance. You can connect to its signals. What do you not know? In any case, you should not be connecting to the delegate: it is an implementation detail of the view. You should interface with the model, not with the view. Connect to the model's dataChanged signal!