您的位置:首页 > 移动开发

Qt探索之旅(九)<QtWebKit/QWebView>简单实现浏览器

2018-01-20 23:38 555 查看
切记要在.pro文件里面添加

QT += webkit

我用的Qt4.7.4

(使用到QWebPage等webkit相关可视部件的,Qt5单独放到了QtWebKitWidgets模块中,所以得加头文件#include ,在.pro中要加QT += webkitwidgets



**QWebPage类和QWebView类一样,都用于查看和编辑网页,不同的是,从两者的包含关系上我们可以知道,QWebView可以用于打开多个网页,而里面具体的网页对象就是QWebPage。QWebPage通过mainframe()方法可以得到More QWebView-like functions,如load(), setUrl() 和 setHtml()。

QWebFrame可以算是QWebPage的元对象了,每一个QWebPage至少有一个QWebFrame,它被称作QWebPage的mainframe,通过QWebPage:: mainframe()方法得到。换言之,QWebFrame依附QWebPage存在。通过调用QWebFrame的page()方法返回它所在的QWebPage对象。而zoomFactor()方法则实现了网页内容的缩放。**

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtWebKit/QtWebKit>
#include <QtWebKit/QWebView>
#include <QUrl>
#include <QtDebug>
#include <QProgressBar>//使用进度条
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include "dialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void initMainPage();
void setUrlSlot(QUrl);
void setTitleSlot(QString title);
void browserWebSlot();
void deleteProgressBarSlot(bool ok);
void setMainPageSlot();
void sourceCodeSlot();

private:
Ui::MainWindow *ui;
QProgressBar *progress;

};

#endif // MAINWINDOW_H


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//实例化进度条
this->progress = new QProgressBar;
ui->statusBar->addWidget(this->progress);//状态栏添加进度条
this->initMainPage();//初始化主页
QObject::connect(ui->webView, SIGNAL(loadProgress(int)), this->progress, SLOT(setValue(int)));
QObject::connect(ui->webView, SIGNAL(urlChanged(QUrl)), this, SLOT(setUrlSlot(QUrl)));
QObject::connect(ui->webView, SIGNAL(titleChanged(QString)), this, SLOT(setWindowTitle(QString)));
QObject::connect(ui->lineEdit, SIGNAL(returnPressed()), this, SLOT(browserWebSlot()));
QObject::connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(deleteProgressBarSlot(bool)));
QObject::connect(ui->setMainPageAction, SIGNAL(triggered()), this, SLOT(setMainPageSlot()));
QObject::connect(ui->viewSourceCodeAction, SIGNAL(triggered()), this, SLOT(sourceCodeSlot()));
}

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

void MainWindow::initMainPage()//初始化主页
{
QString fileName = "mainpage.ini";
QFile *file = new QFile;
file->setFileName("fileName");
bool ok = file->open(QIODevice::ReadOnly);
if (ok)
{
QTextStream in(file);
ui->webView->load(QUrl(in.readLine().split("=").at(1)));
file->close();
delete file;
file = NULL;
}
else
{
QMessageBox::information(this, "Error Message", "Init Main Page Error" + file->errorString());
return;
}
}

void MainWindow::setUrlSlot(QUrl url)//网页更改修改url输入框
{
ui->lineEdit->setText(url.toString());
}

void MainWindow::setTitleSlot(QString title)//网页更改修改标题
{
this->setWindowTitle(title);
}

void MainWindow::browserWebSlot()//lineEdit回车(returnPressed)刷新网页
{
//qDebug() << ui->lineEdit->text();
ui->webView->load(QUrl(ui->lineEdit->text()));
}

void MainWindow::deleteProgressBarSlot(bool ok)//显示过后 删除进度条
{
if (ok)
{
delete this->progress;
this->progress = NULL;
//状态栏,进度条加载完毕后显示"Load Finished"持续5秒后状态栏信息消失
this->ui->statusBar->showMessage("Load Finished", 5*1000);
}
}

void MainWindow::sourceCodeSlot()//查看网页源代码
{
QString context = ui->webView->page()->currentFrame()->toHtml();
//ui->textEdit->setText();
Dialog *dialog = new Dialog;
dialog->setWebSource(context);
dialog->show();
}

void MainWindow::setMainPageSlot()//设置主页
{
QString fileName = "mainpage.ini";
QFile *file = new QFile;
file->setFileName(fileName);
bool ok = file->open(QIODevice::WriteOnly);
if (ok)
{
QTextStream out(file);
out << "mainpage="+ui->lineEdit->text();
file->close();
delete file;
file = NULL;

QMessageBox::information(this, "Information", "Set MaiaPage Success");
}
else
{
qDebug()<< "main page error" << file->errorString();
return;
}
}


dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();

void setWebSource(QString context);
//给Dialog的文本编辑器进行设置文本
private:
Ui::Dialog *ui;
};

#endif // DIALOG_H


dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}

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

void Dialog::setWebSource(QString context)
{
ui->textEdit->setPlainText(context);//设置纯文本
}


main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

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

return a.exec();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: