mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2024-11-23 12:07:03 +08:00
88 lines
2.3 KiB
C++
88 lines
2.3 KiB
C++
// Copyright (C) 2016 The Qt Company Ltd.
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
#include "mainwindow.h"
|
|
#include "textedit.h"
|
|
|
|
#include <QAction>
|
|
#include <QApplication>
|
|
#include <QCompleter>
|
|
#include <QFile>
|
|
#include <QMenuBar>
|
|
#include <QMessageBox>
|
|
#include <QStringListModel>
|
|
|
|
//! [0]
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
{
|
|
createMenu();
|
|
|
|
completingTextEdit = new TextEdit;
|
|
completer = new QCompleter(this);
|
|
completer->setModel(modelFromFile(":/resources/wordlist.txt"));
|
|
completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
|
|
completer->setCaseSensitivity(Qt::CaseInsensitive);
|
|
completer->setWrapAround(false);
|
|
completingTextEdit->setCompleter(completer);
|
|
|
|
setCentralWidget(completingTextEdit);
|
|
resize(500, 300);
|
|
setWindowTitle(tr("Completer"));
|
|
}
|
|
//! [0]
|
|
|
|
//! [1]
|
|
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, &QAction::triggered, qApp, &QApplication::quit);
|
|
connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
|
|
connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
|
|
|
|
QMenu *fileMenu = menuBar()->addMenu(tr("File"));
|
|
fileMenu->addAction(exitAction);
|
|
|
|
QMenu *helpMenu = menuBar()->addMenu(tr("About"));
|
|
helpMenu->addAction(aboutAct);
|
|
helpMenu->addAction(aboutQtAct);
|
|
}
|
|
//! [1]
|
|
|
|
//! [2]
|
|
QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName)
|
|
{
|
|
QFile file(fileName);
|
|
if (!file.open(QFile::ReadOnly))
|
|
return new QStringListModel(completer);
|
|
|
|
#ifndef QT_NO_CURSOR
|
|
QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
|
#endif
|
|
QStringList words;
|
|
|
|
while (!file.atEnd()) {
|
|
QByteArray line = file.readLine();
|
|
if (!line.isEmpty())
|
|
words << QString::fromUtf8(line.trimmed());
|
|
}
|
|
|
|
#ifndef QT_NO_CURSOR
|
|
QGuiApplication::restoreOverrideCursor();
|
|
#endif
|
|
return new QStringListModel(words, completer);
|
|
}
|
|
//! [2]
|
|
|
|
//! [3]
|
|
void MainWindow::about()
|
|
{
|
|
QMessageBox::about(this, tr("About"), tr("This example demonstrates the "
|
|
"different features of the QCompleter class."));
|
|
}
|
|
//! [3]
|
|
|