Completer Example

Image non disponible

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:

 
Sélectionnez
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
   <file>resources/countries.txt</file>
   <file>resources/wordlist.txt</file>
</qresource>
</RCC>

FileSystemModel Class Definition

The FileSystemModel class is a subclass of QFileSystemModel, which provides a data model for the local filesystem.

 
Sélectionnez
class FileSystemModel : public QFileSystemModel
{
public:
    FileSystemModel(QObject *parent = 0);
    QVariant data(const QModelIndex &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.

 
Sélectionnez
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".

 
Sélectionnez
QVariant FileSystemModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole && 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:

Image non disponible

Image non disponible

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().

 
Sélectionnez
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.

 
Sélectionnez
private:
    void createMenu();
    QAbstractItemModel *modelFromFile(const QString& 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".

 
Sélectionnez
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), completer(0), lineEdit(0)
{
    createMenu();

    QWidget *centralWidget = new QWidget;

    QLabel *modelLabel = new QLabel;
    modelLabel->setText(tr("Model"));

    modelCombo = new QComboBox;
    modelCombo->addItem(tr("QFileSytemModel"));
    modelCombo->addItem(tr("QFileSytemModel that shows full path"));
    modelCombo->addItem(tr("Country list"));
    modelCombo->addItem(tr("Word list"));
    modelCombo->setCurrentIndex(0);

    QLabel *modeLabel = new QLabel;
    modeLabel->setText(tr("Completion Mode"));
    modeCombo = new QComboBox;
    modeCombo->addItem(tr("Inline"));
    modeCombo->addItem(tr("Filtered Popup"));
    modeCombo->addItem(tr("Unfiltered Popup"));
    modeCombo->setCurrentIndex(1);

    QLabel *caseLabel = new QLabel;
    caseLabel->setText(tr("Case Sensitivity"));
    caseCombo = new QComboBox;
    caseCombo->addItem(tr("Case Insensitive"));
    caseCombo->addItem(tr("Case Sensitive"));
    caseCombo->setCurrentIndex(0);

The maxVisibleSpinBox is created and determines the number of visible item in the completer

The wrapCheckBox is then set up. This <