如何在QTableWidget中进入单元格编辑时弹出QCompleter
How to popup QCompleter on entering cell editing in QTableWidget
我有一个带有自定义 QStyledItemDelegate
的 QTableWidget
,在单元格编辑中输入时,我想弹出一个完成器,但它没有出现。
委托设置:
tableWidget.setItemDelegate(new DelegateLineEdit());
我的习惯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;
};
当我双击进入单元格编辑时,没有任何反应,但如果我注释该行
line_edit->completer()->complete()
,我可以编辑单元格,但未显示完成。有人有想法吗?
我认为像completer->popup()->show();
这样的东西应该做你想做的或者试着像
那样调用
QCompleter::setCompletionPrefix(index.data(Qt::EditRole).tostring());
和
QCompleter::complete();
我会尝试使用 QLineEdit
子类作为编辑器,其中 focusInEvent
被覆盖以显示弹出窗口:
class LineEdit : public QLineEdit
{
public:
explicit LineEdit(QWidget*parent) : QLineEdit(parent){}
protected:
void focusInEvent(QFocusEvent * e)
{
QLineEdit::focusInEvent(e);
completer()->complete();
}
};
代表变成:
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;
};
我有一个带有自定义 QStyledItemDelegate
的 QTableWidget
,在单元格编辑中输入时,我想弹出一个完成器,但它没有出现。
委托设置:
tableWidget.setItemDelegate(new DelegateLineEdit());
我的习惯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;
};
当我双击进入单元格编辑时,没有任何反应,但如果我注释该行
line_edit->completer()->complete()
,我可以编辑单元格,但未显示完成。有人有想法吗?
我认为像completer->popup()->show();
这样的东西应该做你想做的或者试着像
QCompleter::setCompletionPrefix(index.data(Qt::EditRole).tostring());
和
QCompleter::complete();
我会尝试使用 QLineEdit
子类作为编辑器,其中 focusInEvent
被覆盖以显示弹出窗口:
class LineEdit : public QLineEdit
{
public:
explicit LineEdit(QWidget*parent) : QLineEdit(parent){}
protected:
void focusInEvent(QFocusEvent * e)
{
QLineEdit::focusInEvent(e);
completer()->complete();
}
};
代表变成:
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;
};