#include "mainwindow.h"int main(int argc, char *argv[]) { QApplication app(argc, argv); QLocale::setDefault(QLocale(QLocale::Russian,QLocale::RussianFederation)); QTextCodec *utfcodec=QTextCodec::codecForName("UTF-8"); QTextCodec::setCodecForTr(utfcodec); QTextCodec::setCodecForCStrings(utfcodec); MainWindow *browser = new MainWindow(); browser->setWindowIcon(QIcon("favicon.ico")); browser->resize(1060,800); browser->move(40,0); browser->show(); return app.exec();}
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QtGui>#include <QNetworkAccessManager>#include <QNetworkRequest>#include <QNetworkReply>#include <QWebView>#include <QWebFrame>class netAccessManager: public QNetworkAccessManager{ public: QNetworkReply * createRequest(Operation op,const QNetworkRequest & req,QIODevice * outgoingData){ QNetworkRequest myReq(req); myReq.setRawHeader("User-Agent","MyBrowser QtWebKit/4.8.4"); return QNetworkAccessManager::createRequest(op,myReq,outgoingData); }};class MainWindow : public QMainWindow{ Q_OBJECT public: MainWindow(); netAccessManager *netman; QWebView *view; QLineEdit *locationEdit; void doAlertPan(QString me); public slots: void sslErrorHandler(QNetworkReply* qnr, const QList<QSslError> & errlist); void changeLocation(); void tomysite(); void doPrint(QWebFrame * fr); };#endif
#include "mainwindow.h"MainWindow::MainWindow(){ view=new QWebView(this); netman=new netAccessManager(); view->page()->setNetworkAccessManager(netman); view->page()->settings()->setAttribute(QWebSettings::PluginsEnabled,true); view->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled,true); view->page()->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows,true); view->page()->settings()->setAttribute(QWebSettings::LocalStorageEnabled,true); view->page()->settings()->setAttribute(QWebSettings::PrintElementBackgrounds,false); view->page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled,true); view->page()->setForwardUnsupportedContent(true); view->page()->createStandardContextMenu(); locationEdit = new QLineEdit(this); locationEdit->setSizePolicy(QSizePolicy::Expanding,locationEdit->sizePolicy().verticalPolicy()); QToolBar *toolBar = addToolBar(tr("Navigation")); toolBar->addAction(view->pageAction(QWebPage::Back)); toolBar->addAction(view->pageAction(QWebPage::Forward)); toolBar->addAction(view->pageAction(QWebPage::Reload)); toolBar->addAction(view->pageAction(QWebPage::Stop)); QPushButton *b1=new QPushButton("My site",this); toolBar->addWidget(b1); toolBar->addWidget(locationEdit); QPushButton *b2=new QPushButton("=>",this); toolBar->addWidget(b2); connect(view->page()->networkAccessManager(),SIGNAL(sslErrors(QNetworkReply*,const QList<QSslError> & )), this,SLOT(sslErrorHandler(QNetworkReply*,const QList<QSslError> & ))); connect(view->page(),SIGNAL(printRequested(QWebFrame *)),this,SLOT(doPrint(QWebFrame *))); connect(b1,SIGNAL(clicked()),this,SLOT(tomyprog())); connect(b2,SIGNAL(clicked()),SLOT(changeLocation())); QString addr="http://mysite.ru"; locationEdit->setText(addr); QUrl url = QUrl(addr); view->load(QUrl(url)); setCentralWidget(view);}void MainWindow::doAlertPan(QString me){ QMessageBox msgBox; msgBox.setWindowIcon(QIcon("favicon.ico")); msgBox.setIcon(QMessageBox::Information); msgBox.setWindowTitle(tr("WebPage :: Сообщение")); msgBox.setText(me); msgBox.exec();}void MainWindow::tomysite(){ QString addr="https://mysite.ru"; locationEdit->setText(addr); QUrl url = QUrl(addr); view->load(url); view->setFocus();}void MainWindow::sslErrorHandler(QNetworkReply* qnr,const QList<QSslError> & errlist){ qnr->ignoreSslErrors();}void MainWindow::changeLocation(){ QUrl url = QUrl(locationEdit->text()); view->load(url); view->setFocus();}void MainWindow::doPrint(QWebFrame * fr){ QPrinter *printer=new QPrinter(); printer->setOrientation(QPrinter::Portrait); printer->setPaperSize(QPrinter::A4); QPrintDialog *dialog=new QPrintDialog(printer); if (dialog->exec()==QDialog::Accepted){ fr->print(printer); }}