Completer Example▲

This example uses a custom item model, FileSystemModel, and a QCompleter object. QCompleter is a class that provides completions based on an item model. The type of model, the completion mode, and the case sensitivity can be selected using combo boxes.
The Resource File▲
The Completer example requires a resource file in order to store the countries.txt and words.txt. The resource file contains the following code:
&
lt;!
DOCTYPE RCC&
gt;&
lt;RCC version=
"1.0"
&
gt;
&
lt;qresource prefix=
"/"
&
gt;
&
lt;file&
gt;resources/
countries.txt&
lt;/
file&
gt;
&
lt;file&
gt;resources/
wordlist.txt&
lt;/
file&
gt;
&
lt;/
qresource&
gt;
&
lt;/
RCC&
gt;
FileSystemModel Class Definition▲
The FileSystemModel class is a subclass of QFileSystemModel, which provides a data model for the local filesystem.
class
FileSystemModel : public
QFileSystemModel
{
public
:
FileSystemModel(QObject *
parent =
0
);
QVariant data(const
QModelIndex &
amp;index, int
role =
Qt::
DisplayRole) const
override
;
}
;
This class only has a constructor and a data() function as it is only created to enable data() to return the entire file path for the display role, unlike QFileSystemModel's data() function that only returns the folder and not the drive label. This is further explained in FileSystemModel's implementation.
FileSystemModel Class Implementation▲
The constructor for the FileSystemModel class is used to pass parent to QFileSystemModel.
FileSystemModel::
FileSystemModel(QObject *
parent)
:
QFileSystemModel(parent)
{
}
As mentioned earlier, the data() function is reimplemented in order to get it to return the entire file parth for the display role. For example, with a QFileSystemModel, you will see "Program Files" in the view. However, with FileSystemModel, you will see "C:\Program Files".
QVariant FileSystemModel::
data(const
QModelIndex &
amp;index, int
role) const
{
if
(role ==
Qt::
DisplayRole &
amp;&
amp; index.column() ==
0
) {
QString path =
QDir::
toNativeSeparators(filePath(index));
if
(path.endsWith(QDir::
separator()))
path.chop(1
);
return
path;
}
return
QFileSystemModel::
data(index, role);
}
The screenshots below illustrate this difference:
The Qt::EditRole, which QCompleter uses to look for matches, is left unchanged.
MainWindow Class Definition▲
The MainWindow class is a subclass of QMainWindow and implements five private slots - about(), changeCase(), changeMode(), changeModel(), and changeMaxVisible().
class
MainWindow : public
QMainWindow
{
Q_OBJECT
public
:
MainWindow(QWidget *
parent =
0
);
private
slots:
void
about();
void
changeCase(int
);
void
changeMode(int
);
void
changeModel();
void
changeMaxVisible(int
);
Within the MainWindow class, we have two private functions: createMenu() and modelFromFile(). We also declare the private widgets needed - three QComboBox objects, a QCheckBox, a QCompleter, a QLabel, and a QLineEdit.
private
:
void
createMenu();
QAbstractItemModel *
modelFromFile(const
QString&
amp; fileName);
QComboBox *
caseCombo;
QComboBox *
modeCombo;
QComboBox *
modelCombo;
QSpinBox *
maxVisibleSpinBox;
QCheckBox *
wrapCheckBox;
QCompleter *
completer;
QLabel *
contentsLabel;
QLineEdit *
lineEdit;
}
;
MainWindow Class Implementation▲
The constructor of MainWindow constructs a MainWindow with a parent widget and initializes the private members. The createMenu() function is then invoked.
We set up three QComboBox objects, modelComb, modeCombo and caseCombo. By default, the modelCombo is set to QFileSystemModel, the modeCombo is set to "Filtered Popup" and the caseCombo is set to "Case Insensitive".
MainWindow::
MainWindow(QWidget *
parent)
:
QMainWindow(parent), completer(0
), lineEdit(0
)
{
createMenu();
QWidget *
centralWidget =
new
QWidget;
QLabel *
modelLabel =
new
QLabel;
modelLabel-&
gt;setText(tr("Model"
));
modelCombo =
new
QComboBox;
modelCombo-&
gt;addItem(tr("QFileSytemModel"
));
modelCombo-&
gt;addItem(tr("QFileSytemModel that shows full path"
));
modelCombo-&
gt;addItem(tr("Country list"
));
modelCombo-&
gt;addItem(tr("Word list"
));
modelCombo-&
gt;setCurrentIndex(0
);
QLabel *
modeLabel =
new
QLabel;
modeLabel-&
gt;setText(tr("Completion Mode"
));
modeCombo =
new
QComboBox;
modeCombo-&
gt;addItem(tr("Inline"
));
modeCombo-&
gt;addItem(tr("Filtered Popup"
));
modeCombo-&
gt;addItem(tr("Unfiltered Popup"
));
modeCombo-&
gt;setCurrentIndex(1
);
QLabel *
caseLabel =
new
QLabel;
caseLabel-&
gt;setText(tr("Case Sensitivity"
));
caseCombo =
new
QComboBox;
caseCombo-&
gt;addItem(tr("Case Insensitive"
));
caseCombo-&
gt;addItem(tr("Case Sensitive"
));
caseCombo-&
gt;setCurrentIndex(0
);
The maxVisibleSpinBox is created and determines the number of visible item in the completer
The wrapCheckBox is then set up. This <