qt 6.5.1 original

This commit is contained in:
kleuter
2023-10-29 23:33:08 +01:00
parent 71d22ab6b0
commit 85d238dfda
21202 changed files with 5499099 additions and 0 deletions

View File

@ -0,0 +1,50 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(customcompleter LANGUAGES CXX)
if(NOT DEFINED INSTALL_EXAMPLESDIR)
set(INSTALL_EXAMPLESDIR "examples")
endif()
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tools/customcompleter")
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
qt_standard_project_setup()
qt_add_executable(customcompleter
main.cpp
mainwindow.cpp mainwindow.h
textedit.cpp textedit.h
)
set_target_properties(customcompleter PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
target_link_libraries(customcompleter PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Widgets
)
# Resources:
set(customcompleter_resource_files
"resources/wordlist.txt"
)
qt_add_resources(customcompleter "customcompleter"
PREFIX
"/"
FILES
${customcompleter_resource_files}
)
install(TARGETS customcompleter
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)

View File

@ -0,0 +1,13 @@
QT += widgets
requires(qtConfig(completer))
HEADERS = mainwindow.h \
textedit.h
SOURCES = main.cpp \
mainwindow.cpp \
textedit.cpp
RESOURCES = customcompleter.qrc
# install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/tools/customcompleter
INSTALLS += target

View File

@ -0,0 +1,5 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
<file>resources/wordlist.txt</file>
</qresource>
</RCC>

View File

@ -0,0 +1,165 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!
\example tools/customcompleter
\title Custom Completer Example
\ingroup examples-widgets-tools
\brief The Custom Completer example shows how to provide string-completion
facilities for an input widget based on data provided by a model. The
completer pops up suggestions for possible words based on the first three
characters input by the user and the user's choice of word is inserted
into the \c TextEdit using QTextCursor.
\image customcompleter-example.png
\section1 Setting Up The Resource File
The Custom Completer example requires a resource file, \e wordlist.txt,
that has a list of words to help QCompleter complete words. This file
contains the following:
\quotefile tools/customcompleter/customcompleter.qrc
\section1 TextEdit Class Definition
The \c TextEdit class is a subclass of QTextEdit with a custom
\c insertCompletion() slot and it reimplements the
\l{QAbstractScrollArea::keyPressEvent()}{keyPressEvent()} and the
\l{QWidget::focusInEvent()}{focusInEvent()} functions. \c TextEdit also
contains a private function \c textUnderCursor() and a private instance
of QCompleter, \c c.
\snippet tools/customcompleter/textedit.h 0
\section1 TextEdit Class Implementation
The constructor for \c TextEdit constructs a \c TextEdit with a parent and
initializes \c c. The instructions to use the completer is displayed on
the \c TextEdit object, using the
\l{QTextEdit::setPlainText()}{setPlainText()} function.
\snippet tools/customcompleter/textedit.cpp 0
In addition, \c TextEdit also includes a default destructor:
\snippet tools/customcompleter/textedit.cpp 1
The \c setCompleter() function accepts a \a completer and sets it up.
We use \c{if (c)} to check if \c c has been initialized. If it has been
initialized, the QObject::disconnect() function is invoked to disconnect
the signal from the slot. This is to ensure that no previous completer
object is still connected to the slot.
\snippet tools/customcompleter/textedit.cpp 2
We then instantiate \c c with \a completer and set it as \c{TextEdit}'s
widget. The completion mode and case sensitivity are also set and then
we connect the \l{QCompleter::activated()}{activated()} signal to the
\c insertCompletion() slot.
The \c completer() function is a getter function that returns \c c.
\snippet tools/customcompleter/textedit.cpp 3
The completer pops up the options available, based on the contents of
\e wordlist.txt, but the text cursor is responsible for filling in the
missing characters, according to the user's choice of word.
Suppose the user inputs "ACT" and accepts the completer's suggestion of
"ACTUAL". The \c completion string is then sent to \c insertCompletion()
by the completer's \l{QCompleter::activated()}{activated()} signal.
The \c insertCompletion() function is responsible for completing the word
using a QTextCursor object, \c tc. It validates to ensure that the
completer's widget is \c TextEdit before using \c tc to insert the extra
characters to complete the word.
\snippet tools/customcompleter/textedit.cpp 4
The figure below illustrates this process:
\image customcompleter-insertcompletion.png
\c{completion.length()} = 6
\c{c->completionPrefix().length()}=3
The difference between these two values is \c extra, which is 3. This
means that the last three characters from the right, "U", "A", and "L",
will be inserted by \c tc.
The \c textUnderCursor() function uses a QTextCursor, \c tc, to select a
word under the cursor and return it.
\snippet tools/customcompleter/textedit.cpp 5
The \c TextEdit class reimplements \l{QWidget::focusInEvent()}
{focusInEvent()} function, which is an event handler used to receive
keyboard focus events for the widget.
\snippet tools/customcompleter/textedit.cpp 6
The \l{QAbstractScrollArea::keyPressEvent()}{keyPressEvent()} is
reimplemented to ignore key events like Qt::Key_Enter, Qt::Key_Return,
Qt::Key_Escape, Qt::Key_Tab, and Qt::Key_Backtab so the completer can
handle them.
If there is an active completer, we cannot process the shortcut, Ctrl+E.
\snippet tools/customcompleter/textedit.cpp 7
We also handle other modifiers and shortcuts for which we do not want the
completer to respond to.
\snippet tools/customcompleter/textedit.cpp 8
Finally, we pop up the completer.
\section1 MainWindow Class Definition
The \c MainWindow class is a subclass of QMainWindow and implements a
private slot, \c about(). This class also has two private functions,
\c createMenu() and \c modelFromFile() as well as private instances of
QCompleter and \c TextEdit.
\snippet tools/customcompleter/mainwindow.h 0
\section1 MainWindow Class Implementation
The constructor constructs a \c MainWindow with a parent and initializes
the \c completer. It also instantiates a \c TextEdit and sets its
completer. A QStringListModel, obtained from \c modelFromFile(), is used
to populate the \c completer. The \c{MainWindow}'s central widget is set
to \c TextEdit and its size is set to 500 x 300.
\snippet tools/customcompleter/mainwindow.cpp 0
The \c createMenu() function creates the necessary QAction objects needed
for the "File" and "Help" menu and their \l{QAction::triggered()}
{triggered()} signals are connected to the \c quit(), \c about(), and
\c aboutQt() slots respectively.
\snippet tools/customcompleter/mainwindow.cpp 1
The \c modelFromFile() function accepts a \a fileName and attempts to
extract the contents of this file into a QStringListModel. We display the
Qt::WaitCursor when we are populating the QStringList, \c words, and
restore the mouse cursor when we are done.
\snippet tools/customcompleter/mainwindow.cpp 2
The \c about() function provides a brief description about the Custom
Completer example.
\snippet tools/customcompleter/mainwindow.cpp 3
\section1 \c main() Function
The \c main() function instantiates \c MainWindow and invokes the
\l{QWidget::show()}{show()} function.
\snippet tools/customcompleter/main.cpp 0
*/

View File

@ -0,0 +1,17 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QApplication>
#include "mainwindow.h"
//! [0]
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(customcompleter);
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
//! [0]

View File

@ -0,0 +1,87 @@
// 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]

View File

@ -0,0 +1,35 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
class QAbstractItemModel;
class QCompleter;
QT_END_NAMESPACE
class TextEdit;
//! [0]
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
private slots:
void about();
private:
void createMenu();
QAbstractItemModel *modelFromFile(const QString& fileName);
QCompleter *completer = nullptr;
TextEdit *completingTextEdit;
};
//! [0]
#endif // MAINWINDOW_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,137 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "textedit.h"
#include <QCompleter>
#include <QKeyEvent>
#include <QAbstractItemView>
#include <QtDebug>
#include <QApplication>
#include <QModelIndex>
#include <QAbstractItemModel>
#include <QScrollBar>
//! [0]
TextEdit::TextEdit(QWidget *parent)
: QTextEdit(parent)
{
setPlainText(tr("This TextEdit provides autocompletions for words that have more than"
" 3 characters. You can trigger autocompletion using ") +
QKeySequence("Ctrl+E").toString(QKeySequence::NativeText));
}
//! [0]
//! [1]
TextEdit::~TextEdit()
{
}
//! [1]
//! [2]
void TextEdit::setCompleter(QCompleter *completer)
{
if (c)
c->disconnect(this);
c = completer;
if (!c)
return;
c->setWidget(this);
c->setCompletionMode(QCompleter::PopupCompletion);
c->setCaseSensitivity(Qt::CaseInsensitive);
QObject::connect(c, QOverload<const QString &>::of(&QCompleter::activated),
this, &TextEdit::insertCompletion);
}
//! [2]
//! [3]
QCompleter *TextEdit::completer() const
{
return c;
}
//! [3]
//! [4]
void TextEdit::insertCompletion(const QString &completion)
{
if (c->widget() != this)
return;
QTextCursor tc = textCursor();
int extra = completion.length() - c->completionPrefix().length();
tc.movePosition(QTextCursor::Left);
tc.movePosition(QTextCursor::EndOfWord);
tc.insertText(completion.right(extra));
setTextCursor(tc);
}
//! [4]
//! [5]
QString TextEdit::textUnderCursor() const
{
QTextCursor tc = textCursor();
tc.select(QTextCursor::WordUnderCursor);
return tc.selectedText();
}
//! [5]
//! [6]
void TextEdit::focusInEvent(QFocusEvent *e)
{
if (c)
c->setWidget(this);
QTextEdit::focusInEvent(e);
}
//! [6]
//! [7]
void TextEdit::keyPressEvent(QKeyEvent *e)
{
if (c && c->popup()->isVisible()) {
// The following keys are forwarded by the completer to the widget
switch (e->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
e->ignore();
return; // let the completer do default behavior
default:
break;
}
}
const bool isShortcut = (e->modifiers().testFlag(Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
if (!c || !isShortcut) // do not process the shortcut when we have a completer
QTextEdit::keyPressEvent(e);
//! [7]
//! [8]
const bool ctrlOrShift = e->modifiers().testFlag(Qt::ControlModifier) ||
e->modifiers().testFlag(Qt::ShiftModifier);
if (!c || (ctrlOrShift && e->text().isEmpty()))
return;
static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
const bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor();
if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
|| eow.contains(e->text().right(1)))) {
c->popup()->hide();
return;
}
if (completionPrefix != c->completionPrefix()) {
c->setCompletionPrefix(completionPrefix);
c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
}
QRect cr = cursorRect();
cr.setWidth(c->popup()->sizeHintForColumn(0)
+ c->popup()->verticalScrollBar()->sizeHint().width());
c->complete(cr); // popup it up!
}
//! [8]

View File

@ -0,0 +1,41 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef TEXTEDIT_H
#define TEXTEDIT_H
#include <QTextEdit>
QT_BEGIN_NAMESPACE
class QCompleter;
QT_END_NAMESPACE
//! [0]
class TextEdit : public QTextEdit
{
Q_OBJECT
public:
TextEdit(QWidget *parent = nullptr);
~TextEdit();
void setCompleter(QCompleter *c);
QCompleter *completer() const;
protected:
void keyPressEvent(QKeyEvent *e) override;
void focusInEvent(QFocusEvent *e) override;
private slots:
void insertCompletion(const QString &completion);
private:
QString textUnderCursor() const;
private:
QCompleter *c = nullptr;
};
//! [0]
#endif // TEXTEDIT_H