【问题标题】:using QWebEngine without margins使用没有边距的 QWebEngine
【发布时间】:2015-10-25 00:17:54
【问题描述】:

我试图让 QWebEngine 填满整个窗口。根据这个answer,我正在尝试使用setContentsMargins(0,0,0,0);,结果如下:QWebEngine 以全窗口大小加载页面,但随后立即缩小为:

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

这是我的错误/问题还是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);
}

【问题讨论】:

  • 尝试删除此行中的this指针view = new QWebEngineView(this)

标签: qt qtwebengine


【解决方案1】:

这对我有用,但我正在 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>

【讨论】:

    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 2016-07-19
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多