Skip to content
Snippets Groups Projects
Commit 32495f55 authored by hjk's avatar hjk
Browse files

more fixes to the example

parent 13dec817
No related branches found
No related tags found
No related merge requests found
...@@ -573,51 +573,36 @@ ...@@ -573,51 +573,36 @@
\c{textfinder.cpp}. We begin by filling in the functionality to load a \c{textfinder.cpp}. We begin by filling in the functionality to load a
text file. The code snippet below describes this: text file. The code snippet below describes this:
\code \quotefromfile textfinder.cpp
void TextFinder::loadTextFile() \skipto TextFinder::loadTextFile
{ \printuntil }
QFile inputFile(":/input.txt");
inputFile.open(QIODevice::ReadOnly);
QTextStream in(&inputFile);
QString line = in.readAll();
inputFile.close();
ui->textEdit->setPlainText(line);
QTextCursor cursor = ui->textEdit->textCursor();
}
\endcode
Basically, we load a text file using Basically, we load a text file using
\l{http://doc.trolltech.com/qfile.html}{QFile}, read it with \l{http://doc.trolltech.com/qfile.html}{QFile}, read it with
\l{http://doc.trolltech.com/qtextstream.html}{QTextStream}, and \l{http://doc.trolltech.com/qtextstream.html}{QTextStream}, and
then display it on \c{textEdit} with then display it on \c{textEdit} with
\l{http://doc.trolltech.com/qtextedit.html#plainText-prop}{setPlainText()}. \l{http://doc.trolltech.com/qtextedit.html#plainText-prop}{setPlainText()}
which requires adding the following additional #includes to textfinder.cpp:
\code
#include <QtCore/QFile>
#include <QtCore/QTextStream>
\endcode
For the \c{on_findButton_clicked()} slot, we extract the search string and For the \c{on_findButton_clicked()} slot, we extract the search string and
use the \l{http://doc.trolltech.com/qtextedit.html#find}{find()} function use the \l{http://doc.trolltech.com/qtextedit.html#find}{find()} function
to look for the search string within the text file. The code snippet below to look for the search string within the text file. The code snippet below
further describes it: further describes it:
\code \quotefromfile textfinder.cpp
void TextFinder::on_findButton_clicked() \skipto on_findButton_clicked
{ \printuntil }
QString searchString = ui->lineEdit->text();
ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
}
\endcode
Once we have both these functions complete, we call \c{loadTextFile()} in Once we have both these functions complete, we call \c{loadTextFile()} in
our constructor. our constructor.
\code \quotefromfile textfinder.cpp
TextFinder::TextFinder(QWidget *parent) \skipto TextFinder::TextFinder
: QWidget(parent) \printuntil }
{
ui->setupUi(this);
loadTextFile();
}
\endcode
The \c{on_findButton_clicked()} slot will be called automatically due to The \c{on_findButton_clicked()} slot will be called automatically due to
this line of code: this line of code:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment