Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Created January 25, 2013 11:45
Show Gist options
  • Select an option

  • Save lamprosg/4633819 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/4633819 to your computer and use it in GitHub Desktop.
Qt - QCompleter (auto completions)
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog (QWidget *parent):
QDialog(parent),
ui(new UI::Dialog)
{
ui->setupUI(this);
/***Set the autocompletion*****/
//Using a string list
QStringList CompletionList; //Create a string list
CompletionList << "Bryan" << "Bart" << "Brad" << "Beth"; //Add members to the string list
StringCompleter = new QCompleter(CompletionList, this); //Initializing QCompleter with a reference to the CompletionList
StringCompleter->setCaseSensitivity(Qt::CaseInsensitive); //Don't care for case sensitivity
ui->lineEdit->setCompleter(StringCompleter);
/*-------------------------------------------------------*/
//Using a Model
ModelCompleter = new QCompleter(this);
ModelCompleter->setModel(new QDirModel(ModelCompleter));
ui->lineEdit_2->setCompleter(ModelCompleter);
/***Autocompletion is set******/
}
Dialog::~Dialog()
{
delete ui;
}
//Dialog based app with Base class QDialog
#include <QDialog>
#include <QCompleter> //include the header file
#include <QDirModel> //For directory autocompetions
namespace UI {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
explicit Dialog (QWidget *parent=0);
~Dialog();
private:
UI::Dialog *ui;
QCompleter *StringCompleter; //Our 2 pointers to QCompleter objects
QCompleter *ModelCompleter;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment