I assume that you are using a custom model that inherits QAbstractItemModel . In this case, you can write an insert method:
bool CustomModel::insertMyItem(const MyItemStruct &i) { if (alredyHave(i)) return false; beginInsertRow(); m_ItemList.insert(i); endInsertRow(); }
Your data method would be something like this:
QVariant CustomModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole || role == Qt::ToolTipRole) switch (index.column()) { case INDEX_ID: return m_ItemList[index.row()].id; case INDEX_NAME: return m_ItemList[index.row()].name; ... } return QVariant(); }
And finally, your input method will be:
void MainWindow::input() { MyInputDialog dialog(this); if (dialog.exec() == QDialog::Rejected) return; myModel->insertMyItem(dialog.item()); }
source share