Qt实现网页内嵌

[复制链接]
发表于 2025-9-17 22:19:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
文章目录


一、环境准备
二、代码实现
三、测试
  


一、环境准备

首先,确保你的Qt安装包罗了QtWebEngine模块。我的Qt是5.12.9而且使用MSVC来编译项目。在项目文件中需要添加以下配置,其中在Qt中配置MSVC,发起去看看这位大佬的博客:Qt 添加MSVC2017编译器(2022年保姆级教程,不安装完备VS)_qt msvc2017-CSDN博客
确保:
  1. QT += core gui webenginewidgets
复制代码
二、代码实现

mainwindow.cpp,重要实现的简朴网页欣赏器,其通过QWebEngineView组件实现了网页内嵌功能。如下为测试demo文件代码:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QWebEngineView>
  4. #include <QWebEnginePage>
  5. #include <QWebEngineProfile>
  6. #include <QStyle>
  7. #include <QApplication>
  8. MainWindow::MainWindow(QWidget *parent)
  9.     : QMainWindow(parent)
  10.     , ui(new Ui::MainWindow)
  11. {
  12.     ui->setupUi(this);
  13.     // 设置窗口标题和大小
  14.     setWindowTitle("Web Browser");
  15.     resize(1024, 768);
  16.     // 创建工具栏
  17.     toolBar = new QToolBar(this);
  18.     addToolBar(toolBar);
  19.     // 创建地址栏
  20.     urlLineEdit = new QLineEdit(this);
  21.     urlLineEdit->setPlaceholderText("Enter URL (e.g., https://www.google.com)");
  22.     urlLineEdit->setStyleSheet("QLineEdit { padding: 5px; border-radius: 3px; }");
  23.     toolBar->addWidget(urlLineEdit);
  24.     // 创建前进按钮
  25.     goButton = new QPushButton("Go", this);
  26.     goButton->setStyleSheet("QPushButton { padding: 5px 15px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; }"
  27.                            "QPushButton:hover { background-color: #45a049; }");
  28.     toolBar->addWidget(goButton);
  29.     // 创建网页视图
  30.     webView = new QWebEngineView(this);
  31.     setCentralWidget(webView);
  32.     // 创建进度条
  33.     progressBar = new QProgressBar(this);
  34.     progressBar->setMaximumHeight(2);
  35.     progressBar->setTextVisible(false);
  36.     progressBar->setStyleSheet("QProgressBar { border: none; background-color: #f0f0f0; }"
  37.                               "QProgressBar::chunk { background-color: #4CAF50; }");
  38.     statusBar()->addPermanentWidget(progressBar);
  39.     // 连接信号和槽
  40.     connect(goButton, &QPushButton::clicked, this, &MainWindow::loadPage);
  41.     connect(urlLineEdit, &QLineEdit::returnPressed, this, &MainWindow::loadPage);
  42.     connect(webView, &QWebEngineView::urlChanged, this, &MainWindow::updateUrl);
  43.     connect(webView, &QWebEngineView::loadProgress, this, &MainWindow::updateProgress);
  44.     connect(webView, &QWebEngineView::titleChanged, this, &MainWindow::updateTitle);
  45.     // 设置初始页面
  46.     webView->setUrl(QUrl("https://www.google.com"));
  47. }
  48. MainWindow::~MainWindow()
  49. {
  50.     delete ui;
  51. }
  52. void MainWindow::loadPage()
  53. {
  54.     QString url = urlLineEdit->text();
  55.     if (!url.startsWith("http://") && !url.startsWith("https://")) {
  56.         url = "https://" + url;
  57.     }
  58.     webView->setUrl(QUrl(url));
  59. }
  60. void MainWindow::updateUrl(const QUrl &url)
  61. {
  62.     urlLineEdit->setText(url.toString());
  63. }
  64. void MainWindow::updateProgress(int progress)
  65. {
  66.     progressBar->setValue(progress);
  67.     if (progress == 100) {
  68.         progressBar->hide();
  69.     } else {
  70.         progressBar->show();
  71.     }
  72. }
  73. void MainWindow::updateTitle(const QString &title)
  74. {
  75.     setWindowTitle(title + " - Web Browser");
  76. }
复制代码
三、测试

1.打开博客网页:

2.打开B站网页: 
 
 公道!!!!!

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表