我正在寻找此功能使用的示例:

bool QWebView::findText ( const QString & subString, QWebPage::FindFlags options = 0 )

假设我有一个称为浏览器的QWebView。如何搜索称为单词的QString?还有选项可以做什么?

这是我的代码:
web浏览器
#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();
}

最佳答案

用途是:

browser.findText(*word, QWebPage::FindCaseSensitively);

您声明QString *word;QString word is required,因此在findText();中使用* word

您可以将这些值作为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.)

关于c++ - QWebView::findText Qt C++示例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22837073/

10-10 23:22