当前位置: 首页 > news >正文

Qt实现网页内嵌

文章目录

一、环境准备

二、代码实现

三、测试


一、环境准备

首先,确保你的Qt安装包含了QtWebEngine模块。我的Qt是5.12.9并且使用MSVC来编译项目。在项目文件中需要添加以下配置,其中在Qt中配置MSVC,建议去看看这位大佬的博客:Qt 添加MSVC2017编译器(2022年保姆级教程,不安装完整VS)_qt msvc2017-CSDN博客

确保:

QT += core gui webenginewidgets

二、代码实现

mainwindow.cpp,主要实现的简单网页浏览器,其通过QWebEngineView组件实现了网页内嵌功能。如下为测试demo文件代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QStyle>
#include <QApplication>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 设置窗口标题和大小setWindowTitle("Web Browser");resize(1024, 768);// 创建工具栏toolBar = new QToolBar(this);addToolBar(toolBar);// 创建地址栏urlLineEdit = new QLineEdit(this);urlLineEdit->setPlaceholderText("Enter URL (e.g., https://www.google.com)");urlLineEdit->setStyleSheet("QLineEdit { padding: 5px; border-radius: 3px; }");toolBar->addWidget(urlLineEdit);// 创建前进按钮goButton = new QPushButton("Go", this);goButton->setStyleSheet("QPushButton { padding: 5px 15px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; }""QPushButton:hover { background-color: #45a049; }");toolBar->addWidget(goButton);// 创建网页视图webView = new QWebEngineView(this);setCentralWidget(webView);// 创建进度条progressBar = new QProgressBar(this);progressBar->setMaximumHeight(2);progressBar->setTextVisible(false);progressBar->setStyleSheet("QProgressBar { border: none; background-color: #f0f0f0; }""QProgressBar::chunk { background-color: #4CAF50; }");statusBar()->addPermanentWidget(progressBar);// 连接信号和槽connect(goButton, &QPushButton::clicked, this, &MainWindow::loadPage);connect(urlLineEdit, &QLineEdit::returnPressed, this, &MainWindow::loadPage);connect(webView, &QWebEngineView::urlChanged, this, &MainWindow::updateUrl);connect(webView, &QWebEngineView::loadProgress, this, &MainWindow::updateProgress);connect(webView, &QWebEngineView::titleChanged, this, &MainWindow::updateTitle);// 设置初始页面webView->setUrl(QUrl("https://www.google.com"));
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::loadPage()
{QString url = urlLineEdit->text();if (!url.startsWith("http://") && !url.startsWith("https://")) {url = "https://" + url;}webView->setUrl(QUrl(url));
}void MainWindow::updateUrl(const QUrl &url)
{urlLineEdit->setText(url.toString());
}void MainWindow::updateProgress(int progress)
{progressBar->setValue(progress);if (progress == 100) {progressBar->hide();} else {progressBar->show();}
}void MainWindow::updateTitle(const QString &title)
{setWindowTitle(title + " - Web Browser");
}

三、测试

1.打开博客网页:

2.打开B站网页: 

 

 合理!!!!!

相关文章:

  • UDP协议(特点与应用场景)
  • cudaMalloc函数说明
  • 无人机 | 无人机设计概述
  • 某信服EDR3.5.30.ISO安装测试(一)
  • 【Java项目脚手架系列】第二篇:JavaWeb项目脚手架
  • 数据库MySQL学习——day9(聚合函数与分组数据)
  • 机器学习中常见搜索算法
  • Java基础学完,继续深耕(0505)Linux 常用命令
  • Python学习之路(七)-绘画and动画
  • 【Linux】linux入门——基本指令
  • 人工智能数学基础(十)—— 图论
  • 一个电商场景串联23种设计模式:创建型、结构型和行为型
  • Paramiko 核心类关系图解析
  • 新型深度神经网络架构:ENet模型
  • 健康养生:从生活点滴启航
  • Sliding Window Attention(Longformer)
  • 自注意力(Self-Attention)和位置编码
  • ByteArrayOutputStream 类详解
  • 在Java中,什么是checked exception(即compile-time exception、编译时异常)?
  • 【学习笔记】机器学习(Machine Learning) | 第五章(3)| 分类与逻辑回归
  • 胡塞武装称以色列所有机场均为其打击目标
  • 韩国总统选举民调:共同民主党前党首李在明支持率超46%
  • 五一当天1372对新人在沪喜结连理,涉外婚姻登记全市铺开
  • 马克思主义理论研究教学名师系列访谈|丁晓强:马克思主义学者要更关注社会现实的需要
  • 剑指3000亿产业规模,机器人“武林大会”背后的无锡“野望”
  • 【社论】法治是对民营经济最好的促进