I'm writing a cross-platform web browser in Qt, since it has built-in WebKit support via QWebView or the more up-to-date QWebEngineView. To get compact window chrome, I want to disable the native window title bar and border via Qt::FramelessWindowHint, but still get native behavior like resizing and Windows' Aero Snap.
First I trimmed down PKE's BorderlessWindow demo. This worked just fine: on Windows 8.1 x64, the window is resizable, the custom title bar can be dragged or double-clicked, and Aero Snap works.
Then I tried replacing the central QLabel with a QWebEngineView. This caused gray native-size borders to appear around my window. When I have interactive widgets at the top of the window (like a menu or toolbar), the "ghost" title bar with QWebEngineView pushes them down but accepts cursor clicks in their place.
Here's a screenshot comparing the two windows. (View it on a dark background to better see the light-gray border on the right.)
Is QWebEngineView at all compatible with a frameless window, or should I deal with the wasted space of native window chrome?
Edit: Replacing QWebEngineView with QWebView avoids this problem:
However, WebView is deprecated and WebEngine has more useful features:
- Renders with Chromium's Blink instead of Safari's WebKit
- Multiprocess, so you can run Javascript without locking the UI
- Doesn't leak Browser Plugin Details on Panopticlick (try it, QWebView populates navigator.plugins in Javascript but QWebEngineView does not)
Still, I'd really prefer not to waste space on a native title bar, so if QWebEngineView can be made to work with a frameless window I'd like to know how.
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