Qt开发之webkit(编写网页浏览器)

QtWebKit提供了一个Web浏览器引擎,可以很方便的把万维网中的内容嵌套到Qt应用程序中.比如HTML,CSS,JS等


到这里Qt基础基本过了一遍,接下来就准备开发几个小项目来实践一下.


新建Qt  Gui应用程序,类名保持默认.在工程文件中添加   QT  += webkit.


webkit.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
class QWebView;
class QLineEdit;

#include <QModelIndex>
class QListWidget;

#include <QUrl>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QWebView *view;
    QLineEdit *locationEdit;
    int progress;

    QListWidget *historyList;

protected slots:
    void changeLocation();
    void setProgress(int);
    void adjustTitle();
    void finishLoading(bool);
    void changeIcon();

    void showHistory();
    void gotoHistory(QModelIndex);

    void urlChanged(QUrl);
    void findText();
};

#endif // MAINWINDOW_H

webkit.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebView>
#include <QLineEdit>

#include <QListWidget>
#include <QWebHistory>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    progress = 0;
    view = new QWebView(this);
    setCentralWidget(view);
    resize(800, 600);


    connect(view, SIGNAL(loadProgress(int)), this, SLOT(setProgress(int)));
    connect(view, SIGNAL(titleChanged(QString)), this, SLOT(adjustTitle()));
    connect(view, SIGNAL(loadFinished(bool)), this, SLOT(finishLoading(bool)));

    locationEdit = new QLineEdit(this);
    connect(locationEdit, SIGNAL(returnPressed()), this, SLOT(changeLocation()));


    ui->mainToolBar->addAction(view->pageAction(QWebPage::Back));
    ui->mainToolBar->addAction(view->pageAction(QWebPage::Forward));
    ui->mainToolBar->addAction(view->pageAction(QWebPage::Reload));
    ui->mainToolBar->addAction(view->pageAction(QWebPage::Stop));

    ui->mainToolBar->addAction(tr("ʷ"), this, SLOT(showHistory()));

    ui->mainToolBar->addAction(tr(""), this, SLOT(findText()));
    ui->mainToolBar->addWidget(locationEdit);


    locationEdit->setText("http://www.baidu.com");
    view->load(QUrl("http://www.baidu.com"));


    view->settings()->setIconDatabasePath("./");
    connect(view, SIGNAL(iconChanged()), this, SLOT(changeIcon()));

    historyList = new QListWidget;
    historyList->setWindowTitle(tr("ʷ¼"));
    historyList->setMinimumWidth(300);
    connect(historyList, SIGNAL(clicked(QModelIndex)),
            this, SLOT(gotoHistory(QModelIndex)));

    view->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
    connect(view->page(), SIGNAL(linkClicked(QUrl)),
            this, SLOT(urlChanged(QUrl)));
}

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


void MainWindow::changeLocation()
{
    QUrl url = QUrl(locationEdit->text());
    view->load(url);
    view->setFocus();
}


void MainWindow::setProgress(int p)
{
    progress = p;
    adjustTitle();
}


void MainWindow::adjustTitle()
{
    if ( progress <= 0 || progress >= 100) {
        setWindowTitle(view->title());
    } else {
        setWindowTitle(QString("%1 (%2%)").arg(view->title()).arg(progress));
    }
}


void MainWindow::finishLoading(bool finished)
{
    if (finished) {
        progress = 100;
        setWindowTitle(view->title());
    } else {
        setWindowTitle("web page loading error!");
    }
}


void MainWindow::changeIcon()
{
    setWindowIcon(view->icon());
}


void MainWindow::showHistory()
{
    QList<QWebHistoryItem> list;
    list = view->history()->items();

    historyList->clear();
    foreach (QWebHistoryItem item, list) {
        QListWidgetItem *history = new QListWidgetItem(item.icon(), item.title());
        historyList->addItem(history);
    }
    historyList->show();
}


void MainWindow::gotoHistory(QModelIndex index)
{
    QUrl url = view->history()->itemAt(index.row()).url();
    view->load(url);
}


void MainWindow::urlChanged(QUrl url)
{
    view->load(url);
}


void MainWindow::findText()
{
    view->page()->findText("yafeilinux", QWebPage::HighlightAllOccurrences);
}


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页