diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2019-10-14 17:46:16 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2019-10-14 17:46:34 +0200 |
commit | 440286655e0ca271506cf7cc02ad0dbf4baef9ca (patch) | |
tree | 896fa81adb8b14a69355a3a6cf64d06ec8173c9a /examples/widgets/tools/treemodelcompleter | |
parent | 1e27ad1697187549151657ba187928e439300db7 (diff) | |
parent | e164d61ca8263fc4b46fdd916e1ea77c7dd2b735 (diff) |
Merge remote-tracking branch 'origin/dev' into wip/cmake
Change-Id: I4a78428a8ea273b6960792e3b8043f816fa37fcf
Diffstat (limited to 'examples/widgets/tools/treemodelcompleter')
4 files changed, 47 insertions, 44 deletions
diff --git a/examples/widgets/tools/treemodelcompleter/mainwindow.cpp b/examples/widgets/tools/treemodelcompleter/mainwindow.cpp index dec3cb0496..302ccc436c 100644 --- a/examples/widgets/tools/treemodelcompleter/mainwindow.cpp +++ b/examples/widgets/tools/treemodelcompleter/mainwindow.cpp @@ -48,13 +48,28 @@ ** ****************************************************************************/ -#include <QtWidgets> -#include "treemodelcompleter.h" #include "mainwindow.h" +#include "treemodelcompleter.h" + +#include <QAbstractProxyModel> +#include <QAction> +#include <QApplication> +#include <QCheckBox> +#include <QComboBox> +#include <QFile> +#include <QGridLayout> +#include <QHeaderView> +#include <QLabel> +#include <QLineEdit> +#include <QMenuBar> +#include <QMessageBox> +#include <QStandardItemModel> +#include <QStringListModel> +#include <QTreeView> //! [0] MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), completer(0), lineEdit(0) + : QMainWindow(parent) { createMenu(); @@ -151,10 +166,10 @@ void MainWindow::createMenu() connect(aboutAct, &QAction::triggered, this, &MainWindow::about); connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt); - QMenu* fileMenu = menuBar()->addMenu(tr("File")); + QMenu *fileMenu = menuBar()->addMenu(tr("File")); fileMenu->addAction(exitAction); - QMenu* helpMenu = menuBar()->addMenu(tr("About")); + QMenu *helpMenu = menuBar()->addMenu(tr("About")); helpMenu->addAction(aboutAct); helpMenu->addAction(aboutQtAct); } @@ -175,7 +190,7 @@ void MainWindow::changeMode(int index) } //! [5] -QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) +QAbstractItemModel *MainWindow::modelFromFile(const QString &fileName) { QFile file(fileName); if (!file.open(QFile::ReadOnly)) @@ -184,39 +199,35 @@ QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) #ifndef QT_NO_CURSOR QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); #endif - QStringList words; QStandardItemModel *model = new QStandardItemModel(completer); QVector<QStandardItem *> parents(10); parents[0] = model->invisibleRootItem(); + QRegularExpression re("^\\s+"); while (!file.atEnd()) { - QString line = file.readLine(); - QString trimmedLine = line.trimmed(); - if (line.isEmpty() || trimmedLine.isEmpty()) + const QString line = QString::fromUtf8(file.readLine()).trimmed(); + const QString trimmedLine = line.trimmed(); + if (trimmedLine.isEmpty()) continue; - QRegularExpression re("^\\s+"); - QRegularExpressionMatch match = re.match(line); + const QRegularExpressionMatch match = re.match(line); int nonws = match.capturedStart(); int level = 0; if (nonws == -1) { level = 0; } else { - if (line.startsWith("\t")) { - level = match.capturedLength(); - } else { - level = match.capturedLength()/4; - } + const int capLen = match.capturedLength(); + level = line.startsWith(QLatin1Char('\t')) ? capLen / 4 : capLen; } - if (level+1 >= parents.size()) - parents.resize(parents.size()*2); + if (level + 1 >= parents.size()) + parents.resize(parents.size() * 2); QStandardItem *item = new QStandardItem; item->setText(trimmedLine); parents[level]->appendRow(item); - parents[level+1] = item; + parents[level + 1] = item; } #ifndef QT_NO_CURSOR @@ -252,7 +263,7 @@ void MainWindow::changeCase(int cs) } //! [7] -void MainWindow::updateContentsLabel(const QString& sep) +void MainWindow::updateContentsLabel(const QString &sep) { contentsLabel->setText(tr("Type path from model above with items at each level separated by a '%1'").arg(sep)); } diff --git a/examples/widgets/tools/treemodelcompleter/mainwindow.h b/examples/widgets/tools/treemodelcompleter/mainwindow.h index 2edcd5aab0..87f492c4ac 100644 --- a/examples/widgets/tools/treemodelcompleter/mainwindow.h +++ b/examples/widgets/tools/treemodelcompleter/mainwindow.h @@ -60,8 +60,6 @@ class QAbstractItemModel; class QComboBox; class QLabel; class QLineEdit; -class QProgressBar; -class QCheckBox; class QTreeView; QT_END_NAMESPACE @@ -71,27 +69,27 @@ class MainWindow : public QMainWindow Q_OBJECT public: - MainWindow(QWidget *parent = 0); + MainWindow(QWidget *parent = nullptr); private slots: void about(); void changeCase(int); void changeMode(int); - void highlight(const QModelIndex&); - void updateContentsLabel(const QString&); + void highlight(const QModelIndex &index); + void updateContentsLabel(const QString &sep); //! [0] //! [1] private: void createMenu(); - QAbstractItemModel *modelFromFile(const QString& fileName); + QAbstractItemModel *modelFromFile(const QString &fileName); - QTreeView *treeView; - QComboBox *caseCombo; - QComboBox *modeCombo; - QLabel *contentsLabel; - TreeModelCompleter *completer; - QLineEdit *lineEdit; + QTreeView *treeView = nullptr; + QComboBox *caseCombo = nullptr; + QComboBox *modeCombo = nullptr; + QLabel *contentsLabel = nullptr; + TreeModelCompleter *completer = nullptr; + QLineEdit *lineEdit = nullptr; }; //! [1] diff --git a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.cpp b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.cpp index cab0476e3c..8930c815d5 100644 --- a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.cpp +++ b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.cpp @@ -80,26 +80,20 @@ QString TreeModelCompleter::separator() const //! [3] QStringList TreeModelCompleter::splitPath(const QString &path) const { - if (sep.isNull()) { - return QCompleter::splitPath(path); - } - - return path.split(sep); + return (sep.isNull() ? QCompleter::splitPath(path) : path.split(sep)); } //! [3] //! [4] QString TreeModelCompleter::pathFromIndex(const QModelIndex &index) const { - if (sep.isNull()) { + if (sep.isNull()) return QCompleter::pathFromIndex(index); - } // navigate up and accumulate data QStringList dataList; - for (QModelIndex i = index; i.isValid(); i = i.parent()) { + for (QModelIndex i = index; i.isValid(); i = i.parent()) dataList.prepend(model()->data(i, completionRole()).toString()); - } return dataList.join(sep); } diff --git a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.h b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.h index 1d28fddee0..d7d1852cc7 100644 --- a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.h +++ b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.h @@ -60,8 +60,8 @@ class TreeModelCompleter : public QCompleter Q_PROPERTY(QString separator READ separator WRITE setSeparator) public: - explicit TreeModelCompleter(QObject *parent = 0); - explicit TreeModelCompleter(QAbstractItemModel *model, QObject *parent = 0); + explicit TreeModelCompleter(QObject *parent = nullptr); + explicit TreeModelCompleter(QAbstractItemModel *model, QObject *parent = nullptr); QString separator() const; public slots: |