QWebEngineView does not show

I have a very simple test project based on QtWebEngine:

------webengineview.h-------

#ifndef WEBENGINEVIEW_H
#define WEBENGINEVIEW_H

#include <QWebEngineView>

class WebengineView : public QWebEngineView
{
    Q_OBJECT

public:
    WebengineView(QWidget *parent = 0);
    ~WebengineView();
};

#endif // WEBENGINEVIEW_H



-----webengineview.cpp--------------

#include "webengineview.h"

WebengineView::WebengineView(QWidget *parent)
    : QWebEngineView(parent)
{
}

WebengineView::~WebengineView()
{

}





-----main.cpp---------

#include "webengineview.h"
#include <QApplication>

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

    QApplication a(argc, argv);
    WebengineView w;
    w.show();
    w.load(QUrl("https://www.google.com"));

    return a.exec();
}

The only widget is a QWebEngineView object. Unfortunately, after running the program, no window appears to show the web page. And even more weirdly, when I put the  QWebEngineView  object into a layout of another widget, it can display normally. Why does not the QWebEngineView  instance show its own window? Because there are some error messages appearing in the debug console like:

gpu_process_transport_factory.cc(642)] Switching to software compositing.

WARNING:gpu_process_transport_factory.cc(1007)] Lost UI shared context.

I think maybe there is some problem on the graphic card or the display driver. According to this post, I pass the arguments when running the program:

–disable-gpu  –disable-software-rasterizer

But the situation does not change, no window is shown. I had a problem on the incompatible graphic driver, so I add the following line to use software OpenGL,

QCoreApplication::setAttribute(Qt::AA_UseSoftwareOpenGL );

but the problem persists.

In fact, this problem is caused by not setting the size of the window of the QWebEngineView  widget. We have talked about the size of widgets in qt. It seems QWebEngineView widget gets zero size by default so the window is invisible. The solution is very simple, set a size for this QWebEngineView  widget:

resize(800,600);

 

 

 

Posted in

Leave a Reply