通信人家园

标题: 制作浏览器 _QT  [查看完整版帖子] [打印本页]

时间:  2010-11-1 20:03
作者: jayzhaoqi     标题: 制作浏览器 _QT

1.

Complementary&Statement:

This programe are desined to study the use of
Qt’s WebKit module. If you have any question, you can find some help in the following website.

The web site is:http://doc.qt.nokia.com/4.7/webkit-fancybrowser.html


You may could not usethe webkit before adding the code as in the the following to your qmake.profile.



QT += core gui \


Webkit


At last ,we willexplain the QtBrowse project in detail. First will shoule create anew project named QtBrowse. In the class information tab item,we choose theQMainWindow class and make the checkbox unchecked,then goahead with the default settings.



QtBrowser.pro :
QT
+= core gui\

webkit
TARGET = QtBrowserTEMPLATE = appRESOURCES = QtBrowser.qrcSOURCES += main.cpp\
mainwindow.cpp
HEADERS
+= mainwindow.h

QtBrowser.qrc:


<!DOCTYPE RCC><RCCversion="1.0">

<qresource>



<file>images/back.png</file>



<file>images/forward.png</file>



<file>images/reload.png</file>

</qresource>
</RCC>

MainWindow.h



#ifndef
MAINWINDOW_H#define
MAINWINDOW_H#include
<QtGui/QMainWindow>#include
<QApplication>#include
<QtGui>#include
<QtCore>#include
<QtWebKit/QWebView>class
MainWindow
:
public
QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget
*parent
=
0);

~MainWindow();private
slots:
void
loadLinkPage(const
QUrl
&
url);

void
loadPage();
void
backPage();
void
forwardPage();
void
reloadPage();
void
refreshLine();private:
void
createActions();
void
createToolBars();
void
createStatusBar();
QToolBar
*navigationToolBar;
QAction
*backAct;
QAction
*forwardAct;
QAction
*reloadAct;
QWebView
*pageView;
QLineEdit
*pageLine;};#endif
//
MAINWINDOW_H
MainWindow.cpp


#include
"mainwindow.h"
MainWindow::MainWindow(QWidget
*parent)



:
QMainWindow(parent){

pageView
=
new
QWebView(this);



pageView->load(QUrl("http://www.baidu.com/"));


setCentralWidget(pageView);

pageView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);


pageLine
=
new
QLineEdit(this);



pageLine->setText("http://www.baidu.com/");

createActions();

createToolBars();

createStatusBar();

connect(pageView,
SIGNAL(loadProgress(int)),
this,

SLOT(refreshLine()));

connect(pageLine,
SIGNAL(returnPressed()),
this,

SLOT(loadPage()));

connect(pageView,
SIGNAL(linkClicked(const
QUrl)),

SLOT(loadLinkPage(const
QUrl)));
}MainWindow::~MainWindow(){}void
MainWindow::createActions(){

backAct
=
new
QAction(QIcon(":/images/back.png"),
tr("&Back"),
this);


backAct->setShortcut(tr("Ctrl+B"));

connect(backAct,
SIGNAL(triggered()),
this,
SLOT(backPage()));


forwardAct
=
new
QAction(QIcon(":/images/forward.png"),
tr("&Forward"),
this);


forwardAct->setShortcut(tr("Ctrl+F"));

connect(forwardAct,
SIGNAL(triggered()),
this,
SLOT(forwardPage()));



reloadAct
=
new
QAction(QIcon(":/images/reload.png"),
tr("&Refresh"),
this);


reloadAct->setShortcut(tr("Ctrl+R"));


connect(reloadAct,
SIGNAL(triggered()),
this,
SLOT(reloadPage()));
}
void
MainWindow::createToolBars()
{


navigationToolBar
=
addToolBar(tr("Navigation"));


navigationToolBar->addAction(backAct);

navigationToolBar->addAction(forwardAct);


navigationToolBar->addAction(reloadAct);

navigationToolBar->addWidget(pageLine);
}
void
MainWindow::createStatusBar()
{

statusBar()->showMessage(tr("Ready"));
}
void
MainWindow::loadPage()
{


pageView->load(QUrl(pageLine->text()));
}
void
MainWindow::backPage()
{

pageView->back();
}
void
MainWindow::forwardPage()
{


pageView->forward();
}
void
MainWindow::reloadPage()
{

pageView->reload();
}
void
MainWindow::refreshLine()
{


pageLine->setText(pageView->url().toString());
}
void
MainWindow::loadLinkPage(const
QUrl
&
url)

{

pageView->load(url);

}
main.cpp

#include
<QtGui/QApplication>
#include
"mainwindow.h"#include
<QApplication>#include
<QtGui>#include
<QtCore>int
main(int
argc,
char
*argv[])
{

QApplication
a(argc,
argv);


MainWindow
myBrowser;


myBrowser.show();

return
a.exec();}


Explanation:
1)
QFile::setFileName
Set the name of thefile,which has no path,relative path and absolute path. If it has no path or arelative path.The current path of application is the same as it.
Example:
QFile file;

QDir::setCurrent("/tmp");


file.setFileName("readme.txt");


QDir::setCurrent("/home");


file.open(QIODevice::ReadOnly);


2)
QWebView


The QWebView class provide a widget that isused to view and edit web documents.

QWebView::load(const QUrl &url);
Load the specied url and show the website.



3)

Icon is one ofresource of Qt. In the content of the type of qrc file includerelative path of Icon.rcc is the resource debug of Qt, which bebug the qrcfile. We can use the object in the process of debug.

There are many kinds of picture file ,but weregularly use the png format. For example :


<!DOCTYPERCC><RCC version="1.0">



<qresource>


<file>images/copy.png</file>


<file>images/cut.png</file>


<file>images/new.png</file>


<file>images/open.png</file>


<file>images/paste.png</file>


<file>images/save.png</file>



</qresource>



</RCC>


We can see clearly from the example, the formatof the .qrc is xml.

Attention: Icon file should have the samepath as qrc file or in the same subdirectory.
























































































































通信人家园 (https://www.txrjy.com/) Powered by C114