PDA

View Full Version : Downloading a file and saving in a path



StarRocks
27th December 2012, 05:11
Dear Forums,


Hi guys i have a problem with downloading a file......Actually i need to save a file in a path which i have done but what my question is can we save the same file in two different paths is it possible at a time (or)
is there any way to download a file i mean xml file which im generating through my code to the system path which when clicked should raise a window by showing download option.
Please let me know if any solution.Any solution would be appreciable.Thanks in Advance...


Regards,

anda_skoa
27th December 2012, 09:46
Assuming that downloading in this context means getting data from a QNetworkAccessManager::get() call.

You get the data from the QNetworkReply and you can store it in as many locations as you want.

If you open two files and write the received data into both files, it will be stored in both files.

Cheers,
_

StarRocks
27th December 2012, 10:36
Dear anda_skoa,


Thanks for the reply..........I have got a code and i have worked on and its working fine...Please find the code below:





#include "qtdownload.h"
#include <QCoreApplication>
#include <QUrl>
#include <QNetworkRequest>
#include <QFile>
#include <QDebug>

QtDownload::QtDownload() : QObject(0) {
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*)));

}

QtDownload::~QtDownload() {

}

void QtDownload::setTarget(const QString &t) {
this->target = t;
}

void QtDownload::downloadFinished(QNetworkReply *data) {
QFile localFile("/home/venugopal/downloadedfile");
if (!localFile.open(QIODevice::WriteOnly))
return;
const QByteArray sdata = data->readAll();
localFile.write(sdata);
qDebug() << sdata;
localFile.close();
// emit done();
}

void QtDownload::download() {
QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
QNetworkRequest request(url);
QObject::connect(manager.get(request), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));

}

//void QtDownload::downloadProgress(qint64 received, qint64 total) {
// qDebug() << "HI whts Received"<<received <<"HI whts total"<< total;

//}






Now what my doubt is for generating an xml file i am using a program and for downloading im using the other........When i try to call those methods in a single class by having everything at once....Its not allowing me rather giving me error messages......Please provide me an idea.....Any solution would be appreciable........Thanks in Advance.....


Regards,

amleto
27th December 2012, 15:15
what error message?

StarRocks
28th December 2012, 05:08
Dear Amleto,


Thanks for the reply..........The Error message that i am getting is "No such slot to be defined" eventhough i have given the slot etc ........And the code that i have given is the one that i have completely worked on and its working fine but when tried to do both generating and downloading its not allowing me to do.Hope you got me now......Any solution would be appreciable.....Thanks in Advance.........Please find the attachment of both the programs for your reference....


The below program is for generating an xml file in a location:




#include "generatexml.h"
#include "ui_generatexml.h"
#include<QMap>
#include<QFile>
#include<QXmlStreamWriter>
#include<QMapIterator>
#include<QMessageBox>
#include<QDebug>
#include<QDebug>
#include<QSqlDatabase>
#include<QSqlError>
#include<QSqlQuery>
#include<qdebug.h>
#include<QSqlRecord>


GenerateXML::GenerateXML(QWidget *parent) :
QMainWindow(parent),
ui_xml(new Ui::GenerateXML)
{

ui_xml->setupUi(this);
setWindowTitle("Generate Xml");

connect(ui_xml->pushButton,SIGNAL(clicked()),this,SLOT(CreateXMLFi le()));

// //**** CALLING FUNCTION *****
// CreateXMLFile();
// QObject::deleteLater();
}

GenerateXML::~GenerateXML()
{
qDebug("In Destructor---------------");
delete ui_xml;
}
void GenerateXML::CreateXMLFile()
{

QFile file("/mnt/jffs2/Synch.xml");
if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::critical(this,
"GenerateXML::parseXML",
"Couldn't open example.xml",
QMessageBox::Ok);
return;

}
else
{
qDebug("THE FILE IS READ ONLY MODE --");

qDebug()<<"**************establishing new DB connection**************";
QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE");
db2.setDatabaseName("/mnt/jffs2/apmcdb.sqlite");
bool ok1 = db2.open();
qDebug()<<"*(((((((((((((((((((("<<ok1;
if(ok1)
{
qDebug() << "Database opened";
}
else {
qDebug() << "Database not opened";
}

//*********** GETTING COLOUMN NAMES &&& DATA FROM TABLE **************
QSqlQuery fardb;
bool que=fardb.exec("SELECT * from something");
qDebug()<<"Query Check---------------"<<que;
QSqlRecord colnames=fardb.record();
qDebug()<<"no of coloumnss-----------"<<colnames.count();
qDebug()<<"colname----" <<colnames.fieldName(0);

//*********CREATING & WRITING TO XML FILE**********************
QXmlStreamWriter* xmlWriter= new QXmlStreamWriter();
xmlWriter->setDevice(&file);
xmlWriter->setAutoFormatting(true);
/*Start document */
xmlWriter->writeStartDocument();
xmlWriter->writeStartElement("MAIN");
while(fardb.next()) //entering to write row of query records
{
//*******start tag persons******
xmlWriter->writeStartElement("Record1"); //start new tag for one enitre row of record
for(int cols=0;cols<colnames.count();cols++) //gets the coloumns of the particular row
{
//writes each coloumn in new line with <colname_tag> col_value </colname_tag>

xmlWriter->writeTextElement(colnames.fieldName(cols),fardb.va lue(cols).toString());
}
xmlWriter->writeEndElement(); //end tag for row of record
//********end tag persons**********/
}//ends after writing last record row of query
xmlWriter->writeEndElement();
xmlWriter->writeEndDocument();
/*end document */
delete xmlWriter;
}

}




