c++ - 在qt中的拆分器内调整网页大小

标签 c++ qt layout qwebengineview

我的问题很简单,但我一直在努力寻找解决方案。我在图像中显示了 QMainWindow,它是在 QtCreator 中构建的。

enter image description here

我想在 QWidget csWindow 中加载一个 html 网页,为此我在加载我的网页的地方放置了一个 Qlabel label_pic。这是到目前为止的代码:

MainWindow::MainWindow(QWidget *parent, Project *project) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->project = project;

    QWebEngineView *view = new QWebEngineView(ui->label_pic);
    view->load(QUrl("http://localhost/myWeb.html"));

    ////works fine, for an image
    //QPixmap pix(":/img/imgs/someImage.png");
    //ui->label_pic->setPixmap(pix);

    //I also can load the web page in the QWidget csWindow but with the same result
    //QWebEngineView *view  = new QWebEngineView(ui->csWindow);
    //view->load(QUrl("http://localhost/myWebb.html"));

}

页面加载正常,但不适合相应的空间,它是用固定大小创建的,永远不会调整大小。我希望在移动拆分器时调整网页大小,但我没有成功。

我尝试了几种方法,首先只是将图像放在 label_pic 中,启用属性缩放内容并且工作正常。现在,我想对网页做同样的事情。

提前致谢。

最佳答案

The page loads fine but it does not fit into the corresponding space

这是因为 QWebEngineView 的大小在完成加载之前是未知的,所以您需要连接到它的信号 loadFinished 并调整 label_pic 的大小:

connect(view, &QWebEngineView::loadFinished, [this]() {this->ui->label_pic->resize(this->ui->csWindow->size());});

I want the web page to be resized when I move the splitters

然后你还需要连接到信号 QSplitter::splitterMoved 来自你所有的分离器并像这样调整 csWindow 和 label_pic 的大小:

connect(ui->splitter, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});
connect(ui->splitter_2, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});
connect(ui->splitter_3, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});

请注意,如果您从设计器或添加代码为您的窗口设置布局,这将最有效,例如:

QGridLayout *layout = new QGridLayout;
layout->addWidget(ui->splitter_3);
this->ui->centralWidget->setLayout(layout);

请记住,您应该在加载 View 之前创建所有连接语句。

关于c++ - 在qt中的拆分器内调整网页大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50413981/

相关文章:

css - 我的页面布局在 IE7 中中断,如果我将鼠标悬停在/打开菜单项上,它会自行恢复

c++ - C++中的直线

c++ - pthread C++ 中的 Obj-C performSelector OnThread

c++ - Qt Json序列化

html - 在整个 body 上滚动的居中内容框

magento - 如何从 Magento 的左侧列中删除 "my cart"?

c++ - boost中的线程安全可复制循环缓冲区

c++ - 具有 3 个术语的可变参数宏

python - 设置布局的背景颜色

qt - 我应该如何在 QT4 中使用带有 qmake 而没有 cmake 的 PCL