diff options
author | Kai Koehne <kai.koehne@qt.io> | 2017-06-16 17:38:29 +0200 |
---|---|---|
committer | Kai Koehne <kai.koehne@qt.io> | 2017-06-22 09:50:33 +0000 |
commit | 4ba1f6ec607f05807f39a271e7e4c457fe3b0803 (patch) | |
tree | 977e9cfec3222eac47f4a50445405ba53e7fc996 /examples/uitools/textfinder | |
parent | 6da03454544364ffe414b7c03cfcb0810abb43d4 (diff) |
TextFinder: Improve code and documentation
Task-number: QTBUG-61419
Change-Id: Ib00152f0a04546d275a40a5d57fe046b82ebc421
GPush-Base: 8abb03b0d84a3a94f7db41e010f69955488ea893
Reviewed-by: Martin Smith <martin.smith@qt.io>
Diffstat (limited to 'examples/uitools/textfinder')
-rw-r--r-- | examples/uitools/textfinder/forms/input.txt | 12 | ||||
-rw-r--r-- | examples/uitools/textfinder/main.cpp | 9 | ||||
-rw-r--r-- | examples/uitools/textfinder/textfinder.cpp | 85 | ||||
-rw-r--r-- | examples/uitools/textfinder/textfinder.h | 12 | ||||
-rw-r--r-- | examples/uitools/textfinder/textfinder.pro | 12 |
5 files changed, 59 insertions, 71 deletions
diff --git a/examples/uitools/textfinder/forms/input.txt b/examples/uitools/textfinder/forms/input.txt index 96e8c3047..4dc15ce4e 100644 --- a/examples/uitools/textfinder/forms/input.txt +++ b/examples/uitools/textfinder/forms/input.txt @@ -1,9 +1,3 @@ -These forms are processed at run-time to produce dynamically-generated user interfaces. -In order to generate a form at run-time, a resource file containing a UI file is needed. -Applications that use the form handling classes need to be configured to be built against -the QtUiTools module. This is done by including the following declaration in a qmake project -file to ensure that the application is compiled and linked appropriately. A form loader object, -provided by the QUiLoader class, is used to construct the user interface. This user interface -can be retrieved from any QIODevice; for example, a QFile object can be used to obtain a form -stored in a project's resources. The QUiLoader::load() function takes the user interface -description contained in the file and constructs the form widget.
\ No newline at end of file +This user interface was loaded and processed at run-time. + +Applications that want to load .ui files at run-time must be configured to be link against the QtUiTools module. A form loader object, provided by the QUiLoader class, is used to construct the user interface. This user interface can be retrieved from any QIODevice; for example, a QFile object can be used to obtain a form stored in a project's resources. The QUiLoader::load() function takes the user interface description contained in the file and constructs the form widget. diff --git a/examples/uitools/textfinder/main.cpp b/examples/uitools/textfinder/main.cpp index 9dab51e19..3db26ebe7 100644 --- a/examples/uitools/textfinder/main.cpp +++ b/examples/uitools/textfinder/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -48,17 +48,16 @@ ** ****************************************************************************/ -#include <QApplication> #include "textfinder.h" +#include <QApplication> //! [0] int main(int argc, char *argv[]) { - Q_INIT_RESOURCE(textfinder); QApplication app(argc, argv); - TextFinder *textFinder = new TextFinder; - textFinder->show(); + TextFinder textFinder; + textFinder.show(); return app.exec(); } diff --git a/examples/uitools/textfinder/textfinder.cpp b/examples/uitools/textfinder/textfinder.cpp index a40be8886..a18505c8c 100644 --- a/examples/uitools/textfinder/textfinder.cpp +++ b/examples/uitools/textfinder/textfinder.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -48,15 +48,43 @@ ** ****************************************************************************/ -#include <QtUiTools> -#include <QtWidgets> #include "textfinder.h" +#include <QFile> +#include <QLineEdit> +#include <QMessageBox> +#include <QPushButton> +#include <QTextEdit> +#include <QTextStream> +#include <QUiLoader> +#include <QVBoxLayout> + +//! [4] +static QWidget *loadUiFile(QWidget *parent) +{ + QFile file(":/forms/textfinder.ui"); + file.open(QIODevice::ReadOnly); + + QUiLoader loader; + return loader.load(&file, parent); +} +//! [4] + +//! [5] +static QString loadTextFile() +{ + QFile inputFile(":/forms/input.txt"); + inputFile.open(QIODevice::ReadOnly); + QTextStream in(&inputFile); + in.setCodec("UTF-8"); + return in.readAll(); +} +//! [5] //! [0] TextFinder::TextFinder(QWidget *parent) : QWidget(parent) { - QWidget *formWidget = loadUiFile(); + QWidget *formWidget = loadUiFile(this); //! [1] ui_findButton = findChild<QPushButton*>("findButton"); @@ -69,7 +97,7 @@ TextFinder::TextFinder(QWidget *parent) //! [2] //! [3a] - loadTextFile(); + ui_textEdit->setText(loadTextFile()); //! [3a] //! [3b] @@ -80,40 +108,9 @@ TextFinder::TextFinder(QWidget *parent) //! [3c] setWindowTitle(tr("Text Finder")); - isFirstTime = true; } //! [3c] -//! [4] -QWidget* TextFinder::loadUiFile() -{ - QUiLoader loader; - - QFile file(":/forms/textfinder.ui"); - file.open(QFile::ReadOnly); - - QWidget *formWidget = loader.load(&file, this); - file.close(); - - return formWidget; -} -//! [4] - -//! [5] -void TextFinder::loadTextFile() -{ - QFile inputFile(":/forms/input.txt"); - inputFile.open(QIODevice::ReadOnly); - QTextStream in(&inputFile); - QString line = in.readAll(); - inputFile.close(); - - ui_textEdit->append(line); - ui_textEdit->setUndoRedoEnabled(false); - ui_textEdit->setUndoRedoEnabled(true); -} -//! [5] - //! [6] //! [7] void TextFinder::on_findButton_clicked() { @@ -122,14 +119,14 @@ void TextFinder::on_findButton_clicked() bool found = false; - if (isFirstTime == false) - document->undo(); + // undo previous change (if any) + document->undo(); if (searchString.isEmpty()) { QMessageBox::information(this, tr("Empty Search Field"), - "The search field is empty. Please enter a word and click Find."); + tr("The search field is empty. " + "Please enter a word and click Find.")); } else { - QTextCursor highlightCursor(document); QTextCursor cursor(document); @@ -141,12 +138,13 @@ void TextFinder::on_findButton_clicked() colorFormat.setForeground(Qt::red); while (!highlightCursor.isNull() && !highlightCursor.atEnd()) { - highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords); + highlightCursor = document->find(searchString, highlightCursor, + QTextDocument::FindWholeWords); if (!highlightCursor.isNull()) { found = true; highlightCursor.movePosition(QTextCursor::WordRight, - QTextCursor::KeepAnchor); + QTextCursor::KeepAnchor); highlightCursor.mergeCharFormat(colorFormat); } } @@ -154,11 +152,10 @@ void TextFinder::on_findButton_clicked() //! [8] cursor.endEditBlock(); //! [7] //! [9] - isFirstTime = false; if (found == false) { QMessageBox::information(this, tr("Word Not Found"), - "Sorry, the word cannot be found."); + tr("Sorry, the word cannot be found.")); } } } diff --git a/examples/uitools/textfinder/textfinder.h b/examples/uitools/textfinder/textfinder.h index a28defa49..17336d260 100644 --- a/examples/uitools/textfinder/textfinder.h +++ b/examples/uitools/textfinder/textfinder.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -54,9 +54,9 @@ #include <QWidget> QT_BEGIN_NAMESPACE +class QLineEdit; class QPushButton; class QTextEdit; -class QLineEdit; QT_END_NAMESPACE //! [0] @@ -65,20 +65,16 @@ class TextFinder : public QWidget Q_OBJECT public: - TextFinder(QWidget *parent = 0); + explicit TextFinder(QWidget *parent = nullptr); private slots: void on_findButton_clicked(); private: - QWidget* loadUiFile(); - void loadTextFile(); - QPushButton *ui_findButton; QTextEdit *ui_textEdit; QLineEdit *ui_lineEdit; - bool isFirstTime; }; //! [0] -#endif +#endif // TEXTFINDER_H diff --git a/examples/uitools/textfinder/textfinder.pro b/examples/uitools/textfinder/textfinder.pro index 667711dda..8f66814c4 100644 --- a/examples/uitools/textfinder/textfinder.pro +++ b/examples/uitools/textfinder/textfinder.pro @@ -1,8 +1,10 @@ -HEADERS = textfinder.h -RESOURCES = textfinder.qrc -SOURCES = textfinder.cpp main.cpp +#! [0] +QT += widgets uitools + +HEADERS = textfinder.h +SOURCES = textfinder.cpp main.cpp +RESOURCES = textfinder.qrc +#! [0] target.path = $$[QT_INSTALL_EXAMPLES]/uitools/textfinder INSTALLS += target - -QT += widgets uitools |