Files
qt-creator/doc/qtcreator/examples/textfinder/textfinder.cpp

49 lines
986 B
C++
Raw Normal View History

// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "textfinder.h"
//! [1]
#include "./ui_textfinder.h"
#include <QFile>
#include <QTextStream>
//! [1]
2008-12-02 12:01:29 +01:00
//! [3]
2009-03-24 17:35:59 +01:00
TextFinder::TextFinder(QWidget *parent)
: QWidget(parent)
, ui(new Ui::TextFinder)
2008-12-02 12:01:29 +01:00
{
2009-03-24 17:35:59 +01:00
ui->setupUi(this);
2008-12-02 12:01:29 +01:00
loadTextFile();
}
//! [3]
2008-12-02 12:01:29 +01:00
TextFinder::~TextFinder()
{
2009-03-24 17:35:59 +01:00
delete ui;
2008-12-02 12:01:29 +01:00
}
//! [0]
2008-12-02 12:01:29 +01:00
void TextFinder::loadTextFile()
{
QFile inputFile(":/input.txt");
inputFile.open(QIODevice::ReadOnly);
QTextStream in(&inputFile);
QString line = in.readAll();
inputFile.close();
2009-03-24 17:35:59 +01:00
ui->textEdit->setPlainText(line);
QTextCursor cursor = ui->textEdit->textCursor();
2008-12-02 12:01:29 +01:00
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
//! [0]
2008-12-02 12:01:29 +01:00
//! [2]
2008-12-02 12:01:29 +01:00
void TextFinder::on_findButton_clicked()
{
2009-03-24 17:35:59 +01:00
QString searchString = ui->lineEdit->text();
ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
2008-12-02 12:01:29 +01:00
}
//! [2]