Reputation: 218
I am looking for an example of this function use:
bool QWebView::findText ( const QString & subString, QWebPage::FindFlags options = 0 )
Let's say I have a QWebView called browser. How can I search for a QString called word? Also what can it be done with options?
Here is my code: webbrowser.h
#ifndef WEBBROWSER_H
#define WEBBROWSER_H
#include <QWidget>
#include <QUrl>
#include <QWebFrame>
#include <QtGui>
#include <QWebView>
class QLineEdit;
class QToolButton;
class QWebView;
class WebBrowser : public QWidget
{
Q_OBJECT
public:
WebBrowser(QWidget *parent = 0);
signals:
public slots:
void loadPage();
void updateAddressBar(const QUrl &url);
void search();
void scanPageForWord();
private:
QLineEdit *addressBar;
QToolButton *backButton;
QToolButton *forwardButton;
QToolButton *reloadButton;
QToolButton *searchButton;
QWebView *browser;
QString *word;
//QWebFrame frame;
QStringList tempList;
};
#endif // WEBBROWSER_H
webbrowser.cpp
#include "webbrowser.h"
#include <QLayout>
#include <QToolButton>
#include <QLineEdit>
#include <QWebView>
#include <QWebFrame>
WebBrowser::WebBrowser(QWidget *parent) :
QWidget(parent)
{
addressBar = new QLineEdit(this);
backButton = new QToolButton(this);
forwardButton = new QToolButton(this);
reloadButton = new QToolButton(this);
searchButton = new QToolButton(this);
browser = new QWebView(this);
backButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
forwardButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
reloadButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
searchButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
connect(addressBar, SIGNAL(returnPressed()), this, SLOT(loadPage()));
QHBoxLayout *toolsLayout = new QHBoxLayout;
toolsLayout->addWidget(backButton);
toolsLayout->addWidget(forwardButton);
toolsLayout->addWidget(reloadButton);
toolsLayout->addWidget(searchButton);
toolsLayout->addWidget(addressBar);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(toolsLayout);
mainLayout->addWidget(browser);
backButton->setDefaultAction(browser->pageAction(QWebPage::Back));
forwardButton->setDefaultAction(browser->pageAction(QWebPage::Forward));
reloadButton->setDefaultAction(browser->pageAction(QWebPage::Reload));
connect(browser, SIGNAL(urlChanged(QUrl)),this, SLOT(updateAddressBar(QUrl)));
connect(searchButton,SIGNAL(clicked()),this, SLOT(search()));
//frame = browser->page()->mainFrame();
}
void WebBrowser::loadPage() {
browser->load(QUrl::fromUserInput(addressBar->text()));
}
void WebBrowser::updateAddressBar(const QUrl &url) {
QString urlChange = url.toString();
addressBar->setText(urlChange);
}
void WebBrowser::search()
{
QLineEdit *searchBox = new QLineEdit();
searchBox->show();
word = new QString(searchBox->text());
connect(searchBox,SIGNAL(returnPressed()),this, SLOT(scanPageForWord()));
}
void WebBrowser::scanPageForWord()
{
browser->findText(word,QWebPage::HighlightAllOccurrences);
}
main.cpp
#include <QtGui>
#include <QWebView>
#include "webbrowser.h"
int main(int argc, char **argv){
QApplication app(argc, argv);
WebBrowser browser;
browser.show();
return app.exec();
}
Upvotes: 2
Views: 2526
Reputation: 1478
If you don't write QWebPage::FindFlags, the default find direction is forward!
browser->findText(word);
Upvotes: 0
Reputation: 29471
use is:
browser.findText(*word, QWebPage::FindCaseSensitively);
You declare QString *word;
and a QString word is required
, so use *word in findText();
and you can put thoses values as FindFlags
Constant Value Description
QWebPage::FindBackward 1 Searches backwards instead of forwards.
QWebPage::FindCaseSensitively 2 By default findText() works case insensitive. Specifying this option changes the behaviour to a case sensitive find operation.
QWebPage::FindWrapsAroundDocument 4 Makes findText() restart from the beginning of the document if the end was reached and the text was not found.
QWebPage::HighlightAllOccurrences 8 Highlights all existing occurrences of a specific string. (This value was introduced in 4.6.)
Upvotes: 4