Qt find word function with QWebEngine

945 views Asked by At

I create a simple web browser with Qt 5.7 and the webEngine. I would like to create an search function to find word in webView but I don't really know how make interaction between web content and Qt ( I'm new on Qt, I do this for fun and to improve my level ).

this is the minimal code too display WebView : main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;
    w.showMaximized();

    return a.exec();
}

mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtWebEngineWidgets>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void setWebEngine();

private:
    Ui::MainWindow *ui;
    QWebEngineView *view;    
};

#endif // MAINWINDOW_H

mainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWebEngine();
    setCentralWidget(view);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setWebEngine()
{
    view = new QWebEngineView();
    view->load(QUrl("http://www.google.com"));
}

I would like to know something else, is it possible to list elements in the web content ( form, h1, h2, ... )

Have a nice day, and sorry for my English ;)

1

There are 1 answers

0
realvictorprm On BEST ANSWER

From the Qt5 documentation

void QWebEngineView::findText(const QString &subString, QWebEnginePage::FindFlags options = QWebEnginePage::FindFlags())

Finds the specified string, subString, in the page, using the given options.

To clear the selection, just pass an empty string.

See also selectedText() and selectionChanged().

Should be quite simple.