Qt探索之旅(九)簡單實現瀏覽器
阿新 • • 發佈:2019-01-30
切記要在.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();
}