And the other program for downloading is as i have already given....Hope this would be a better reference...



Regards,

anda_skoa
28th December 2012, 10:53
The Error message that i am getting is "No such slot to be defined" eventhough i have given the slot etc

Check if you have all the necessary requirements:
- the C++ method
- declared in a slots section
- in a QObject subclass
- having the Q_OBJECT marker

Cheers,
_

StarRocks
28th December 2012, 11:48
Dear anda_skoa,


Thanks for the reply....I have checked everything is working fine but not knowing where the problem is .......I am sending you the complete programs of both in a zip format so that you can get an idea i guess.......Please find the attachment of the files given....Any solution would be appreciable ..........Thanks in advance...


Regards,

d_stranz
28th December 2012, 18:53
I can't see anything wrong with your code (at least the part that declares and connects to the slot). It is possible that since your UI is named "GenerateXML" and your main window class is *also* named "GenerateXML" the SLOT macro is getting confused about the scope. Try renaming your main window class, maybe something like "GenerateXMLWindow" and see if that makes any difference.

Are you certain that the connect() error message is due to the call in *your* code, and not coming from somewhere else? Typically the error message also includes the name of the signal / slot that can't be found, and you have not provided that information in your post.

StarRocks
2nd January 2013, 05:11
Dear d_stranz,


Thanks for the reply........Actually the error is now from the other project its from the same and renamed the class name as you said me to but helpless no change in the code...........I want to call the download of xml file project in the Generatingxml project because i don't want to make the code long and confused so thought to have all in a single project.........Hope you got my point and understood what i really wanted to frame the sentence in and it says the slot/signal not found but not providing me the name of the slot/signal.........Any solution would be appreciable.........Thanks in Advance......

StarRocks
2nd January 2013, 10:11
Dear d_stranz,

Please find the attachment of my updated program on generating and downloading........What my problem here is it downloading before i generate any file......It means downloading is done first.........The error that im getting here is "Object::connect: No such slot MainWindow::downloadProgress(qint64,qint64)
Object::connect: (receiver name: 'MainWindow')" ............Please have a look at the code and let me know with a solution.......Any solution would be appreciable.....Thanks in Advance........


Regards,

Santosh Reddy
2nd January 2013, 10:57
You are trying to create two MainWindows, both will run simultaneously, if you want the 2nd MainWindow to wait until first one is done then

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

w.show();
a.exec();//<<<<<<<<<<<<<<<<<<<<


MainWindow dl;
dl.setTarget("/mnt/jffs2/Synch.xml");

dl.download();

return 0;//<<<<<<<<<<<<<<<<<<<<
}

also the error you mentioned (altually warnning) is because you commented out that solt in the code

Lesiok
2nd January 2013, 10:57
Dear StarRocks - some independence please. Don't You see in mainwindow.h that line with definiton of downloadProgress slot is commented ?

StarRocks
2nd January 2013, 11:23
Dear Santosh,


Thanks for the reply.........Actually i dont need that slot to be used so commented it.....As u have given the main method i have done the same but what i need is when the generation of Xml is done right after that i want it to get downloaded in the path where i have mentioned ....And download should be done after the file is generated.........Hope you got me.....Thanks in Advance.........Any solution would be appreciable........



Regards,

Santosh Reddy
2nd January 2013, 11:31
Creating two instance of MainWindow will not help you.

You need to have two slots connected to QPushButton click signal
1. First slot will save the file
2. Second slot will download it form there to new location

Signal & Slot connection (connect() call) order is important here.

StarRocks
2nd January 2013, 11:36
Dear Santosh,


Thanks for the reply.........As you have seen the code i am pretty confused how to have the slots there .......So can you help me out in having the slots set there hope you understood my point.............As its not allowing me to have couple of slots as used by me.........Thanks in advance.........Any solution would be appreciable.....



Regards,

Santosh Reddy
2nd January 2013, 11:57
Check this

StarRocks
2nd January 2013, 12:09
Dear Santosh,

Thanks for the reply........The coding that you have given is working perfectly thankyou for the code........Thanks in Advance.........


Regards,

d_stranz
3rd January 2013, 01:21
@Santosh: If I can offer one nit-pick criticism - the signal / slot connections in the MainWindow constructor rely on some Qt behaviour which may not be obvious to the OP, namely that signals are handled in the order that the connections are made.

I would have defined a signal for the MainWindow: createXMLDone() or something like that, and connected that to the slot that creates the file copy. That would also allow some degree of control - as the code is now implemented, the copy is attempted no matter what happens in the createXMLFile() method, even when it fails and there is no xml file to copy.

Otherwise, your patience in dealing with this poster is amazing.

StarRocks
3rd January 2013, 05:00
Dear d_stranz,


Thanks for the comments but the way we have coded looks fine i guess.......And i have coded many things in the program but was lacking with somethings so Santosh helped me out in clearing those am i right santosh.......Hope you help me out in those critical solving issues............Thanks in Advance..........
Any comments would be appreciable....


Regards,

Santosh Reddy
3rd January 2013, 06:43
@Santosh: If I can offer one nit-pick criticism - the signal / slot connections in the MainWindow constructor rely on some Qt behaviour which may not be obvious to the OP, namely that signals are handled in the order that the connections are made.
I agree.


I would have defined a signal for the MainWindow: createXMLDone() or something like that, and connected that to the slot that creates the file copy. That would also allow some degree of control - as the code is now implemented, the copy is attempted no matter what happens in the createXMLFile() method, even when it fails and there is no xml file to copy.
This is a better solution, and I would also prefer this way.