Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:40:37+00:00 2026-06-16T00:40:37+00:00

I am currently working on a code editor written in Qt, I have managed

  • 0

I am currently working on a code editor written in Qt,

I have managed to implement most of the features which I desire, i.e. auto completion and syntax highlighting but there is one problem which I can’t figure out.

I have created a model for which the QCompleter uses, which is fine for things like html tags and c++ keywords such as if else etc.

But I would like to add variables to the completer as they are entered by the user.

So I created an event on the QTextEdit which will get the word (I know I need to check to make sure that it is a variable etc but I just want to get it working for now).

void TextEdit::checkWord()
{
    //going to get the previous word and try to do something with it
    QTextCursor tc = textCursor();
    tc.movePosition(QTextCursor::PreviousWord);
    tc.select(QTextCursor::WordUnderCursor);
    QString word = tc.selectedText();
    //check to see it is in the model
}

But now I want to work out how to check to see if that word is already in the QCompleters model and if it isn’t how do I add it?

I have tried the following:

QAbstractItemModel *m = completer->model();
//dont know what to do with it now :(
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:40:37+00:00Added an answer on June 16, 2026 at 12:40 am

    You can check if word is in your QCompleter really by using

    QAbstractItemModel *m = completer->model();
    

    as you can see, method model() returns const pointer.

    That is good for checking procedure, you can check like this:

    bool matched = false;
    QString etalon("second");
    QStringListModel *strModel = qobject_cast<QStringListModel*>(completer.model());
    if (strModel!=NULL)
        foreach (QString str, strModel->stringList()) {
            if (str == etalon)
            {
                matched = true;
                break;
            }
        }
    qDebug()<<matched;
    

    But for your purposes, I recommend you to declare QStringListModel, and connect it to your completer, and then, all of operations you’ll must do thru your model, according to Qt’s principles of MVC programming (http://doc.qt.digia.com/qt/model-view-programming.html).

    Your code can be like this:

    // declaration
    QCompleter completer;
    QStringListModel completerModel;
    
    // initialization
    completer.setModel(&completerModel);
    QStringList stringListForCompleter;
    stringListForCompleter << "first" << "second" << "third";
    completerModel.setStringList(stringListForCompleter);
    
    // adding new word to your completer list
    completerModel.setStringList(completerModel.stringList() << "New Word");
    

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a project where I have code that looks like
I am currently working on Problem 62 I have tried the following code to
I'm working on code for a Java project, I currently have replaceAll(String, int) in
I'm working on personal project which is basically a code editor. Imagine standard File
I am currently working on a multi-line Edit Text which might have placeholders within
The code I'm currently working on requires adding an NSNumber object to an array.
I am currently working on some Ajax heavy code and I am wondering how
I am currently working with the following pseudo code block to fix a regex
Here's the code I'm currently working with: http://jsfiddle.net/gEMCm/ I'm trying to make an image
I'm currently working on a compiler for a language (an external byte-code), and am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.