I'm sorry about that. Here are some excerpts which should be relevant. If you would like to see the whole program, I can attach it.
Below is what happens when the user pressed the load button. The XML constructor executes (downloading and saving file), following by getNumImages() to see how many images there are. The rest works fine.
void WallpaperSelection::on_loadButton_clicked()
{
//ui->loadButton->setEnabled(false);
//creating new xmlReader
xml = new xmlReader(externalSettings);
//getting number of images from the XML file
howMany = xml->getNumImages();
//outputting number of images
qDebug(numImages.toAscii());
//setting image objects
image = new imageEncapsulation[howMany];
//loop gets each image's name and URL
for(int i = 0; i < howMany; i++) //
{
qDebug("entered images loop");
//sets image URL and name. Parameters passed by reference
xml->xmlData(uRL, name, i);
qDebug("requested image");
//now with the paremeters sent back, we set the object variables
image[i].setimageName(name);
image[i].setimageURL(uRL);
//outputting newly-acquired information
qDebug("name is " + name.toAscii() + " url is " + uRL.toAscii());
//inserting each image name into the list widget. Only text, no other info
ui->listWidget->insertItem(i,image[i].imageName());
}
}
void WallpaperSelection::on_loadButton_clicked()
{
//ui->loadButton->setEnabled(false);
//creating new xmlReader
xml = new xmlReader(externalSettings);
//getting number of images from the XML file
howMany = xml->getNumImages();
//outputting number of images
QString numImages = QString::number(howMany);
qDebug(numImages.toAscii());
QString uRL, name;
//setting image objects
image = new imageEncapsulation[howMany];
//loop gets each image's name and URL
for(int i = 0; i < howMany; i++) //
{
qDebug("entered images loop");
//sets image URL and name. Parameters passed by reference
xml->xmlData(uRL, name, i);
qDebug("requested image");
//now with the paremeters sent back, we set the object variables
image[i].setimageName(name);
image[i].setimageURL(uRL);
//outputting newly-acquired information
qDebug("name is " + name.toAscii() + " url is " + uRL.toAscii());
//inserting each image name into the list widget. Only text, no other info
ui->listWidget->insertItem(i,image[i].imageName());
}
}
To copy to clipboard, switch view to plain text mode
Overloaded constructor for xmlReader:
xmlReader::xmlReader(QString& newURL) //overloaded constructor
{
xmlURL = newURL;
//new network access manager, when request is finished, it goes to the slow below
m_netwManager = new QNetworkAccessManager(this);
connect(m_netwManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_netwManagerFinished(QNetworkReply*)));
QUrl url
(xmlURL.
toAscii());
//getting the request
QNetworkRequest request(url);
m_netwManager->get(request);
qDebug("I'm done with the xmlReader constructor");
qDebug("I requested the URL " + xmlURL.toAscii());
}
xmlReader::xmlReader(QString& newURL) //overloaded constructor
{
xmlURL = newURL;
//new network access manager, when request is finished, it goes to the slow below
m_netwManager = new QNetworkAccessManager(this);
connect(m_netwManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_netwManagerFinished(QNetworkReply*)));
QUrl url(xmlURL.toAscii());
//getting the request
QNetworkRequest request(url);
m_netwManager->get(request);
qDebug("I'm done with the xmlReader constructor");
qDebug("I requested the URL " + xmlURL.toAscii());
}
To copy to clipboard, switch view to plain text mode
Slot for when the image is finished() downloading:
xmlReader::xmlReader(QString& newURL) //overloaded constructor
{
xmlURL = newURL;
//new network access manager, when request is finished, it goes to the slow below
m_netwManager = new QNetworkAccessManager(this);
connect(m_netwManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_netwManagerFinished(QNetworkReply*)));
QUrl url
(xmlURL.
toAscii());
//getting the request
QNetworkRequest request(url);
m_netwManager->get(request);
qDebug("I'm done with the xmlReader constructor");
qDebug("I requested the URL " + xmlURL.toAscii());
}
void xmlReader::slot_netwManagerFinished(QNetworkReply *reply)
{
//error handling
qDebug("I've entered the request finished slot of the xmlReader");
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error in" << reply->url() << ":" << reply->errorString();
reply->deleteLater();
return;
}
//redirection, if needed
QVariant redir
= reply
->attribute
(QNetworkRequest
::RedirectionTargetAttribute);
if (redir.isValid()) {
QUrl url
= redir.
toUrl();
qDebug() << "must go to:" << url;
if (url.isRelative()) {
url.setScheme(reply->url().scheme());
url.setEncodedHost(reply->url().encodedHost());
}
QNetworkRequest req(url);
m_netwManager->get(req);
reply->deleteLater();
return;
}
qDebug() << "ContentType:" << reply->header(QNetworkRequest::ContentTypeHeader).toString();
//saving the image
QFile file("Settings.xml");
file.write(reply->readAll());
file.close();
qDebug("Saved settings.xml");
reply->deleteLater();
}
xmlReader::xmlReader(QString& newURL) //overloaded constructor
{
xmlURL = newURL;
//new network access manager, when request is finished, it goes to the slow below
m_netwManager = new QNetworkAccessManager(this);
connect(m_netwManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_netwManagerFinished(QNetworkReply*)));
QUrl url(xmlURL.toAscii());
//getting the request
QNetworkRequest request(url);
m_netwManager->get(request);
qDebug("I'm done with the xmlReader constructor");
qDebug("I requested the URL " + xmlURL.toAscii());
}
void xmlReader::slot_netwManagerFinished(QNetworkReply *reply)
{
//error handling
qDebug("I've entered the request finished slot of the xmlReader");
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error in" << reply->url() << ":" << reply->errorString();
reply->deleteLater();
return;
}
//redirection, if needed
QVariant redir = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (redir.isValid()) {
QUrl url = redir.toUrl();
qDebug() << "must go to:" << url;
if (url.isRelative()) {
url.setScheme(reply->url().scheme());
url.setEncodedHost(reply->url().encodedHost());
}
QNetworkRequest req(url);
m_netwManager->get(req);
reply->deleteLater();
return;
}
qDebug() << "ContentType:" << reply->header(QNetworkRequest::ContentTypeHeader).toString();
//saving the image
QFile file("Settings.xml");
file.open(QIODevice::WriteOnly);
file.write(reply->readAll());
file.close();
qDebug("Saved settings.xml");
reply->deleteLater();
}
To copy to clipboard, switch view to plain text mode
And lastly, getNumImages():
int xmlReader::getNumImages()
{
QFile settingsFile
("Settings.xml");
qDebug("at get Num images");
{
qDebug("opened settings file");
int temp = parseForNum(settingsFile.readAll().data());
settingsFile.close();
qDebug("there are so many images: " + tempToString.toAscii());
return temp;
}
}
int xmlReader::getNumImages()
{
QFile settingsFile("Settings.xml");
qDebug("at get Num images");
if(settingsFile.open(QIODevice::ReadOnly))
{
qDebug("opened settings file");
int temp = parseForNum(settingsFile.readAll().data());
QString tempToString = QString::number(temp);
settingsFile.close();
qDebug("there are so many images: " + tempToString.toAscii());
return temp;
}
}
To copy to clipboard, switch view to plain text mode
Thank you
Bookmarks