How to make the QCompleter read from a QString?



  • Hi guys, I'm working on an app a little different from many of the ones that are spoken in this forum, I want to complete words from a string in a data structure .



  • error C2065: 'data structure' : undeclared identifier

    please define what you mean. maybe provide an example?



  • @VRonin
    what I mean is that the QCompleter read a QString and not a QTextEdit or QLineEdit



  • Can you provide an example of what you want to achieve?
    something like: "I type a string in a QLineEdit and I want QCompleter to complete using a list of strings I have in a database"



  • @VRonin
    That is not what I want to do. I'm writing a QTextEdit, but in a QString am keeping what am writing, without showing what is in the QTextEdit, then, I want to complete what is in the QString with a database in a file



  • with a database in a file

    If you mean a SQLite DB then just use a QSqlTableModel as QSqlQueryModel with setModel()

    want to complete what is in the QString

    call setCompletionPrefix() with the string you want to complete and just iterate through the completions



  • @VRonin

    It is not a database, it is a file with a long list of words

    If you mean a SQLite DB then just use a QSqlTableModel as QSqlQueryModel with setModel()

    Thanks



  • Then read all the words in a QStringList and use a QStringListModel with setModel()



  • @VRonin

    Then read all the words in a QStringList and use a QStringListModel with setModel()

    I know how to do that part. Thanks again.

    By using the setCompletionPrefix() funtion the QCompleter does not work. Can you show me how you do it



  • #include <QFile>
    #include <QApplication>
    #include <QDebug>
    #include <QCompleter>
    #include <QTextStream>
    #include <QStringList>
    #include <QStringListModel>
    
    void createBaseFile();
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc,argv);
        createBaseFile();
        QFile baseFile("baseFile.txt");
        QStringList baseList;
        if(baseFile.open(QIODevice::ReadOnly | QIODevice::Text)){
            QTextStream baseFileStream(&baseFile);
            QString line;
            while (baseFileStream.readLineInto(&line)) {
                baseList << line;
            }
        }
        QStringListModel baseListModel(baseList);
        QCompleter baseCompleter;
        baseCompleter.setModel(&baseListModel);
        baseCompleter.setCompletionPrefix("in"); // word to complete
        for (int i = 0; baseCompleter.setCurrentRow(i); i++)
            qDebug() << baseCompleter.currentCompletion() << " is match number " << i;
        return 0;
    }
    void createBaseFile(){ //generate a random words "database"
        QFile baseFile("baseFile.txt");
        if(baseFile.open(QIODevice::WriteOnly | QIODevice::Text)){
            QTextStream baseFileStream(&baseFile);
            baseFileStream
                    << "Lorem" << '\n'
                    << "ipsum" << '\n'
                    << "dolor" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "consectetur" << '\n'
                    << "adipiscing" << '\n'
                    << "elit" << '\n'
                    << "Ut" << '\n'
                    << "accumsan" << '\n'
                    << "suscipit" << '\n'
                    << "ipsum" << '\n'
                    << "blandit" << '\n'
                    << "consectetur" << '\n'
                    << "lectus" << '\n'
                    << "cursus" << '\n'
                    << "vitae" << '\n'
                    << "Donec" << '\n'
                    << "velit" << '\n'
                    << "odio" << '\n'
                    << "iaculis" << '\n'
                    << "a" << '\n'
                    << "dictum" << '\n'
                    << "id" << '\n'
                    << "tincidunt" << '\n'
                    << "et" << '\n'
                    << "sem" << '\n'
                    << "Morbi" << '\n'
                    << "metus" << '\n'
                    << "metus" << '\n'
                    << "fringilla" << '\n'
                    << "in" << '\n'
                    << "ante" << '\n'
                    << "in" << '\n'
                    << "aliquet" << '\n'
                    << "condimentum" << '\n'
                    << "erat" << '\n'
                    << "Cras" << '\n'
                    << "lectus" << '\n'
                    << "libero" << '\n'
                    << "consectetur" << '\n'
                    << "et" << '\n'
                    << "metus" << '\n'
                    << "vitae" << '\n'
                    << "maximus" << '\n'
                    << "efficitur" << '\n'
                    << "enim" << '\n'
                    << "Vivamus" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "pellentesque" << '\n'
                    << "nibh" << '\n'
                    << "Curabitur" << '\n'
                    << "dapibus" << '\n'
                    << "diam" << '\n'
                    << "laoreet" << '\n'
                    << "consequat" << '\n'
                    << "purus" << '\n'
                    << "at" << '\n'
                    << "finibus" << '\n'
                    << "enim" << '\n'
                    << "Orci" << '\n'
                    << "varius" << '\n'
                    << "natoque" << '\n'
                    << "penatibus" << '\n'
                    << "et" << '\n'
                    << "magnis" << '\n'
                    << "dis" << '\n'
                    << "parturient" << '\n'
                    << "montes" << '\n'
                    << "nascetur" << '\n'
                    << "ridiculus" << '\n'
                    << "mus" << '\n'
                    << "Quisque" << '\n'
                    << "suscipit" << '\n'
                    << "massa" << '\n'
                    << "at" << '\n'
                    << "lobortis" << '\n'
                    << "iaculis" << '\n'
                    << "dui" << '\n'
                    << "mi" << '\n'
                    << "dictum" << '\n'
                    << "lacus" << '\n'
                    << "eget" << '\n'
                    << "dictum" << '\n'
                    << "metus" << '\n'
                    << "nulla" << '\n'
                    << "id" << '\n'
                    << "nibh" << '\n'
                    << "Nullam" << '\n'
                    << "dapibus" << '\n'
                    << "lectus" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "augue" << '\n'
                    << "tempus" << '\n'
                    << "ultricies" << '\n'
                    << "Maecenas" << '\n'
                    << "eleifend" << '\n'
                    << "dolor" << '\n'
                    << "eget" << '\n'
                    << "porttitor" << '\n'
                    << "vehicula" << '\n'
                    << "Vivamus" << '\n'
                    << "iaculis" << '\n'
                    << "sodales" << '\n'
                    << "rutrum" << '\n'
                    << "Nulla" << '\n'
                    << "ultricies" << '\n'
                    << "ligula" << '\n'
                    << "tellus" << '\n'
                    << "id" << '\n'
                    << "tempus" << '\n'
                    << "odio" << '\n'
                    << "congue" << '\n'
                    << "ac" << '\n'
                    << "Maecenas" << '\n'
                    << "porttitor" << '\n'
                    << "finibus" << '\n'
                    << "libero" << '\n'
                    << "quis" << '\n'
                    << "facilisis" << '\n'
                    << "enim" << '\n'
                    << "scelerisque" << '\n'
                    << "at" << '\n'
                    << "Proin" << '\n'
                    << "et" << '\n'
                    << "nibh" << '\n'
                    << "dui" << '\n'
                    << "Vestibulum" << '\n'
                    << "non" << '\n'
                    << "consequat" << '\n'
                    << "odio" << '\n'
                    << "non" << '\n'
                    << "sodales" << '\n'
                    << "nibh" << '\n'
                    << "Phasellus" << '\n'
                    << "lobortis" << '\n'
                    << "ante" << '\n'
                    << "at" << '\n'
                    << "dignissim" << '\n'
                    << "interdum" << '\n'
                    << "Vestibulum" << '\n'
                    << "magna" << '\n'
                    << "mauris" << '\n'
                    << "aliquam" << '\n'
                    << "nec" << '\n'
                    << "erat" << '\n'
                    << "eget" << '\n'
                    << "lacinia" << '\n'
                    << "pretium" << '\n'
                    << "lacus" << '\n'
                    << "Donec" << '\n'
                    << "sollicitudin" << '\n'
                    << "maximus" << '\n'
                    << "ex" << '\n'
                    << "nec" << '\n'
                    << "finibus" << '\n'
                    << "turpis" << '\n'
                    << "dictum" << '\n'
                    << "et" << '\n'
                    << "Cras" << '\n'
                    << "bibendum" << '\n'
                    << "tellus" << '\n'
                    << "ac" << '\n'
                    << "suscipit" << '\n'
                    << "egestas" << '\n'
                    << "eros" << '\n'
                    << "metus" << '\n'
                    << "mattis" << '\n'
                    << "augue" << '\n'
                    << "vitae" << '\n'
                    << "sagittis" << '\n'
                    << "elit" << '\n'
                    << "augue" << '\n'
                    << "a" << '\n'
                    << "mi" << '\n'
                    << "Nullam" << '\n'
                    << "venenatis" << '\n'
                    << "commodo" << '\n'
                    << "aliquam" << '\n'
                    << "Duis" << '\n'
                    << "sed" << '\n'
                    << "mi" << '\n'
                    << "dignissim" << '\n'
                    << "maximus" << '\n'
                    << "ipsum" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "venenatis" << '\n'
                    << "diam" << '\n'
                    << "Praesent" << '\n'
                    << "ullamcorper" << '\n'
                    << "luctus" << '\n'
                    << "maximus" << '\n'
                    << "Nulla" << '\n'
                    << "ac" << '\n'
                    << "consectetur" << '\n'
                    << "sem" << '\n'
                    << "Sed" << '\n'
                    << "eu" << '\n'
                    << "dui" << '\n'
                    << "in" << '\n'
                    << "eros" << '\n'
                    << "mattis" << '\n'
                    << "aliquam" << '\n'
                    << "Suspendisse" << '\n'
                    << "potenti" << '\n'
                    << "Aliquam" << '\n'
                    << "sollicitudin" << '\n'
                    << "ipsum" << '\n'
                    << "at" << '\n'
                    << "dictum" << '\n'
                    << "iaculis" << '\n'
                    << "Integer" << '\n'
                    << "tempus" << '\n'
                    << "egestas" << '\n'
                    << "vulputate" << '\n'
                    << "Nam" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "dictum" << '\n'
                    << "ante" << '\n'
                    << "Mauris" << '\n'
                    << "quam" << '\n'
                    << "quam" << '\n'
                    << "vestibulum" << '\n'
                    << "a" << '\n'
                    << "lorem" << '\n'
                    << "ut" << '\n'
                    << "dignissim" << '\n'
                    << "pretium" << '\n'
                    << "est" << '\n'
                    << "Pellentesque" << '\n'
                    << "ut" << '\n'
                    << "enim" << '\n'
                    << "condimentum" << '\n'
                    << "tristique" << '\n'
                    << "nunc" << '\n'
                    << "ut" << '\n'
                    << "aliquet" << '\n'
                    << "turpis" << '\n'
                    << "Interdum" << '\n'
                    << "et" << '\n'
                    << "malesuada" << '\n'
                    << "fames" << '\n'
                    << "ac" << '\n'
                    << "ante" << '\n'
                    << "ipsum" << '\n'
                    << "primis" << '\n'
                    << "in" << '\n'
                    << "faucibus" << '\n'
                    << "Sed" << '\n'
                    << "venenatis" << '\n'
                    << "nibh" << '\n'
                    << "neque" << '\n'
                    << "nec" << '\n'
                    << "fringilla" << '\n'
                    << "lectus" << '\n'
                    << "molestie" << '\n'
                    << "at" << '\n'
                    << "Nullam" << '\n'
                    << "in" << '\n'
                    << "metus" << '\n'
                    << "ac" << '\n'
                    << "mi" << '\n'
                    << "volutpat" << '\n'
                    << "facilisis" << '\n'
                    << "eget" << '\n'
                    << "vel" << '\n'
                    << "felis" << '\n'
                    << "Aenean" << '\n'
                    << "eu" << '\n'
                    << "lorem" << '\n'
                    << "feugiat" << '\n'
                    << "aliquet" << '\n'
                    << "eros" << '\n'
                    << "vitae" << '\n'
                    << "sollicitudin" << '\n'
                    << "nulla" << '\n'
                    << "Donec" << '\n'
                    << "dolor" << '\n'
                    << "nibh" << '\n'
                    << "bibendum" << '\n'
                    << "in" << '\n'
                    << "pretium" << '\n'
                    << "et" << '\n'
                    << "lobortis" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "erat" << '\n'
                    << "Integer" << '\n'
                    << "accumsan" << '\n'
                    << "ut" << '\n'
                    << "nisi" << '\n'
                    << "eu" << '\n'
                    << "sagittis" << '\n'
                    << "Mauris" << '\n'
                    << "vitae" << '\n'
                    << "interdum" << '\n'
                    << "ligula" << '\n'
                    << "Nullam" << '\n'
                    << "quis" << '\n'
                    << "ultrices" << '\n'
                    << "arcu" << '\n'
                    << "a" << '\n'
                    << "rhoncus" << '\n'
                    << "orci" << '\n'
                    << "In" << '\n'
                    << "velit" << '\n'
                    << "nisl" << '\n'
                    << "scelerisque" << '\n'
                    << "nec" << '\n'
                    << "ultricies" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "pulvinar" << '\n'
                    << "ut" << '\n'
                    << "lorem" << '\n'
                    << "Suspendisse" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "varius" << '\n'
                    << "libero" << '\n'
                    << "vitae" << '\n'
                    << "rutrum" << '\n'
                    << "erat" << '\n'
                    << "Integer" << '\n'
                    << "lacus" << '\n'
                    << "lorem" << '\n'
                    << "vestibulum" << '\n'
                    << "quis" << '\n'
                    << "erat" << '\n'
                    << "quis" << '\n'
                    << "fermentum" << '\n'
                    << "pellentesque" << '\n'
                    << "enim" << '\n'
                    << "Nam" << '\n'
                    << "dolor" << '\n'
                    << "turpis" << '\n'
                    << "fringilla" << '\n'
                    << "ac" << '\n'
                    << "lectus" << '\n'
                    << "in" << '\n'
                    << "mollis" << '\n'
                    << "tempor" << '\n'
                    << "ipsum" << '\n'
                    << "Integer" << '\n'
                    << "in" << '\n'
                    << "justo" << '\n'
                    << "sit" << '\n'
                    << "amet" << '\n'
                    << "sapien" << '\n'
                    << "bibendum" << '\n'
                    << "convallis" << '\n'
                    << "Suspendisse" << '\n'
                    << "interdum" << '\n'
                    << "ante" << '\n'
                    << "eget" << '\n'
                    << "aliquet" << '\n'
                    << "congue" << '\n'
                    << "elit" << '\n'
                    << "nunc" << '\n'
                    << "tincidunt" << '\n'
                    << "tortor" << '\n'
                    << "at" << '\n'
                    << "consectetur" << '\n'
                    << "sem" << '\n'
                    << "magna" << '\n'
                    << "vel" << '\n'
                    << "ante" << '\n'
                    << "Vivamus" << '\n'
                    << "in" << '\n'
                    << "diam" << '\n'
                    << "lacus" << '\n'
                    << "Pellentesque" << '\n'
                    << "volutpat" << '\n'
                    << "lacinia" << '\n'
                    << "eros" << '\n'
                    << "et" << '\n'
                    << "convallis" << '\n'
                    << "Suspendisse" << '\n'
                    << "eget" << '\n'
                    << "accumsan" << '\n'
                    << "nisl" << '\n'
                    << "Fusce" << '\n'
                    << "viverra" << '\n'
                    << "nisi" << '\n'
                    << "elit" << '\n'
                    << "sed" << '\n'
                    << "venenatis" << '\n'
                    << "elit" << '\n'
                    << "vulputate" << '\n'
                    << "nec" << '\n'
                    << "Aenean" << '\n'
                    << "tincidunt" << '\n'
                    << "est" << '\n'
                    << "a" << '\n'
                    << "quam" << '\n'
                    << "vestibulum" << '\n'
                    << "ut" << '\n'
                    << "maximus" << '\n'
                    << "massa" << '\n'
                    << "tristique" << '\n'
                    << "Vestibulum" << '\n'
                    << "ligula" << '\n'
                    << "velit" << '\n'
                    << "lobortis" << '\n'
                    << "sed" << '\n'
                    << "elit" << '\n'
                    << "in" << '\n'
                    << "facilisis" << '\n'
                    << "blandit" << '\n'
                    << "justo" << '\n'
                    << "Nunc" << '\n'
                    << "eget" << '\n'
                    << "magna" << '\n'
                    << "mattis" << '\n'
                    << "consectetur" << '\n'
                    << "sem" << '\n'
                    << "id" << '\n'
                    << "ultrices" << '\n'
                    << "mauris" << '\n'
                    << "Nulla" << '\n'
                    << "porta" << '\n'
                    << "consectetur" << '\n'
                    << "mauris" << '\n'
                    << "Aliquam" << '\n'
                    << "non" << '\n'
                    << "commodo" << '\n'
                    << "metus" << '\n'
                    << "in" << '\n'
                    << "lacinia" << '\n'
                    << "urna" << '\n'
                    << "Proin" << '\n'
                    << "sapien" << '\n'
                    << "lorem" << '\n'
                    << "auctor" << '\n'
                    << "vitae" << '\n'
                    << "maximus" << '\n'
                    << "non" << '\n'
                    << "elementum" << '\n'
                    << "vitae" << '\n'
                    << "quam" << '\n'
                    << "Pellentesque" << '\n'
                    << "eget" << '\n'
                    << "felis" << '\n'
                    << "vehicula" << '\n'
                    << "imperdiet" << '\n'
                    << "dolor" << '\n'
                    << "quis" << '\n'
                    << "sodales" << '\n'
                    << "mi" << '\n'
                    << "Etiam" << '\n'
                    << "sodales" << '\n'
                    << "placerat" << '\n'
                    << "metus" << '\n'
                    << "ac" << '\n'
                    << "elementum" << '\n'
                    << "risus" << '\n'
                    << "finibus" << '\n'
                    << "egestas" << '\n'
                    << "In" << '\n'
                    << "vitae" << '\n'
                    << "lectus" << '\n'
                    << "ligula" << '\n'
                    << "Quisque" << '\n'
                    << "fringilla" << '\n'
                    << "porta" << '\n'
                    << "efficitur" << '\n'
                    << "Nullam" << '\n'
                    << "a" << '\n'
                    << "malesuada" << '\n'
                    << "diam" << '\n'
                    << "Praesent" << '\n'
                    << "viverra" << '\n'
                    << "porta" << '\n'
                    << "augue" << '\n'
                    << "sagittis" << '\n'
                    << "convallis" << '\n'
                    << "Praesent" << '\n'
                    << "laoreet" << '\n'
                    << "mauris" << '\n'
                    << "quis" << '\n'
                    << "gravida" << '\n'
                    << "fermentum" << '\n'
                    << "nisl" << '\n'
                    << "sem" << '\n'
                    << "molestie" << '\n'
                    << "lorem" << '\n'
                    << "vel" << '\n'
                    << "iaculis" << '\n'
                    << "magna" << '\n'
                    << "quam" << '\n'
                    << "in" << '\n'
                    << "nisl" << '\n'
                    << "Ut" << '\n'
                    << "pulvinar" << '\n'
                    << "erat" << '\n'
                    << "ut" << '\n'
                    << "cursus" << '\n'
                    << "sagittis" << '\n'
                    << "Nunc" << '\n'
                    << "at" << '\n'
                    << "dui" << '\n'
                    << "at" << '\n'
                    << "metus" << '\n'
                    << "rhoncus" << '\n'
                    << "ullamcorper" << '\n'
    
    
                ;
        }
    
    }
    
    


  • @VRonin
    Let's see, I use what you tell me about setCompletionPrefix (), but on any side it raises the popup.
    What I do is that I connect a signal that passes the prefix and a slot that look like this:

    void MineTextEdit::completeWord(QString cad)
    {
        if(cad.size() > 3)
            {
                this->setFocus();
                c->setCompletionPrefix(cad);
                QRect cr = cursorRect();
                cr.setWidth(c->popup()->sizeHintForColumn(0)
                            + c->popup()->verticalScrollBar()->sizeHint().width());
                c->complete(cr);
            }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.