main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QtGui>
#include <downloaddialog.h>
#include <xronview.h>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
QTabWidget *tab;
XronView* view;
~MainWindow();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
tab = new QTabWidget;
tab->addTab(view = new XronView,"hello");
tab->addTab(view = new XronView,"world");
setCentralWidget(tab);
}
MainWindow::~MainWindow()
{
}
xronview.h
#ifndef XRONVIEW_H
#define XRONVIEW_H
#include <QtGui>
#include <QtWebKit/QtWebKit>
#include <downloaddialog.h>
#include <QtNetwork/QtNetwork>
class XronView : public QWebView
{
Q_OBJECT
public:
XronView();
downLoadDialog* d;
~XronView();
public slots:
void down(const QNetworkRequest & req);
};
#endif // XRONVIEW_H
xronview.cpp
#include "xronview.h"
#include <QtWebKit/QtWebKit>
#include <downloaddialog.h>
XronView::XronView(): QWebView()
{
d = new downLoadDialog;
/* QWebPage::LinkDelegationPolicy policy=QWebPage::DelegateAllLinks;
this->page()->setLinkDelegationPolicy(policy);*/
this->page()->setForwardUnsupportedContent(true);
connect(this->page(),SIGNAL(downloadRequested(QNetworkRequest)),
d,SLOT(connectItem(QNetworkRequest)));
connect(this->page(),SIGNAL(downloadRequested(const QNetworkRequest &)),
this,SLOT(down(const QNetworkRequest &)));
d->show();
this->load(QUrl("http://www.upload.com.ua"));
}
void XronView::down(const QNetworkRequest & req)
{
QMessageBox msgBox;
msgBox.setText("ROBIT");
msgBox.exec();
}
XronView::~XronView()
{
}
downloaddialog.h
#ifndef DOWNLOADDIALOG_H
#define DOWNLOADDIALOG_H
#include <QDialog>
#include <downloaditem.h>
#include <QtGui>
#include <QtNetwork>
class downLoadDialog : public QDialog
{
Q_OBJECT
public:
explicit downLoadDialog(QWidget *parent = 0);
QGridLayout* layout;
downLoadItem* item;
public slots:
void connectItem(QNetworkRequest &request;);
signals:
// void recived(QUrl url);
};
#endif // DOWNLOADDIALOG_H
downloaddialog.cpp
#include "downloaddialog.h"
#include <QtGui>
#include <QString>
#include <QtNetwork>
downLoadDialog::downLoadDialog(QWidget *parent) :
QDialog(parent)
{
item = new downLoadItem;
layout = new QGridLayout;
layout->addWidget(item->_name,0,0);
layout->addWidget(item->bar,0,1);
layout->addWidget(item->speed,0,2);
layout->addWidget(item->size,1,0);
// item->init("http://dl10.upload.com.ua/file/626238646a32606e3132363a6067383137326262383c323c666931696035656a6b643863673769663c2d3c38656336333262/Blokada_2.rar");
connect(this,SIGNAL(recived(QUrl)),item,SLOT(init(QUrl)));
setLayout(layout);
}
void downLoadDialog::connectItem(QNetworkRequest &request;)
{
item->init(request);
QMessageBox msgBox;
msgBox.setText("ROBIT");
msgBox.exec();
// emit recived(link);
}
downloaditem.h
#ifndef DOWNLOADITEM_H
#define DOWNLOADITEM_H
#include <QObject>
#include <QtNetwork/QtNetwork>
#include <QFile>
#include <QString>
#include <QtGui>
class downLoadItem : public QObject
{
Q_OBJECT
public:
explicit downLoadItem(QObject *parent = 0);
QFile file;
QNetworkAccessManager *Manager;
QString link;
QString name;
QString getFileName(QUrl url);
QProgressBar* bar;
QLabel* _name;
QLabel* size;
QLabel* speed;
QLabel* time;
qint64 Recived;
qint64 Total;
QTime downloadTime;
public slots:
void slotReadyRead();
void slotError(QNetworkReply::NetworkError);
void slotFinished();
void downloadProgress(qint64,qint64);
void init(QNetworkRequest &request;);
public slots:
};
#endif // DOWNLOADITEM_H
downloaditem.cpp
#include "downloaditem.h"
downLoadItem::downLoadItem(QObject *parent) :
QObject(parent)
{
bar = new QProgressBar;
size= new QLabel("size");
time= new QLabel("time");
speed= new QLabel("speed");
_name= new QLabel("name");
}
void downLoadItem::init(QNetworkRequest &request;)
{
Manager = new QNetworkAccessManager;
// QNetworkRequest request;
// request.setUrl(QUrl(link));
QNetworkReply *reply = Manager->get(request);
Manager->head(request);
/* if ((reply->hasRawHeader("Content-Type") == true) && (reply->rawHeader("Content-Type") == "application/octet-stream"))
{*/
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(finished()), this, SLOT(slotFinished()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(downloadProgress(qint64,qint64)));
file.setFileName(getFileName(reply->url()));
file.open(QIODevice::WriteOnly);
downloadTime.start();
// }
}
void downLoadItem::downloadProgress(qint64 bRecived, qint64 bTotal)
{
bar->setValue(bRecived);
bar->setMaximum(bTotal);
size->setText(QString::number(bTotal));
double sped = bRecived * 1000.0 / downloadTime.elapsed();
speed->setText(QString::number(sped));
double timeRemaining = ((double)(bTotal - bRecived)) / sped;
time->setText(QString::number(timeRemaining));
}
QString downLoadItem::getFileName(QUrl url)
{
QString path = url.path();
QFileInfo info(path);
QString suffix = info.suffix();
QString defaultLocation = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
name += defaultLocation+QLatin1Char('/')+info.completeBaseName()+"."+suffix;
_name->setText(info.completeBaseName()+"."+suffix);
return name;
}
void downLoadItem::slotError(QNetworkReply::NetworkError)
{
}
void downLoadItem::slotFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
file.close();
reply->deleteLater();
}
void downLoadItem::slotReadyRead()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
file.write(reply->readAll());
}
Add a code snippet to your website: www.paste.org