Search code examples
c++qtqcompleter

QCompleter runtime crash


I want to do a Code Completer so i subclassed QCompleter:

http://hastebin.com/qeyumevisa.cpp

But, when I try to run this code, i get a runtime error:

Runtime Error

And debug output shows:

ASSERT: "d->widget != 0" in file util\qcompleter.cpp, line 1446

Crash seems to come from the line 53:

QCompleter::complete(rect);

How i can fix this bug? Thanks


Solution

  • The assert is fired from QCompleter::complete(rect);

    It means that QCompleter::widget() is zero. The private member d->widget is always initialized by zero. Its type is QPointer<QWidget>. The widget is set only by QCompleter::setWidget(QWidget *widget). According to the documentation QCompleter::setWidget(QWidget *widget):

    Sets the widget for which completion are provided for to widget. This function is automatically called when a QCompleter is set on a QLineEdit using QLineEdit::setCompleter() or on a QComboBox using QComboBox::setCompleter(). The widget needs to be set explicitly when providing completions for custom widgets.

    Thus, the widget must be set either by QCompleter::setWidget() or by QLineEdit::setCompleter(). If non of those options is used the function QCompleter::complete(rect) crashes if the completion mode is not QCompleter::InlineCompletion.

    So, there are two possibilities for the crash:

    • d->widget is not initialized befor calling QCompleter::complete(rect);
    • since d->widget is a QPointer it can be automatically set to 0 when the referenced QWidget object is destroyed.