Qt | Implement web page history and search functions QWebEngineView

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

Foreword:

General browsers support displaying the history of browsed web pages. In the Qt WebEngine Widgets module, the QWebEngineHistory class can represent the browsing history of QWebEnginePage. So we can realize the function of displaying web page history through the QWebEngineHistory class.

QWebEngineView also provides the findText() function to find and highlight strings in web pages. You can use the findText() function to find strings in web pages.

Show history:

  • Use the history() function of QWebEngineView to return a QWebEngineHistory object, which represents the browsing history of a QWebEnginePage.
  • QWebEngineHistory uses the concept of the current item, and divides the visited pages into three parts: the current item, the items before the current item, and the items after the current item. You can use the back() and forward() functions to jump backwards or forwards.
  • You can use the currentItem() function to get the current item.
  • The current item can be specified using the goToItem() function.
  • You can use the backItems() function and the forwardItems() function to get a list of all items that can be browsed back and can be browsed forward.
  • A list of all items can be obtained using the items() function.
  • The total number of all items can be obtained using the count() function.
  • History can be cleared using the clear() function.

example:

QListWidget historyList = new QListWidget;//应该定义为成员对象
foreach(QWebEngineHistoryItem item, view->history()->items())
{
    QListWidget *history = new QListWidgetItem(item.title());
    historyList->addItem(history);
}
historyList->show();
复制代码

Find string:

  • The findText() function is also provided in QWebEngineView to realize the search and highlighting of strings in web pages. The default is to search forward and is case-insensitive.
  • It can be modified to look backward (QWebEnginePage::FindBackward) or case sensitive (QWebEnginePage::FindCaseSensitively) by setting the second parameter QWebEnginePage::FindFlags.

example:

QLineEdit *findEdit;//定义为成员对象
findEdit = new QLineEdit(this);
findEdit->setMaximumWidth(150);
ui->mainToolBar->addWidget(findEdit);
ui->mainToolBar->addAction(tr("查找"), [this]{
    view->findText(findEdit->text());
});
复制代码

Here a line edit box is created for entering the string to find, then it is added to the toolbar, and a "find" action is added to the toolbar. Click the Find button to highlight all the found strings on the page; click the "Find" button multiple times to traverse all the found strings.

If you want to cancel the find, just set the findText() parameter to empty. It is to clear the content in the search navigation editor, and then click the "Find" button. The search is then canceled.

Guess you like

Origin juejin.im/post/7080096293443338277
Recommended