Uzun zamandır PIC web server'e Mini2440 içinden basit bir browserle ulaşan bir uygulama için vakit bulamıyordum. Nihayet projeyi tamamladım. QT-4.7.3 altında yazdığım browserin kodları aşağıda. Derlemek için WebKit kütüphanesinin yüklü olması gerekir. PIC tarafındaki kod ise Microchip stack kullanıyor.
İşte kodlar;
***browser.h***
#ifndef BROWSER_H
#define BROWSER_H
#include "QApplication"
#include "QtGui"
#include "QtCore"
#include "QtWebKit/QWebView"
class Browser :
public QMainWindow
{
Q_OBJECT
public:
Browser();
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
***browser.cpp***
#include "browser.h"
#include "QtWebKit/QWebView"
#include "QUrl"
Browser::Browser()
{
pageView = new QWebView(this);
pageView->load(QUrl("http://192.168.1.52/"));
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)));
}
void Browser::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 Browser::createToolBars()
{
navigationToolBar = addToolBar(tr("Navigation"));
navigationToolBar->addAction(backAct);
navigationToolBar->addAction(forwardAct);
navigationToolBar->addAction(reloadAct);
navigationToolBar->addWidget(pageLine);
}
void Browser::createStatusBar()
{
statusBar()->showMessage(tr("Ready"));
}
void Browser::loadPage()
{
pageView->load(QUrl(pageLine->text()));
}
void Browser::backPage()
{
pageView->back();
}
void Browser::forwardPage()
{
pageView->forward();
}
void Browser::reloadPage()
{
pageView->reload();
}
void Browser::refreshLine()
{
pageLine->setText(pageView->url().toString());
}
void Browser::loadLinkPage(const QUrl & url)
{
pageView->load(url);
}
***main.cpp***
#include "QApplication"
#include "QtGui"
#include "QtCore"
#include "browser.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Browser myBrowser;
myBrowser.show();
return app.exec();
}