Добрый день!
Подскажите пожалуйста, как решить такую проблему:
После того как ячейка отредактирована, редактор закрывается и фокус передается на следующую ячейку, находящуюся справа.
А мне нужно чтобы фокус оставался на той же ячейке которая редактировалась...
Обычно я для этого в делегате пишу такую строку(в методе setModelData):
((QTableView *)this->parent())->setCurrentIndex(index);
Да, сделал делегат, и вроде все написал как обычно.
А если установить QItemDelegate то все работает нормально.
Значит в делегате ошибся.
Спасибо!
class ReportDataDelegate : public QItemDelegate {
Q_OBJECT
public:
ReportDataDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
public slots:
void commitAndCloseEditor();
};
ReportDataDelegate::ReportDataDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *ReportDataDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
editorType etype;
if (index.column() == 0 && index.row() > 2) {
etype = strEdit;
} else
if (index.column() > 0 && index.row() == 0) {
etype = intEdit;
} else
if (index.column() > 0 && (index.row() == 1 || index.row() == 2)) {
etype = strEdit;
} else
if (index.column() > 0 && index.row() > 2) {
etype = dblEdit;
}
ReportDataEditor *editor = new ReportDataEditor(etype, parent);
connect(editor, SIGNAL(Finished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
void ReportDataDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
ReportDataEditor *ed = qobject_cast<ReportDataEditor *>(editor);
ed->edit->setText(index.data(Qt::EditRole).toString());
}
void ReportDataDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
ReportDataEditor *ed = qobject_cast<ReportDataEditor *>(editor);
model->setData(index, ed->edit->text(), Qt::EditRole);
((QTableView *)this->parent())->setCurrentIndex(index);
}
void ReportDataDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
QSize ReportDataDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QString display_text;
display_text = index.data(Qt::DisplayRole).toString();
int width = QFontMetrics(option.font).width(display_text);
QSize size;
size.setWidth(width + 40);
size.setHeight(option.rect.height());
return size;
}
void ReportDataDelegate::commitAndCloseEditor()
{
ReportDataEditor *editor = qobject_cast<ReportDataEditor *>(sender());
emit commitData(editor);
emit closeEditor(editor, QAbstractItemDelegate::NoHint);
((QTableView *)this->parent())->setFocus();
}
Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)