Results 1 to 8 of 8

Thread: Safe to share a QCompleter?

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Question Safe to share a QCompleter?

    Hi All,

    Is it safe to share a single QCompleter instance between several line edits on the same dialog? For example, is this safe:
    Qt Code:
    1. // Set up a completer for the acType
    2. QSqlQueryModel * typeModel = new QSqlQueryModel(this);
    3. typeModel->setQuery("select name from acType", m_model->database());
    4. QCompleter *typeCompleter = new QCompleter(typeModel, this);
    5. typeCompleter->setCaseSensitivity(Qt::CaseInsensitive);
    6. m_ui->acTypeEdit->setCompleter(typeCompleter);
    7. m_ui->acAltTypeEdit->setCompleter(typeCompleter);
    To copy to clipboard, switch view to plain text mode 
    Cheers,
    Chris

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Safe to share a QCompleter?

    Yes it is safe to share QCompleter. Since QDialog is the parent of the QCompleter object, it gets deleted only when the dialog gets deleted and not when one of the line edits.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Safe to share a QCompleter?

    Thanks.
    I was more wondering whether the activation of QCompleter on one line edit will adversely affect its operation on another. I guess it comes down to what, if any, state is kept between activations of the QCompleter class.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Safe to share a QCompleter?

    I wouldn't share completers among different widgets. You can share the model behind the completers though.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    ChrisW67 (5th May 2009)

  6. #5
    Join Date
    Mar 2012
    Location
    Bulgaria
    Posts
    5
    Thanks
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Safe to share a QCompleter?

    wysota, please specify the reason why you would not use one QCompleter for multiple widgets. I am using shared QCompleter now and it works but I want to know if there are some hidden dangers of doing so.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Safe to share a QCompleter?

    Quote Originally Posted by emobemo View Post
    wysota, please specify the reason why you would not use one QCompleter for multiple widgets. I am using shared QCompleter now and it works but I want to know if there are some hidden dangers of doing so.
    If you have more than one widget using the same completer then how are you going to share the completion prefix between them? If one widget contains text "abcd" and the other contains "klmnopq" then you have to ensure every focus change of the widget is going to reset the completion prefix.

    Consider the following code (Qt5 + C++11):

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. int main(int argc, char **argv) {
    4. QApplication app(argc, argv);
    5. QVBoxLayout *l = new QVBoxLayout(&w);
    6. QLineEdit *le1 = new QLineEdit;
    7. QLineEdit *le2 = new QLineEdit;
    8. l->addWidget(le1);
    9. l->addWidget(le2);
    10. QCompleter *cpl = new QCompleter(&w);
    11. le1->setCompleter(cpl);
    12. le2->setCompleter(cpl);
    13. QStringListModel model({"abcde", "abcdef", "abcdefg", "klmno", "klmnopq", "klmnopqr"});
    14. cpl->setModel(&model);
    15. QObject::connect(le1, SIGNAL(returnPressed()), cpl, SLOT(complete()));
    16. QObject::connect(le2, SIGNAL(returnPressed()), cpl, SLOT(complete()));
    17. le1->setText("abc");
    18. le2->setText("klm");
    19. w.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    See what happens if you request completion (by pressing return key) after switching focus to the other widget -- it shows completions for a wrong widget. In addition to that even if you do update the prefix as the focus changes, you'll get a performance hit as the model has to be refiltered each time. QCompleter is just a couple of bytes of additional memory used, why bother "saving" those several bytes if it can get you in trouble?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Mar 2012
    Location
    Bulgaria
    Posts
    5
    Thanks
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Safe to share a QCompleter?

    See what happens if you request completion (by pressing return key) after switching focus to the other widget -- it shows completions for a wrong widget. In addition to that even if you do update the prefix as the focus changes, you'll get a performance hit as the model has to be refiltered each time. QCompleter is just a couple of bytes of additional memory used, why bother "saving" those several bytes if it can get you in trouble?
    When you setText() on the QLineEdit the completion prefix of the completer does not change since it changes on keyPressEvent(), so even if you remove the second QLineEdit from the sample code the full list will popup. The other issue about the completer is that it not just keeps couple of bytes of additional memory. The QCompleter has its own internal proxy model to filter and sort data from the source model and if you have a lot of completers you will also have a lot of internal proxy models. However the documentation doesn't say that it should be used only on one widget at a time, but it slightly implies so with the setWidget() method, which in my opinion is redundant. I was wondering if there is some posibility of chrash or other dangerous behaviour when sharing QCompleter.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Safe to share a QCompleter?

    Quote Originally Posted by emobemo View Post
    When you setText() on the QLineEdit the completion prefix of the completer does not change since it changes on keyPressEvent(), so even if you remove the second QLineEdit from the sample code the full list will popup.
    That's not the issue I meant (the one you say is easily avoided by connecting textChanged signal to setCompletionPrefix slot). Focus on first line edit, press "d", focus on second line edit, press return. You'll get completions for the first widget while being focused on the second widget. This cannot be countered using signals and slots (you can do it using event filters though however this makes the objects more tightly coupled).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. What makes something not thread safe?
    By tgreaves in forum Newbie
    Replies: 9
    Last Post: 20th February 2009, 20:16
  2. QCompleter + QSqlTableModel problem
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2007, 20:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.