The default behaviour of QCompleter on QLineEdit set using function QLineEdit::setCompleter() is to take all of the string in QLineEdit::text. So if I want a behaviour where in a QTreeModelCompleter parses parts of QLineEdit::text() [ex: A.B.C && C.D.E] it is not possible with the default behaviour. As A.B.C will work but after it all of the text is given to my Completer and it doesn't work.
To avoid it I decided to have my own LineEdit inheriting from QLineEdit with this format:
Qt Code:
  1. class LineEdit : public QLineEdit
  2. {
  3. Q_OBJECT
  4. public:
  5. explicit LineEdit(QWidget *parent = 0);
  6. protected:
  7. signals:
  8. private slots:
  9. void insertCompletion(QString);
  10. void textEditedSlot(const QString &);
  11. private:
  12. QCompleter* completer;
  13. QString textUnderCursor();
  14. };
  15.  
  16. LineEdit::LineEdit(QWidget *parent) : QLineEdit(parent)
  17. {
  18. completer= new TreeModelCompleter(this);//TreeModelCompleter is my implementation of QCompleter, works ok with qtextedit
  19. completer->setWidget(this);
  20. QObject::connect(completer, SIGNAL(activated(QString)), this, SLOT(insertCompletion(QString)));
  21. QObject::connect(this, SIGNAL(textEdited(QString)), this, SLOT(textEditedSlot(QString)));
  22. }
  23.  
  24. void LineEdit::insertCompletion(QString completion)
  25. {
  26. bool done = false;
  27. QString rep;
  28. int initPos = cursorPosition();
  29. int pos = initPos;
  30. QString t = text();
  31. while(!done){
  32. bool moved=true;
  33. if(pos == 0) moved = false;
  34. else { --pos; rep = t.at(pos) + rep; }
  35. if(!moved) done = true;
  36. else if(rep.contains(QRegExp("[\\s+-&!,|<>?/ \\(\\) \\{\\} ]")))
  37. {
  38. if(!done) rep.remove(0, 1);
  39. done = true;
  40. ++pos;
  41. }
  42. }
  43. setText(t.replace(pos, initPos-pos+1, completion));
  44. }
  45.  
  46. void LineEdit::textEditedSlot(const QString &text)
  47. {
  48.  
  49. QString completionPrefix = textUnderCursor();
  50. if (completionPrefix != completer->completionPrefix()) {
  51. completer->setCompletionPrefix(completionPrefix);
  52. completer->popup()->setCurrentIndex(completer->popup()->model()->index(0,0));
  53. }
  54. QRect cr = cursorRect();
  55. cr.setWidth(completer->popup()->sizeHintForColumn(0)
  56. + completer->popup()->verticalScrollBar()->sizeHint().width());
  57. completer->complete(cr); // popup it up!
  58. }
  59.  
  60. QString LineEdit::textUnderCursor()
  61. {
  62. bool done = false;
  63. QString rep;
  64. int initPos = cursorPosition();
  65. int pos = initPos;
  66. QString t = text();
  67. while(!done){
  68. bool moved=true;
  69. if(pos == 0) moved = false;
  70. else {--pos;rep = t.at(pos) + rep; }
  71. if(!moved) done = true;
  72. else if(rep.contains(QRegExp("[\\s+-&!,|<>?/ \\(\\) \\{\\} ]")))
  73. {
  74. if(!done) rep.remove(0, 1);
  75. done = true;
  76. ++pos;
  77. }
  78. }
  79.  
  80. if(!thisString.isEmpty() && rep.contains(QString("this"))){
  81. rep = rep.replace(QString("this"), thisString);
  82. }
  83. return rep;
  84. }
To copy to clipboard, switch view to plain text mode 

Now the Problem is after typing in say A no event is taken in by LineEdit. I tried checking with implementing event functions of LineEdit and MyCompleter. So a QKeyEvent of '.' comes to LineEdit but no editchanged signal arrives! LineEdit is rejecting QKeyEvents of '.' For if LineEdit is receiving the event of '.' it should result in text() being changed, leading to fire of textEdited() signal. But is not happening. I don't get it.