使用没有边距的 QWebEngine

using QWebEngine without margins

我正在尝试让 QWebEngine 填充整个 window。根据这个 answer 我正在尝试使用 setContentsMargins(0,0,0,0); 并得到以下结果:QWebEngine 以完整 window 大小加载页面,但随后立即缩小到这个大小:

当我在布局中使用 setContentsMargins(1,1,1,1);QWebEngine 时,它加载正确,边距为 1 像素。我做了一个直接加载图像的测试,没有边距,它加载得很好并填满了屏幕。

这是我的bug/issue还是QWebEngine's


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebEngineWidgets>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->setContentsMargins(0,0,0,0);
    ui->centralWidget->setLayout(mainLayout);

//    // load and show image
//    inputImg = new QImage(":/images/testScreen.jpg");
//    imgDisplayLabel = new QLabel("");
//    imgDisplayLabel->setPixmap(QPixmap::fromImage(*inputImg));
//    imgDisplayLabel->adjustSize();
//     mainLayout->addWidget(imgDisplayLabel);

    view = new QWebEngineView(this);
     mainLayout->addWidget(view);

    QUrl url;
    url = QUrl("qrc:/testScreen.html");
    view->load(url);
}

这对我有用,但我正在 windows 平台上进行测试。 为了简洁起见,我在这里内联了构造函数定义。

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow() {
        auto webView = new QWebEngineView(this);
        auto main_layout = new QVBoxLayout(this);
        main_layout->setMargin(0);
        main_layout->addWidget(webView);
    }
};

而 html 是这样的:

<!DOCTYPE html>
<html style="width: 100%; height: 100%; margin: 0; padding: 0">
  <body style="overflow: hidden; width: 100%; height: 100%; margin: 0; padding: 0">
  </body>
</html>