QT webengine显示HTML简单示例

文章目录

参考

QT webengine显示HTML简单示例

示例1

  • 编译器 : Desktop Qt 5.15.2 MSVC2019 64bit
  • 编辑器: QtCreator
  • 代码:

TestWebenqine.pro

cpp 复制代码
# TestWebenqine.pro
QT       += core gui webenginewidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mainwindow.h

cpp 复制代码
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWebEngineView>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    //使得网页的窗口大小随着mainwindow的窗口大小变化而变化
    void resizeEvent(QResizeEvent *event);

private:
    Ui::MainWindow *ui;

    QWebEngineView *view;  //声明view
};
#endif // MAINWINDOW_H

mainwindow.cpp

cpp 复制代码
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QResizeEvent>

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

    view = new QWebEngineView(this);
    //view->setFixedSize(this->width(),this->height());
    view->load(QUrl(QStringLiteral("https://www.qweather.com/weather/luoyang-101180901.html")));
    //view->load(QUrl(QStringLiteral("https://www.baidu.com")));
    view->showMaximized();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    QMainWindow::resizeEvent(event); // 调用基类的 resizeEvent,确保正常的处理

    // 获取新的 mainwindow 大小
    QSize newSize = event->size();

    // 将新的大小应用于 view
    view->setFixedSize(newSize.width(), newSize.height());
}

main.cpp

cpp 复制代码
//main.cpp
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

效果


示例2 (使用setDevToolsPage函数)

  • 编译器 : Desktop Qt 5.15.2 MSVC2019 64bit
  • 编辑器: QtCreator
  • 代码:

main.cpp

cpp 复制代码
#include "mainwindow.h"

#include <QApplication>
#include <QWebEngineView>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //MainWindow w;
    //w.show();
    QWebEngineView *view = new QWebEngineView();
    QWebEngineView *view1 = new QWebEngineView();
    view->setUrl(QUrl("http://baidu.com"));
    view->page()->setDevToolsPage(view1->page());
    
    view->setWindowTitle("BaiDu");
    view1->setWindowTitle("DevTool");
    
    view->show();//显示页面
    view1->show();//显示view页面的JS脚本(也就是说:view1是view的开发者工具)
    return a.exec();
}

效果

相关推荐
唐叔在学习2 分钟前
insertAdjacentHTML踩坑实录:AI没搞定的问题,我给搞定啦
前端·javascript·html
小则又沐风a14 分钟前
数据结构->链表篇
前端·html
CS Beginner1 小时前
【单片机】嵌入式显示屏开发框架:QT、SDL、LVGL 深度解析
单片机·嵌入式硬件·qt
金色熊族1 小时前
MV结构下设置Qt表格的代理(2)
c++·qt
Morwit2 小时前
Qt qml创建c++类的单例对象
开发语言·c++·qt
晓得迷路了2 小时前
栗子前端技术周刊第 112 期 - Rspack 1.7、2025 JS 新星榜单、HTML 状态调查...
前端·javascript·html
jinmo_C++2 小时前
从零开始学前端 · HTML 基础篇(一):认识 HTML 与页面结构
前端·html·状态模式
YxVoyager2 小时前
Qt C++ :QRegularExpression 正则表达式使用详解
c++·qt·正则表达式
qq_401700412 小时前
QStackedLayout 实现遮罩层
qt
Larry_Yanan2 小时前
Qt多进程(十一)Linux下socket通信
linux·开发语言·c++·qt