[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QTextCursor>
#include "textfinder.h"
#include "ui_textfinder.h"
TextFinder::TextFinder(QWidget *parent) :
QWidget(parent),
ui(new Ui::TextFinder)
{
ui->setupUi(this);
loadTextFile();
}
TextFinder::~TextFinder()
{
delete ui;
}
void TextFinder::loadTextFile()
{
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();
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
void TextFinder::on_findButton_clicked()
{
QString searchString = ui->lineEdit->text();
ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
}
#ifndef TEXTFINDER_H
#define TEXTFINDER_H
#include <QWidget>
namespace Ui {
class TextFinder;
}
class TextFinder : public QWidget
{
Q_OBJECT
public:
explicit TextFinder(QWidget *parent = 0);
~TextFinder();
private slots:
void on_findButton_clicked();
private:
Ui::TextFinder *ui;
void loadTextFile();
};
#endif // TEXTFINDER_H
To use QFile and QTextStream, add the following #includes to textfinder.cpp: #include <QtCore/QFile>ソースを見てもらえばわかるけど、TextEdit内でカーソル位置を動かすために
#include <QtCore/QTextStream>
COMMENT