剛搭建了QT的開發環境,試試手吧,參見http://doc.trolltech.com/4.6/uitools-textfinder.html
。不過沒使用QT Creator,而是用Eclipse.
1、textfinder.ui檔案(修改):
<?xml version="1.0" encoding="UTF-8"?><br /><ui version="4.0"><br /> <class>TextFinderClass</class><br /> <widget class="QWidget" name="TextFinderClass"><br /> <property name="geometry"><br /> <rect><br /> <x>0</x><br /> <y>0</y><br /> <width>359</width><br /> <height>185</height><br /> </rect><br /> </property><br /> <property name="windowTitle"><br /> <string>TextFinder</string><br /> </property><br /> <widget class="QLabel" name="label"><br /> <property name="geometry"><br /> <rect><br /> <x>10</x><br /> <y>20</y><br /> <width>68</width><br /> <height>18</height><br /> </rect><br /> </property><br /> <property name="text"><br /> <string>Keywords:</string><br /> </property><br /> </widget><br /> <widget class="QLineEdit" name="lineEdit"><br /> <property name="geometry"><br /> <rect><br /> <x>100</x><br /> <y>20</y><br /> <width>141</width><br /> <height>26</height><br /> </rect><br /> </property><br /> </widget><br /> <widget class="QTextEdit" name="textEdit"><br /> <property name="geometry"><br /> <rect><br /> <x>20</x><br /> <y>60</y><br /> <width>321</width><br /> <height>111</height><br /> </rect><br /> </property><br /> </widget><br /> <widget class="QPushButton" name="findButton"><br /> <property name="geometry"><br /> <rect><br /> <x>250</x><br /> <y>20</y><br /> <width>92</width><br /> <height>28</height><br /> </rect><br /> </property><br /> <property name="text"><br /> <string>Find</string><br /> </property><br /> </widget><br /> </widget><br /> <layoutdefault spacing="6" margin="11"/><br /> <resources/><br /> <connections/><br /></ui>
2、textfinder.h檔案(修改):
#ifndef TEXTFINDER_H<br />#define TEXTFINDER_H<br />#include <QtGui/QWidget><br />#include "ui_textfinder.h"<br />class TextFinder : public QWidget<br />{<br /> Q_OBJECT<br />public:<br /> TextFinder(QWidget *parent = 0);<br /> ~TextFinder();<br />private:<br /> Ui::TextFinderClass ui;<br />private slots:<br /> void on_findButton_clicked();<br />private:<br /> void loadTextFile();<br />};<br />#endif // TEXTFINDER_H
3、textfinder.cpp檔案(修改):
#include <QtCore/QFile><br />#include <QtCore/QTextStream><br />#include "textfinder.h"<br />TextFinder::TextFinder(QWidget *parent)<br /> : QWidget(parent)<br />{<br /> ui.setupUi(this);<br /> loadTextFile();<br />}<br />TextFinder::~TextFinder()<br />{<br />}<br />void TextFinder::loadTextFile()<br />{<br /> QFile inputFile("input.txt");<br /> inputFile.open(QIODevice::ReadOnly);<br /> QTextStream in(&inputFile);<br /> QString line = in.readAll();<br /> inputFile.close();<br /> ui.textEdit->setPlainText(line);<br /> QTextCursor cursor = ui.textEdit->textCursor();<br /> cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);<br />}<br />void TextFinder::on_findButton_clicked()<br />{<br /> QString searchString = ui.lineEdit->text();<br /> ui.textEdit->find(searchString, QTextDocument::FindWholeWords);<br />}
4、ui_textfinder.h檔案(查看):
/********************************************************************************<br />** Form generated from reading UI file 'textfinder.ui'<br />**<br />** Created: Fri Dec 25 19:52:16 2009<br />** by: Qt User Interface Compiler version 4.6.0<br />**<br />** WARNING! All changes made in this file will be lost when recompiling UI file!<br />********************************************************************************/<br />#ifndef UI_TEXTFINDER_H<br />#define UI_TEXTFINDER_H<br />#include <QtCore/QVariant><br />#include <QtGui/QAction><br />#include <QtGui/QApplication><br />#include <QtGui/QButtonGroup><br />#include <QtGui/QHeaderView><br />#include <QtGui/QLabel><br />#include <QtGui/QLineEdit><br />#include <QtGui/QPushButton><br />#include <QtGui/QTextEdit><br />#include <QtGui/QWidget><br />QT_BEGIN_NAMESPACE<br />class Ui_TextFinderClass<br />{<br />public:<br /> QLabel *label;<br /> QLineEdit *lineEdit;<br /> QTextEdit *textEdit;<br /> QPushButton *findButton;<br /> void setupUi(QWidget *TextFinderClass)<br /> {<br /> if (TextFinderClass->objectName().isEmpty())<br /> TextFinderClass->setObjectName(QString::fromUtf8("TextFinderClass"));<br /> TextFinderClass->resize(359, 185);<br /> label = new QLabel(TextFinderClass);<br /> label->setObjectName(QString::fromUtf8("label"));<br /> label->setGeometry(QRect(10, 20, 68, 18));<br /> lineEdit = new QLineEdit(TextFinderClass);<br /> lineEdit->setObjectName(QString::fromUtf8("lineEdit"));<br /> lineEdit->setGeometry(QRect(100, 20, 141, 26));<br /> textEdit = new QTextEdit(TextFinderClass);<br /> textEdit->setObjectName(QString::fromUtf8("textEdit"));<br /> textEdit->setGeometry(QRect(20, 60, 321, 111));<br /> findButton = new QPushButton(TextFinderClass);<br /> findButton->setObjectName(QString::fromUtf8("findButton"));<br /> findButton->setGeometry(QRect(250, 20, 92, 28));<br /> retranslateUi(TextFinderClass);<br /> QMetaObject::connectSlotsByName(TextFinderClass);<br /> } // setupUi<br /> void retranslateUi(QWidget *TextFinderClass)<br /> {<br /> TextFinderClass->setWindowTitle(QApplication::translate("TextFinderClass", "TextFinder", 0, QApplication::UnicodeUTF8));<br /> label->setText(QApplication::translate("TextFinderClass", "Keywords:", 0, QApplication::UnicodeUTF8));<br /> findButton->setText(QApplication::translate("TextFinderClass", "Find", 0, QApplication::UnicodeUTF8));<br /> } // retranslateUi<br />};<br />namespace Ui {<br /> class TextFinderClass: public Ui_TextFinderClass {};<br />} // namespace Ui<br />QT_END_NAMESPACE<br />#endif // UI_TEXTFINDER_H