Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Crash when trying to change QCompleter
Forum Updated to NodeBB v4.3 + New Features

Crash when trying to change QCompleter

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 1 Posters 996 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cheezus
    wrote on last edited by cheezus
    #1

    Here is the full source code. What I'm trying to do is create a QCompleter where I can update the list as the user types stuff in. Changing an external model does not seem to work, so I tried to change it by creating a new QCompleter each time. However, that results in a crash. If you run the code below and just type in "comp" it will crash.

    So, any ideas on:

    1. What is the proper way to update the completions for a QCompleter?
    2. Why this is crashing?
    #include <QLineEdit>
    #include <QApplication>
    #include <QCompleter>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QLineEdit edit;
    
        edit.connect(&edit,&QLineEdit::textChanged,
                     &edit,[&edit]() {
            auto list = QStringList() << "completion1" << "completion2" << "completion3";
            auto completer = new QCompleter(list,&edit);
            edit.setCompleter(completer);
        });
    
        edit.show();
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      cheezus
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • C Offline
        C Offline
        cheezus
        wrote on last edited by
        #3

        Here is the ultimate solution. After getting a QSqlQueryModel to work, I compared the code of QSqlQueryModel and QStringListModel and realized that the only difference was a call to begin/endInsertRows. And voila, it worked.

        For completless sake:

        #include <QLineEdit>
        #include <QApplication>
        #include <QCompleter>
        #include <QStringListModel>
        
        struct MyStringListModel : public QStringListModel {
        public:
          using QStringListModel::QStringListModel;
        
          void setStringList(QStringList const & list) {
            QStringListModel::setStringList(list);
            Q_EMIT beginInsertRows(QModelIndex(),0,list.size()-1);
            Q_EMIT endInsertRows();
          }
        };
        
        
        int main(int argc, char *argv[]) {
            QApplication app(argc, argv);
        
            QLineEdit edit;
            MyStringListModel model;
            QCompleter completer(&model,&edit);
            edit.setCompleter(&completer);
        
            edit.connect(&edit,&QLineEdit::textChanged,
                         &model,[&model]() {
                auto lst = model.stringList();
                lst << QString("completion-%1").arg(lst.size());
                model.setStringList(lst);
            });
        
            edit.show();
            return app.exec();
        }
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved