
Originally Posted by
Misenko
But why this dont work? Or is there other way to do that with signals-slots system?
when you call setCurrentIndex then editor will be closed after that QItemDelegate::setModelData is called and empty value set in model.
...
{
QLineEdit *lineEdit
= static_cast<QLineEdit
*>
(editor
);
if (value.isEmpty()) {
QMetaObject::invokeMethod(const_cast<LineEditDelegate
*>
(this),
"keepEditorOpened", Qt
::QueuedConnection, Q_ARG
(QModelIndex, index
));
return;
}
model->setData(index, value, Qt::EditRole);
}
...
...
void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
QString value = lineEdit->text();
if (value.isEmpty()) {
QMetaObject::invokeMethod(const_cast<LineEditDelegate *>(this), "keepEditorOpened", Qt::QueuedConnection, Q_ARG(QModelIndex, index));
return;
}
model->setData(index, value, Qt::EditRole);
}
...
To copy to clipboard, switch view to plain text mode
signal
signals:
signals:
void keepEditorOpened(const QModelIndex &index);
To copy to clipboard, switch view to plain text mode
must be deslarated in lineeditdelegate.h
then in widget do next
{
tableView->setModel(model);
LineEditDelegate *delegate = new LineEditDelegate();
tableView->setItemDelegate(delegate);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 2; ++column) {
model
->setData
(index,
QVariant((row
+1) * (column
+1)));
}
}
...
{
tableView->edit(index);
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
QStandardItemModel *model = new QStandardItemModel(4, 2);
tableView = new QTableView();
tableView->setModel(model);
LineEditDelegate *delegate = new LineEditDelegate();
tableView->setItemDelegate(delegate);
connect(delegate, SIGNAL(keepEditorOpened(const QModelIndex &)), SLOT(openEditor(const QModelIndex &)));
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 2; ++column) {
QModelIndex index = model->index(row, column, QModelIndex());
model->setData(index, QVariant((row+1) * (column+1)));
}
}
...
void MainWindow::openEditor(const QModelIndex &index)
{
tableView->edit(index);
}
To copy to clipboard, switch view to plain text mode
I hope this is not complicated example.
Bookmarks