Reputation: 4059
I have a QTableWidget
with custom QStyledItemDelegate
and when enter in cell editing, I want to popup a completer but it doesn't appear.
The setup of delegate:
tableWidget.setItemDelegate(new DelegateLineEdit());
My custom class:
class DelegateLineEdit : public QStyledItemDelegate
{
public:
DelegateLineEdit() {
signs << "<" << "<=" << ">" << ">=" << "=";
}
~DelegateLineEdit(){ }
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
QLineEdit *line_edit = new QLineEdit(parent);
line_edit->setStyle(parent->style());
line_edit->setFocusPolicy(Qt::StrongFocus);
QCompleter *completer = new QCompleter(signs, line_edit);
completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
line_edit->setCompleter(completer);
return line_edit;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const {
QStyledItemDelegate::setEditorData(editor, index);
QLineEdit *line_edit = dynamic_cast<QLineEdit*>(editor);
if (line_edit){
line_edit->completer()->complete();
}
}
private:
QStringList signs;
};
When I enter in cell editing by double click, nothing happen but if I comment the line
line_edit->completer()->complete()
, I can edit the cell but no completion is show. Somebody have an idea ?
Upvotes: 1
Views: 1785
Reputation: 10057
I would try using a QLineEdit
subclass as editor, where the focusInEvent
is overridden to show the popup:
class LineEdit : public QLineEdit
{
public:
explicit LineEdit(QWidget*parent) : QLineEdit(parent){}
protected:
void focusInEvent(QFocusEvent * e)
{
QLineEdit::focusInEvent(e);
completer()->complete();
}
};
The delegate becomes:
class DelegateLineEdit : public QStyledItemDelegate
{
public:
DelegateLineEdit() {
signs << "<" << "<=" << ">" << ">=" << "=";
}
~DelegateLineEdit(){ }
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
//use the subclass instead of QLineEdit:
LineEdit *line_edit = new LineEdit(parent);
line_edit->setStyle(parent->style());
line_edit->setFocusPolicy(Qt::StrongFocus);
QCompleter *completer = new QCompleter(signs, line_edit);
completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
line_edit->setCompleter(completer);
return line_edit;
}
private:
QStringList signs;
};
Upvotes: 1
Reputation: 1
I think something like completer->popup()->show();
should do what you want or try to call like
QCompleter::setCompletionPrefix(index.data(Qt::EditRole).tostring());
and
QCompleter::complete();
Upvotes: 0