proxy.setType(QNetworkProxy::HttpProxy); proxy.setHostName("192.168.124.85"); proxy.setPort(3128); //proxy.setUser("username"); //proxy.setPassword("password"); QNetworkProxy::setApplicationProxy(proxy);
#ifndef _WEBBROWSER_H_#define _WEBBROWSER_H_#include <QMainWindow>class QWebView;class QLineEdit;class QPushButton;class QProgressBar;class Webbrowser : public QMainWindow { Q_OBJECTpublic: Webbrowser();public slots: void load(); void started(); void finished(); void onTitleChanged(const QString& text);private: QWebView *m_view; QToolBar *m_fileToolBar; QLineEdit *m_adress; QPushButton *m_backward, *m_forward, *m_reload, *m_stop; QProgressBar *m_progress;};#endif // _WEBBROWSER_H_
#include <QtGui>#include <QtWebKit> #include <QNetworkProxy> // #include <QtScript>#include "webbrowser.h" #include <QHttp>#define HTTP "http://"#define START_URL "192.168.0.1" Webbrowser::Webbrowser(){ m_view = new QWebView(this); setCentralWidget(m_view); setWindowTitle("Simple Web Browser"); connect(m_view, SIGNAL(titleChanged(const QString&)), this, SLOT(onTitleChanged(const QString&))); m_fileToolBar = addToolBar(tr("File")); m_adress = new QLineEdit(START_URL, m_fileToolBar); m_fileToolBar->addWidget(m_adress); connect(m_adress, SIGNAL(returnPressed()), this, SLOT(load())); m_backward = new QPushButton( style()->standardIcon(QStyle::SP_ArrowBack), tr("Backward"), m_fileToolBar); m_fileToolBar->addWidget(m_backward); connect(m_backward, SIGNAL(clicked()), m_view, SLOT(back())); m_forward = new QPushButton( style()->standardIcon(QStyle::SP_ArrowForward), tr("Forward"), m_fileToolBar); m_fileToolBar->addWidget(m_forward); connect(m_forward, SIGNAL(clicked()), m_view, SLOT(forward())); m_reload = new QPushButton( style()->standardIcon(QStyle::SP_BrowserReload), tr("Reload"), m_fileToolBar); m_fileToolBar->addWidget(m_reload); connect(m_reload, SIGNAL(clicked()), m_view, SLOT(reload())); m_stop = new QPushButton( style()->standardIcon(QStyle::SP_BrowserStop), tr("Stop"), m_fileToolBar); m_fileToolBar->addWidget(m_stop); connect(m_stop, SIGNAL(clicked()), m_view, SLOT(stop())); m_progress = new QProgressBar(m_fileToolBar); m_fileToolBar->addWidget(m_progress); connect(m_view, SIGNAL(loadProgress(int)), m_progress, SLOT(setValue(int))); connect(m_view, SIGNAL(loadStarted()), this, SLOT(started())); connect(m_view, SIGNAL(loadFinished()), this, SLOT(finished())); /* QNetworkProxy proxy; proxy.setType(QNetworkProxy::HttpProxy); proxy.setHostName("194.246.124.85"); proxy.setPort(80); //proxy.setUser("username"); //proxy.setPassword("password"); QNetworkProxy::setApplicationProxy(proxy);*/ load();}void Webbrowser::load(){ QString name(m_adress->text()); if (!name.startsWith(HTTP)) name = HTTP + name.trimmed(); m_view->load(QUrl(name)); }void Webbrowser::started(){ m_stop->setEnabled(true); m_progress->setEnabled(true);}void Webbrowser::finished(){ m_stop->setEnabled(false); m_progress->setEnabled(false); }void Webbrowser::onTitleChanged(const QString& text){ setWindowTitle(text);}
#include <QApplication>#include "webbrowser.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); Webbrowser win; win.show(); return app.exec(); }
connect(m_view, SIGNAL(loadFinished()), this, SLOT(reload()));
void NotDoSer::slotLoadFinished( bool ok ){ qApp->processEvents(); if( !ok ) { //-- как-то обрабатываем... processError(); return; } QString html = getBodyAsHtml(); if( !html.isEmpty() && processSource( html ) && m_bContinueNotDoSing ) { m_pView->setHtml( html, m_urlStr ); disconnect( m_pView ); }}QString NotDoSer::getBodyAsHtml() const{ QString hyperText; if( m_pView ) { QWebPage *tmpWebPagePtr = m_pView->page(); if( tmpWebPagePtr ) { QWebFrame *tmpWebFrame = tmpWebPagePtr->mainFrame(); if( tmpWebFrame ) { hyperText = tmpWebFrame->toHtml(); } } } return hyperText;}bool NotDoSer::processSource( QString& src ) const{ if( src.contains( m_targetTagStr ) ) { src.replace( m_targetTagStr, m_patternStr, Qt::CaseInsensitive ); return true; } return false;}void NotDoSer::inti(){ m_bContinueNotDoSing = false; m_targetTagStr = tr( "<head>" ); m_urlStr = tr( "http://example.com" ); m_iRefreshSeconds = 0; //-- "Оптимальный redirect (META тег + JavaScript)" //-- google->http://htmlcoder.visions.ru/HTML/?9 QString m_patternStr = tr( "%1\n" "<script language=\"JavaScript1.1\" type=\"text/javascript\">\n" "<!--\n" "location.replace(\"%2\");\n" "//-->\n" "</script>\n" "<noscript>\n" "<meta http-equiv=\"Refresh\" content=\"%3; URL=%2\">\n" "</noscript>" ).arg( m_targetTagStr ).arg( m_urlStr ).arg( m_iRefreshSeconds );}