Qt 5.5的QWebEngineView与FramelessWindowHint兼容吗?

时间:2015-08-24 23:33:43

标签: c++ qt webkit qwebview borderless

我在Qt中编写了一个跨平台的Web浏览器,因为它通过QWebView或更新的QWebEngineView内置了WebKit支持。为了获得紧凑的窗口镶边,我想通过Qt :: FramelessWindowHint禁用本机窗口标题栏和边框,但仍然会获得本机行为,如调整大小和Windows' Aero Snap。

首先,我调整了PKEBorderlessWindow演示。这很好用:在Windows 8.1 x64上,窗口可以调整大小,可以拖动或双击自定义标题栏,Aero Snap可以工作。

然后我尝试用QWebEngineView替换中央QLabel。这导致灰色原生大小的边框出现在我的窗口周围。当我在窗口顶部有交互式小部件时(如菜单或工具栏)," ghost"带有QWebEngineView的标题栏将它们向下推,但接受光标点击。

这是比较两个窗口的屏幕截图。 (在深色背景上查看它以更好地看到右侧的浅灰色边框。)

QLabel and QWebEngineView windows

QWebEngineView是否与无框窗口兼容,还是应该处理原生窗口chrome的浪费空间?

编辑:用QWebView替换QWebEngineView可以避免此问题:

QWebView window

但是,不推荐使用WebView,WebEngine具有更多有用的功能:

  • 使用Chromium的Blink而不是Safari的WebKit进行渲染
  • Multiprocess,因此您可以在不锁定UI的情况下运行Javascript
  • 不会泄漏Panopticlick上的浏览器插件详细信息(试试看,QWebView在Javascript中填充navigator.plugins但QWebEngineView没有)

尽管如此,我真的不想在原生标题栏上浪费空间,所以如果QWebEngineView可以用于无框窗口,我想知道如何。

mainwindow.h:

#include <QtWidgets>
#include <QToolBar>

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow();
protected:
    void showEvent(QShowEvent *event) Q_DECL_OVERRIDE;
    bool nativeEvent(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
private:
    QToolBar *titleBar;
};

mainwindow.cpp:

#include <QtWidgets>
#include <QLabel>
#include <QWebEngineView>
#include <windows.h>
#include <windowsx.h>
#include "mainwindow.h"

MainWindow::MainWindow() : QMainWindow() {
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    titleBar = addToolBar(tr("Title Bar"));
    titleBar->setIconSize(QSize(16, 16));
    titleBar->setFloatable(false);
    titleBar->setMovable(false);
    titleBar->setStyleSheet("QToolBar { background: red; border: 0; padding: 0; }");
    titleBar->addWidget(new QLabel("Title Bar", titleBar));
    // Try QLabel...
    QLabel *central = new QLabel("Hello World");
    // ...or QWebEngineView
    //QWebEngineView *central = new QWebEngineView(this);
    //central->load(QUrl("http://www.google.com"));
    setCentralWidget(central);
    resize(320, 240);
}

bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) {
    Q_UNUSED(eventType);
    MSG *msg = (MSG *)message;
    HWND hwnd = isVisible() ? (HWND)winId() : NULL;
    LPARAM lparam = msg->lParam;
    const LONG border_width = 4;
    RECT winrect;
    long x, y;
    switch (msg->message) {
    case WM_NCCALCSIZE:
        result = 0;
        return true;
    case WM_NCHITTEST:
        GetWindowRect(hwnd, &winrect);
        x = GET_X_LPARAM(lparam);
        y = GET_Y_LPARAM(lparam);
        if (x >= winrect.left && x < winrect.left + border_width &&
            y < winrect.bottom && y >= winrect.bottom - border_width)
            *result = HTBOTTOMLEFT;
        else if (x < winrect.right && x >= winrect.right - border_width &&
            y < winrect.bottom && y >= winrect.bottom - border_width)
            *result = HTBOTTOMRIGHT;
        else if (x >= winrect.left && x < winrect.left + border_width &&
            y >= winrect.top && y < winrect.top + border_width)
            *result = HTTOPLEFT;
        else if (x < winrect.right && x >= winrect.right - border_width &&
            y >= winrect.top && y < winrect.top + border_width)
            *result = HTTOPRIGHT;
        else if (x >= winrect.left && x < winrect.left + border_width)
            *result = HTLEFT;
        else if (x < winrect.right && x >= winrect.right - border_width)
            *result = HTRIGHT;
        else if (y < winrect.bottom && y >= winrect.bottom - border_width)
            *result = HTBOTTOM;
        else if (y >= winrect.top && y < winrect.top + border_width)
            *result = HTTOP;
        else if (titleBar->underMouse())
            *result = HTCAPTION;
        else
            break;
        return true;
    }
    return QMainWindow::nativeEvent(eventType, message, result);
}

void MainWindow::showEvent(QShowEvent *event) {
    Q_UNUSED(event);
    HWND hwnd = (HWND)winId();
    DWORD newStyle = WS_POPUP | WS_CAPTION | WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
    SetWindowLongPtr(hwnd, GWL_STYLE, static_cast<LONG>(newStyle));
    SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
    ShowWindow(hwnd, SW_SHOW);
}

main.cpp中:

#include <QtWidgets>
#include "mainwindow.h"

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MainWindow *window = new MainWindow();
    window->show();
    return app.exec();
}

example.pro:

QT += core gui widgets webenginewidgets
msvc:LIBS += -luser32
TEMPLATE = app
SOURCES += main.cpp \
           mainwindow.cpp
HEADERS += mainwindow.h

0 个答案:

没有答案