Tree Model Completer Example▲
Similar to the Completer Example, we provide QComboBox objects to enable selection for completion mode and case sensitivity, as well as a QCheckBox for wrap completions.
The Resource File▲
The contents of the TreeModelCompleter is read from treemodel.txt. This file is embedded within the treemodelcompleter.qrc resource file, which contains the following:
&
lt;!
DOCTYPE RCC&
gt;&
lt;RCC version=
"1.0"
&
gt;
&
lt;qresource prefix=
"/"
&
gt;
&
lt;file&
gt;resources/
treemodel.txt&
lt;/
file&
gt;
&
lt;/
qresource&
gt;
&
lt;/
RCC&
gt;
TreeModelCompleter Class Definition▲
The TreeModelCompleter is a subclass of QCompleter with two constructors - one with parent as an argument and another with parent and model as arguments.
class
TreeModelCompleter : public
QCompleter
{
Q_OBJECT
Q_PROPERTY(QString separator READ separator WRITE setSeparator)
public
:
explicit
TreeModelCompleter(QObject *
parent =
nullptr
);
explicit
TreeModelCompleter(QAbstractItemModel *
model, QObject *
parent =
nullptr
);
QString separator() const
;
public
slots:
void
setSeparator(const
QString &
amp;separator);
protected
:
QStringList splitPath(const
QString &
amp;path) const
override
;
QString pathFromIndex(const
QModelIndex &
amp;index) const
override
;
private
:
QString sep;
}
;
The class reimplements the protected functions splitPath() and pathFromIndex() to suit a tree model. For more information on customizing QCompleter to suit tree models, refer to Handling Tree Models.
TreeModelCompleter also has a separator property which is declared using the Q_PROPERTY() macro. The separator has READ and WRITE attributes and the corresponding functions separator() and setSeparator(). For more information on Q_PROPERTY(), refer to Qt's Property System.
TreeModelCompleter Class Implementation▲
The first constructor constructs a TreeModelCompleter object with a parent while the second constructor constructs an object with a parent and a QAbstractItemModel, model.
TreeModelCompleter::
TreeModelCompleter(QObject *
parent)
:
QCompleter(parent)
{
}
TreeModelCompleter::
TreeModelCompleter(QAbstractItemModel *
model, QObject *
parent)
:
QCompleter(model, parent)
{
}
The separator() function is a getter function that returns the separator string.
QString TreeModelCompleter::
separator() const
{
return
sep;
}
As mentioned earlier, the splitPath() function is reimplemented because the default implementation is more suited to QFileSystemModel or list models. In order for QCompleter to split the path into a list of strings that are matched at each level, we split it using QString::split() with sep as its separator.
QStringList TreeModelCompleter::
splitPath(const
QString &
amp;path) const
{
return
(sep.isNull() ? QCompleter::
splitPath(path) : path.split(sep));
}
The pathFromIndex() function returns data for the completionRole() for a tree model. This function is reimplemented as its default implementation is more suitable for list models. If there is no separator, we use QCompleter's default implementation, otherwise we use the prepend() function to navigate upwards and accumulate the data. The function then returns a QStringList, dataList, using a separator to join objects of different levels.
QString TreeModelCompleter::
pathFromIndex(const
QModelIndex &
amp;index) const
{
if
(sep.isNull())
return
QCompleter::
pathFromIndex(index);
// navigate up and accumulate data
QStringList dataList;
for
(QModelIndex i =
index; i.isValid(); i =
i.parent())
dataList.prepend(model()-&
gt;data(i, completionRole()).toString());
return
dataList.join(sep);
}
MainWindow Class Definition▲
The MainWindow class is a subclass of QMainWindow and implements five custom slots: about(), changeCase(), changeMode(), highlight(), and updateContentsLabel().
class
MainWindow : public
QMainWindow
{
Q_OBJECT
public
:
MainWindow(QWidget *
parent =
nullptr
);
private
slots:
void
about();
void
changeCase(int
);
void
changeMode(int
);
void
highlight(const
QModelIndex &
amp;index);
void
updateContentsLabel(const
QString &
amp;sep);
In addition, the class has two private functions, createMenu() and modelFromFile(), as well as private instances of QTreeView, QComboBox, QLabel, TreeModelCompleter and QLineEdit.
private
:
void
createMenu();
QAbstractItemModel *
modelFromFile(const
QString &
amp;fileName);
QTreeView *
treeView =
nullptr
;
QComboBox *
caseCombo =
nullptr
;
QComboBox *
modeCombo =
nullptr
;
QLabel *
contentsLabel =
nullptr
;
TreeModelCompleter *
completer =
nullptr
;
QLineEdit *
lineEdit =
nullptr
;
}
;
MainWindow Class Implementation▲
The MainWindow's constructor creates a MainWindow object with a parent and initializes the completer and lineEdit. The createMenu() function is invoked to set up the "File" menu and "Help" menu. The completer's model is set to the QAbstractItemModel obtained from modelFromFile(), and the highlighted() signal is connected to MainWindow's highlight() slot.
MainWindow::
MainWindow(QWidget *
parent)
:
QMainWindow(parent)
{
createMenu();
completer =
new
TreeModelCompleter(this
);
completer-&
gt;setModel(modelFromFile(":/resources/treemodel.txt"
));
completer-&
gt;setSeparator(QLatin1String("."
));
QObject::
connect(completer, QOverload&
lt;const
QModelIndex &
amp;&
gt;::
of(&
amp;TreeModelCompleter::
highlighted),
this
, &
amp;MainWindow::
highlight);
QWidget *
centralWidget =
new
QWidget;
QLabel *
modelLabel =
new
QLabel;
modelLabel-&
gt;setText(tr("Tree Model<br>(Double click items to edit)"
));
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 QLabel objects modelLabel, modeLabel and caseLabel are instantiated. Also, the QComboBox objects, modeCombo and caseCombo, are instantiated and populated. By default, the completer's mode is "Filtered Popup" and the case is insensitive.
QLabel *
separatorLabel =
new
QLabel;
separatorLabel-&
gt;setText(tr("Tree Separator"
));
QLineEdit *
separatorLineEdit =
new
QLineEdit;
separatorLineEdit-&
gt;setText(completer-&
gt;separator());
connect(separatorLineEdit, &
amp;QLineEdit::
textChanged,
completer, &
amp;TreeModelCompleter::
setSeparator);
QCheckBox *
wrapCheckBox =
new
QCheckBox;
wrapCheckBox-&
gt;setText(tr("Wrap around completions"
));
wrapCheckBox-&
gt;setChecked(completer-&
gt;wrapAround());
connect(wrapCheckBox, &
amp;QAbstractButton::
clicked, completer, &
amp;QCompleter::
setWrapAround);
contentsLabel =
new
QLabel;
contentsLabel-&
gt;setSizePolicy(QSizePolicy::
Fixed, QSizePolicy::
Fixed);
connect(separatorLineEdit, &
amp;QLineEdit::
textChanged,
this
, &
amp;MainWindow::
updateContentsLabel);
treeView =
new
QTreeView;
treeView-&
gt;setModel(completer-&
gt;model());
treeView-&
gt;header()-&
gt;hide();
treeView-&
gt;expandAll();
connect(modeCombo, &
amp;QComboBox::
activated,
this
, &
amp;MainWindow::
changeMode);
connect(caseCombo, &
amp;QComboBox::
activated,
this
, &
amp;MainWindow::
changeMode);
lineEdit =
new
QLineEdit;
lineEdit-&
gt;setCompleter(completer);
We use a QGridLayout to place all the objects in the MainWindow.
QGridLayout *
layout =
new
QGridLayout;
layout-&
gt;addWidget(modelLabel, 0
, 0
); layout-&
gt;addWidget(treeView, 0
, 1
);
layout-&
gt;addWidget(modeLabel, 1
, 0
); layout-&
gt;addWidget(modeCombo, 1
, 1
);
layout-&
gt;addWidget(caseLabel, 2
, 0
); layout-&
gt;addWidget(caseCombo, 2
, 1
);
layout-&
gt;addWidget(separatorLabel, 3
, 0
); layout-&
gt;addWidget(separatorLineEdit, 3
, 1
);
layout-&
gt;addWidget(wrapCheckBox, 4
, 0
);
layout-&
gt;addWidget(contentsLabel, 5
, 0
, 1
, 2
);
layout-&
gt;addWidget(lineEdit, 6
, 0
, 1
, 2
);
centralWidget-&
gt;setLayout(layout);
setCentralWidget(centralWidget);
changeCase(caseCombo-&
gt;currentIndex());
changeMode(modeCombo-&
gt;currentIndex());
setWindowTitle(tr("Tree Model Completer"
));
lineEdit-&
gt;setFocus();
}
The createMenu() function sets up the QAction objects required and adds them to the "File" menu and "Help" menu. The triggered() signals from these actions are connected to their respective slots.
void
MainWindow::
createMenu()
{
QAction *
exitAction =
new
QAction(tr("Exit"
), this
);
QAction *
aboutAct =
new
QAction(tr("About"
), this
);
QAction *
aboutQtAct =
new
QAction(tr("About Qt"
), this
);
connect(exitAction, &
amp;QAction::
triggered, qApp, &
amp;QApplication::
quit);
connect(aboutAct, &
amp;QAction::
triggered, this
, &
amp;MainWindow::
about);
connect(aboutQtAct, &
amp;QAction::
triggered, qApp, &
amp;QApplication::
aboutQt);
QMenu *
fileMenu =
menuBar()-&
gt;addMenu(tr("File"
));
fileMenu-&
gt;addAction(exitAction);
QMenu *
helpMenu =
menuBar()-&
gt;addMenu(tr("About"
));
helpMenu-&
gt;addAction(aboutAct);
helpMenu-&
gt;addAction(aboutQtAct);
}
The changeMode() function accepts an index corresponding to the user's choice of completion mode and changes the completer's mode accordingly.
void
MainWindow::
changeMode(int
index)
{
QCompleter::
CompletionMode mode;
if
(index ==
0
)
mode =
QCompleter::
InlineCompletion;
else
if
(index ==
1
)
mode =
QCompleter::
PopupCompletion;
else
mode =
QCompleter::
UnfilteredPopupCompletion;
completer-&
gt;setCompletionMode(mode);
}
The about() function provides a brief description on the Tree Model Completer example.
void
MainWindow::
about()
{
QMessageBox::
about(this
, tr("About"
), tr("This example demonstrates how "
"to use a QCompleter with a custom tree model."
));
}
The changeCase() function alternates between Case Sensitive and Case Insensitive modes, depending on the value of cs.
void
MainWindow::
changeCase(int
cs)
{
completer-&
gt;setCaseSensitivity(cs ? Qt::
CaseSensitive : Qt::
CaseInsensitive);
}
main() Function▲
The main() function instantiates MainWindow and invokes the show() function to display it.
int
main(int
argc, char
*
argv[])
{
Q_INIT_RESOURCE(treemodelcompleter);
QApplication app(argc, argv);
MainWindow window;
window.show();
return
app.exec();
}