Question

I need to implement a table in Qt that shows a QComboBox on each row on a particular column.

Based on this question: QStandardItem + QComboBox I succesfully managed to create a QItemDelegate. In that example the QComboBox contents are defined statically on ComboBoxDelegate class, but in my case I need to define the QComboBox contents within the function where the QStandardItemModel is created.

The model is defined inside a MainWindow class method:

void MainWindow::fooHandler()  {
    QStandardItemModel*  mymodel = new QStandardItemModel;
    ui->tablePoint->setModel(mymodel);
    ComboBoxDelegate* delegate=new ComboBoxDelegate;
    ui->tablePoint->setItemDelegateForColumn(2,delegate);

    QStringList Pets;
    Pets.append("cat");
    Pets.append("dog");
    Pets.append("parrot");

    // So far this is how I tried to store data under `Qt::UserRole` in "mymodel":

    QModelIndex idx = mymodel->index(0, 2, QModelIndex());
    mymodel->setData(idx,QVariant::fromValue(Pets), Qt::UserRole);

    //Now i fill the table with some values...
    QList< QStandardItem * > items;
    items.clear();
    items << new QStandardItem("col0");
    items << new QStandardItem("col1");
    items << new QStandardItem("parrot");
    items << new QStandardItem("col3");
    mymodel->appendRow(items);

    items.clear();
    items << new QStandardItem("col0");
    items << new QStandardItem("col1");
    items << new QStandardItem("cat");
    items << new QStandardItem("col3");
    mymodel->appendRow(items);
    }

Then I should be able to recover the ComboBox contents from the delegate class:

void ComboBoxDelegate::setEditorData(QWidget *editor,
                                     const QModelIndex &index) const
   {
   QString value = index.model()->data(index, Qt::EditRole).toString();
   QComboBox *cBox = static_cast<QComboBox*>(editor);

   if(index.column()==2) {
      QModelIndex idx = index.model()->index(0, 2, QModelIndex());
      cBox->addItem( index.model()->data(idx,Qt::UserRole).toStringList().at(0) );
      cBox->addItem( index.model()->data(idx,Qt::UserRole).toStringList().at(1) );
      cBox->addItem( index.model()->data(idx,Qt::UserRole).toStringList().at(2) );
      }
   cBox->setCurrentIndex(cBox->findText(value));
   }

The project compiles well but when I click on a cell to change the QComboBox value the program crashes and I got an "Invalid parameter passed to C run time function."

Was it helpful?

Solution

My problem was that I was trying to use mymodel->setdata() before I append rows to the model.

So If at first I should do:

QList< QStandardItem * > items;
items.clear();
items << new QStandardItem("col0");
items << new QStandardItem("col1");
items << new QStandardItem("parrot");
items << new QStandardItem("col3");
mymodel->appendRow(items);

and ONLY then...

QModelIndex idx = mymodel->index(0, 2, QModelIndex());
mymodel->setData(idx,QVariant::fromValue(Pets), Qt::UserRole);

This solved the issue.

Thank you all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top