[Interest] QWebEngineView not loading pages from history
Mike Jackson
imikejackson at gmail.com
Wed Dec 16 21:13:10 CET 2015
I have an small app that uses QWebEngineView to display web pages. We have tried hooking up a pair of QPushButtons to invoke the forward and back in the QWebEngineHistory but neither seem to work. The QActions are getting triggered and our code is correctly calling them but the QWebEngineView does not actually load the previous page from its history. We have printed out the history each time the button is clicked and the history is correctly navigated. I am sure we have missed something simple that needs to be performed. Maybe some sort of setup of the QWebEngineView or something? I am pasting in our simple application hoping that someone can spot the error.
Thanks
Mike Jackson
———main.cpp—————
#include <QApplication>
#include "WebTest_UI.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QCoreApplication::setOrganizationDomain("Foo");
QCoreApplication::setOrganizationName("Bar");
QCoreApplication::setApplicationName("WebTest_UI");
#if defined (Q_OS_MAC)
app.setQuitOnLastWindowClosed( true );
#endif
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
WebTest_UI* helper = WebTest_UI::Instance();
helper->show();
return app.exec();
}
——WebTest_UI.h—————
#ifndef _WebTest_UI_H_
#define _WebTest_UI_H_
#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>
#include <QtWebEngineWidgets/QWebEngineView>
class WebTest_UI : public QWidget
{
Q_OBJECT
public:
virtual ~WebTest_UI();
static WebTest_UI* Instance();
protected:
WebTest_UI();
protected slots:
void goBack(bool b);
private:
static WebTest_UI* self;
QWebEngineView* m_WebView;
QPushButton* forwardBtn;
QPushButton* backBtn;
WebTest_UI(const WebTest_UI&); // Copy Constructor Not Implemented
void operator=(const WebTest_UI&); // Operator '=' Not Implemented
};
#endif /* _WebTest_UI_H */
——WebTest_UI.cpp—————
#include "WebTest_UI.h"
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QAction>
#include <QtWebEngineWidgets/QWebEngineHistory>
// Include the MOC generated CPP file which has all the QMetaObject methods/data
#include "moc_WebTest_UI.cpp"
WebTest_UI* WebTest_UI::self = NULL;
// -----------------------------------------------------------------------------
WebTest_UI::WebTest_UI()
{
Q_ASSERT_X(!self, "WebTest_UI", "There should be only one WebTest_UI object");
WebTest_UI::self = this;
// Create the web view
m_WebView = new QWebEngineView(NULL);
m_WebView->setObjectName(QStringLiteral("m_WebView"));
QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(m_WebView->sizePolicy().hasHeightForWidth());
m_WebView->setSizePolicy(sizePolicy1);
m_WebView->setUrl(QUrl(QStringLiteral("about:blank")));
QGridLayout* gridLayout = new QGridLayout(this);
gridLayout->addWidget(m_WebView, 2, 0, 1, 2);
m_WebView->load(QUrl("http://download.qt.io"));
m_WebView->show();
backBtn = new QPushButton("Back");
gridLayout->addWidget(backBtn, 1, 0, 1, 1);
connect(backBtn, SIGNAL(clicked(bool)),
this, SLOT(goBack(bool)));
forwardBtn = new QPushButton("Forward");
gridLayout->addWidget(forwardBtn, 1,1,1,1);
self->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
#if defined (Q_OS_MAC)
QAction* closeAction = new QAction(this);
closeAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
addAction(closeAction);
#endif
}
// -----------------------------------------------------------------------------
WebTest_UI::~WebTest_UI() {}
// -----------------------------------------------------------------------------
WebTest_UI* WebTest_UI::Instance()
{
if (self == NULL)
{
self = new WebTest_UI();
}
return self;
}
// -----------------------------------------------------------------------------
void WebTest_UI::goBack(bool b)
{
QList<QWebEngineHistoryItem> backHistory = m_WebView->history()->backItems(10000);
QList<QWebEngineHistoryItem> forwardHistory = m_WebView->history()->forwardItems(10000);
qDebug() << "Before:" << backHistory.size() << " " << forwardHistory.size();
m_WebView->back();
backHistory = m_WebView->history()->backItems(10000);
forwardHistory = m_WebView->history()->forwardItems(10000);
qDebug() << "Current History Index:" << m_WebView->history()->currentItemIndex();
qDebug() << "After:" << backHistory.size() << " " << forwardHistory.size();
}
More information about the Interest
mailing list