Stefan Titus
Stefan Titus

Reputation: 13

Referencing WebKit or WebEngine in .ui file depending on Qt version

I am attempting to upgrade a Qt application from 5.3.1 to 5.7.0. The main change is the migration from WebKit to WebEngine. http://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html

We are not yet ready to stop using 5.3.1, so I created a preprocessor define based on the Qt version. This works well for the project file, class source, and class header. However, I don't see a way to do this in the .ui file.

The Qt version is used to define a preprocessor define in the .pro file:

greaterThan(QT_MAJOR_VERSION, 4) {
    QT += widgets

    greaterThan(QT_MINOR_VERSION, 5) {
        QT += webengine webenginewidgets
        DEFINES += _WEBENGINE_
    } else {
        QT += webkit       webkitwidgets
    }
}

This works well in the .cpp file:

#ifdef _WEBENGINE_
    #include <QWebEngineView>
    #include <QWebEnginePage>
    #include <QWebEngineFrame>
    #include <QWebEngineElement>
#else
    #include <QWebView>
    #include <QWebPage>
    #include <QWebFrame>
    #include <QWebElement>
#endif // _WEBENGINE_

These are the offending references in the .ui file:

<widget class="QWebView" name="webView" native="true">
...
</widget>
...
<customwidget>
 <class>QWebView</class>
 <extends>QWidget</extends>
 <header>QtWebKit/QWebView</header>
</customwidget>

Which results in the ui_*.h file:

#include <QtWebKitWidgets/QWebView>
...
public:
    QWebView *webView;

How can I use the correct include paths and class names in the .ui file depending on the Qt version?

Upvotes: 1

Views: 314

Answers (1)

E4z9
E4z9

Reputation: 1767

You cannot do that in the .ui file. The usual way to solve this would be by creating your own wrapper widget, and using that in the .ui file.

class MyWebView : public QWidget
{ .... }

MyWebView::MyWebView(QObject *parent) : QWidget(parent)
{
#ifdef _WEBENGINE_
    // add webengineview to layout so it takes all space
#else
    // add webview to layout so it takes all space
#endif
    ...
}

Upvotes: 1

Related Questions