在qt中的拆分器内调整网页大小
Resizing web page inside a spliter in qt
我的问题很简单,但我一直在努力寻找解决方案。我在图像中显示了 QMainWindow,它是在 QtCreator 中构建的。
我想在 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"));
}
页面加载正常,但不适合相应的 space,它是用固定大小创建的,永远不会调整大小。我想在移动拆分器时调整网页大小,但我没有成功。
我尝试了几种方法,首先只是将图像放入 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());});
请注意,如果您通过设计器或添加代码为 window 设置布局,效果最佳,例如:
QGridLayout *layout = new QGridLayout;
layout->addWidget(ui->splitter_3);
this->ui->centralWidget->setLayout(layout);
请记住,您应该在加载视图之前创建所有连接语句。
我的问题很简单,但我一直在努力寻找解决方案。我在图像中显示了 QMainWindow,它是在 QtCreator 中构建的。
我想在 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"));
}
页面加载正常,但不适合相应的 space,它是用固定大小创建的,永远不会调整大小。我想在移动拆分器时调整网页大小,但我没有成功。
我尝试了几种方法,首先只是将图像放入 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());});
请注意,如果您通过设计器或添加代码为 window 设置布局,效果最佳,例如:
QGridLayout *layout = new QGridLayout;
layout->addWidget(ui->splitter_3);
this->ui->centralWidget->setLayout(layout);
请记住,您应该在加载视图之前创建所有连接语句。