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();
}

效果

相关推荐
liwenzhuola4 小时前
解决 Ubuntu 上 Qt Creator 项目编译失败的问题
数据库·qt·ubuntu
娇娇yyyyyy5 小时前
QT编程(18): Qt QItemSelectionModel介绍
开发语言·qt
sthnyph5 小时前
QT开发:事件循环与处理机制的概念和流程概括性总结
开发语言·qt
机器视觉知识推荐、就业指导9 小时前
LVGL真能动摇Qt的地位吗?
开发语言·qt·系统架构
xingyynt13 小时前
【HTML+CSS】使用HTML与后端技术连接数据库
css·数据库·html
羊小猪~~14 小时前
【QT】--QWIdget与QDialog
开发语言·数据库·c++·后端·qt·求职招聘
Blasit16 小时前
Qt 程序打包,运行提示找不到或无法加载平台插件 qwindows.dll
开发语言·windows·qt
C++ 老炮儿的技术栈16 小时前
c++常见配置文件格式 JSON、INI、XML、YAML 它们如何解析
xml·开发语言·c++·windows·qt·json
镜中月ss16 小时前
QT中的资源树
开发语言·qt·qml
Laurence16 小时前
GitHub 1.2 万星 Qt 项目 VNote 源码解读(一):核心类与主流程
qt·github·源码·代码·介绍·解读·vnote