What I do wrong ? I always have in debug console-"error! cannot load data". Can anyone point out an error? Maybe I create bad "settings" ? It's a permission program problem?
//main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>
#include <QSettings>
#include "settings.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/kon/main.qml"));
Settings* settings = new Settings();
viewer.rootContext()->setContextProperty("settings", settings);
viewer.showExpanded();
return app.exec();
}
//settings.h
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QObject>
#include <QSettings>
class Settings : public QSettings {
Q_OBJECT
public:
Settings(QObject *parent = 0);
virtual ~Settings();
Q_INVOKABLE
void setValue(const QString &key, const QVariant &value);
Q_INVOKABLE
void setValueIfNotSet(const QString &key, const QVariant &value);
Q_INVOKABLE
QVariant value(const QString &key, const QVariant &defaultValue);
Q_INVOKABLE
bool boolValue(const QString &key, const bool defaultValue);
Q_INVOKABLE
void initToDefaults();
signals:
void settingChanged(const QString& key);
};
#endif // SETTINGS_H
//settings.cpp
#include "settings.h"
Settings::Settings(QObject* parent) :
QSettings(parent) {
}
Settings::~Settings() {
}
QVariant Settings::value(const QString &key, const QVariant &defaultValue = QVariant()) {
return QSettings::value(key, defaultValue);
}
bool Settings::boolValue(const QString &key, bool defaultValue) {
return QSettings::value(key, defaultValue).toBool();
}
void Settings::setValue(const QString &key, const QVariant &value) {
// change the setting and emit a changed signal
// (we are not checking if the value really changed before emitting for simplicity)
QSettings::setValue(key, value);
emit settingChanged(key);
}
void Settings::setValueIfNotSet(const QString &key, const QVariant &value) {
// change the setting and emit a changed signal
if( !QSettings::contains(key) ) {
QSettings::setValue(key, value);
// (we are not checking if the value really changed before emitting for simplicity)
emit settingChanged(key);
}
}
void Settings::initToDefaults() {
setValueIfNotSet("test", true);
}
And in QML i use this class like that:
Button
{
id:button1
nazwa: "Set value"
onClicked: settings.setValue("1","adskmmads")
}
Button
{
id:button2
onClicked: console.log(settings.value("1","error! cannot load data"))
nazwa: "Load value and show"
}
QSettings constuctor needs at least 2 strings organization name and application name
QSettings::QSettings(const QString & organization, const QString & application = QString(), QObject * parent = 0);
In your case just use this code below in your main()
(1)If you use QSettings from many places in your application, you might want to specify the organization name and the application name using QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(), and then use the default QSettings constructor:
QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setOrganizationDomain("mysoft.com");
QCoreApplication::setApplicationName("Star Runner");
...
QSettings settings; //default constructor
(1)reference: QSettings
Related
I'm working on a simple wrapper for a IPC lib we are using.
I want to convert the events from this lib to calls on Qt slots.
Right now i have something like this:
void Caller::registerCallback(int id, QObject* reciever, const char* member)
{
_callbackMap[id] = std::make_pair(reciever, QString(member));
}
bool Caller::call(const SomeData data)
{
auto reciever = _callbackMap.value(data.id);
return QMetaObject::invokeMethod(reciever.first, reciever.second.toLocal8Bit(), Qt::QueuedConnection,
QGenericReturnArgument(),
Q_ARG(SomeData, data));
}
void Receiver::someCallback(SomeData data)
{
qDebug() << data.str;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Caller caller;
Receiver reciever;
caller.registerCallback(1, &reciever, "someCallback");
caller.call(SomeData({ "Hi", 1 }));
return a.exec();
}
struct SomeData {
QString str;
int id;
}; Q_DECLARE_METATYPE(SomeData);
This works quite well. But I don't like to register the callbacks as strings. I would prefer a compile time checking with a syntax like this:
caller.registerCallback(1, &reciever, &Reciever::someCallback);
I am aware of this implementation.
The slots I want to register always have exactly one argument and no return value.
I already found this request what could solve my problem but unfortunately this was never implemented.
Also this question doesn't help me as I'm not able to patch the moc we are using.
So is this really not possible with all the meta magic Qt is using?
EDIT:
I found a solution that works also when the Caller dose not know about the Receiver (what is actually what I need):
//Caller.h
class Caller : public QObject
{
Q_OBJECT
public:
Caller(QObject *parent = nullptr);
~Caller();
//void registerCallback(int id, QObject* reciever, const char *member);
template < class R, typename Func >
void inline registerCallback(int id, R reciever, Func callback)
{
using std::placeholders::_1;
registerCallbackImpl(id, reciever, std::bind(callback, reciever, _1));
};
bool call(const SomeData);
private:
QMap<int, std::pair<QObject *, std::function<void(SomeData)>> > _callbackMap;
void registerCallbackImpl(int id, QObject* reciever, std::function<void(SomeData)> callback);
};
//Caller.cpp
void Caller::registerCallbackImpl(int id, QObject* reciever, std::function<void(SomeData)> callback)
{
_callbackMap[id] = std::make_pair(reciever, callback);
}
bool Caller::call(const SomeData data)
{
auto reciever = _callbackMap.value(data.id).first;
auto fn = _callbackMap.value(data.id).second;
QMetaObject::invokeMethod(reciever, [reciever, fn, data]() {
std::invoke(fn, data);
fn(data);
}, Qt::QueuedConnection);
return true;
}
//main.cpp
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Caller caller;
Receiver reciever;
using std::placeholders::_1;
caller.registerCallback(2, &reciever, &Receiver::someCallback);
caller.call(SomeData({ "Hi2", 2 }));
return a.exec();
}
This soulution relies upon std::invoke and lambda.
Variant 1: use std::invoke directly instead of QMetaObject::invoke
Variant 2: use std::invoke inside a lambda, which is passed to QMetaObject::invoke
Variant 3: use MACRO instead of std::invoke in variant 2.
If you use QMetaObject::invoke you've got an option to choose connection type - Direct or Queued. In variant 1 the call is invoked immediately like in direct connection.
receiver.h
#ifndef RECEIVER_H
#define RECEIVER_H
#include <QObject>
#include <QDebug>
struct SomeData {
QString str;
int id;
};
//Q_DECLARE_METATYPE(SomeData);
class Receiver : public QObject
{
Q_OBJECT
public:
explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}
void doSmth(SomeData data) {
qDebug() << data.str;
}
signals:
};
#endif // RECEIVER_H
caller.h
#ifndef CALLER_H
#define CALLER_H
#include <QObject>
#include <QMap>
#include <utility>
#include <map>
#include "receiver.h"
#define CALL_MEMBER_FN(object,ptrToMember) ((object)->*(ptrToMember))
typedef void (Receiver::*callback)(SomeData);
class Caller : public QObject
{
Q_OBJECT
public:
explicit Caller(QObject *parent = nullptr) : QObject(parent) { }
void registerCallback(int id, Receiver* receiver, callback c)
{
auto pair = std::make_pair(receiver, c);
_callbackMap.emplace(id, pair);
}
bool call(const SomeData data)
{
auto &receiver = _callbackMap.at(data.id);
return QMetaObject::invokeMethod(receiver.first, [data, receiver] () {
// method 1
std::invoke(receiver.second, receiver.first, data);
// method 2 (better not to use a MACRO)
CALL_MEMBER_FN(receiver.first, receiver.second)(data);
}, Qt::QueuedConnection);
}
bool call_invoke(const SomeData data)
{
auto &receiver = _callbackMap.at(data.id);
std::invoke(receiver.second, receiver.first, data);
return true;
}
signals:
private:
std::map<int,std::pair<Receiver*,callback>> _callbackMap;
};
#endif // CALLER_H
main.cpp
#include <QCoreApplication>
#include "receiver.h"
#include "caller.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Caller caller;
Receiver reciever;
caller.registerCallback(1, &reciever, &Receiver::doSmth);
caller.registerCallback(2, &reciever, &Receiver::doSmth);
caller.call(SomeData({ "Hi", 1 }));
caller.call_invoke(SomeData({ "Hi2", 2 }));
return a.exec();
}
An alternative approach might be to use a suitable std::function to capture the callback and then make use of QTimer::singleShot with a zero timeout to invoke the callback in the correct context.
struct SomeData {
QString str;
int id;
};
class Caller {
public:
using task = std::function<void(SomeData)>;
void registerCallback (int id, QObject *receiver, task t)
{
_callbackMap[id] = std::make_pair(receiver, t);
}
bool call (SomeData data)
{
auto receiver = _callbackMap.value(data.id);
QTimer::singleShot(0, receiver.first, [=](){ receiver.second(data); });
return true;
}
private:
QMap<int, std::pair<QObject *, task>> _callbackMap;
};
class Receiver: public QObject {
public:
void someCallback (SomeData data)
{
qDebug() << data.str;
}
};
Then use as...
Caller caller;
Receiver receiver;
caller.registerCallback(1, &receiver, [&](SomeData d){ receiver.someCallback(d); });
caller.call(SomeData({ "Hi", 1 }));
I really don't know what's the problem. I know that i'm using the correct instance because I set this class as a context, and even better, the handler is invoked. I also pass the arguments to the c++ signal by value.
what is the problem and how to solve it?
main.cpp
websocket_session sess;
rootContext->setContextProperty("websocketSession", &sess);
const QUrl url(QStringLiteral("qrc:/main.qml"));
main.qml
Connections {
target: websocketSession;
onLoginResponseProcessed: {
console.log(logged_in, role)
}
}
websocket_session.hpp
class websocket_session : public QObject
{
Q_OBJECT
QWebSocket websocket_;
char *buffer_;
QString url_;
bool autoConnect_;
bool rememberMe_;
QString username_;
QString password_;
public:
explicit websocket_session(QObject *parent = nullptr);
~websocket_session();
Q_INVOKABLE void send(const control_messages::Request &req);
Q_INVOKABLE void init(const QString &url, const QString &username, const QString &password);
void process_message(const std::string &data);
//Messages
Q_INVOKABLE void login(const QString &username, const QString &password);
private slots:
void onConnected();
void onDisconnected();
void onTextMessageReceived(const QString &message);
void onError();
signals:
void loginResponseProcessed(bool logged_in, RoleWrapper::Role role);
void error(const QString &error);
};
RoleWrapper.h
#ifndef ROLEWRAPPER_H
#define ROLEWRAPPER_H
#include <QObject>
namespace RoleWrapper
{
Q_NAMESPACE
enum Role {
USER,
ADMIN
};
Q_ENUM_NS(Role)
}
#endif // ROLEWRAPPER_H
I saw this thread that says it's a bug: Qml - c++ signal parameters "undefined" in qml
main.qml prints:
qml: undefined undefined
If the problem is indeed a bug then how can I overcome this problem?
UPDATE
this is the code that emits the signal:
websocket_session.cpp
case LOGIN: {
LoginResponse loginResponse;
payload.UnpackTo(&loginResponse);
auto logged_in = loginResponse.loggedin();
auto role = static_cast<RoleWrapper::Role>(loginResponse.role());
std::cout << "logged_in: " << logged_in << ", role: " << loginResponse.role() << role << Role_Name(loginResponse.role()) << std::endl;
emit loginResponseProcessed(logged_in, role);
break;
}
You have to register the type:
qRegisterMetaType<RoleWrapper::Role>();
I am trying to make a very simple Qt application that will display a table that is referenced from another class. that's it.
I have class UWRegModel, which takes a pointer to a vector of vectors as a parameter for the constructor.
When I build the vector in my main method and pass a reference, everything works fine, but I need to pass a reference to a vector that is contained in another class. This class has a method to get that pointer, but when I initialize this way, the program crashes with the following error:
QWidget: Must construct a QApplication before a QWidget
I'm guessing there is some kind of problem with the reference, and that it has nothing to do with a widget being created before an application. However, there is no compile error, and that is all I'm getting.
Here is the code:
uwregmodel.h
#ifndef UWREGMODEL_H
#define UWREGMODEL_H
#include <QAbstractTableModel>
#include <vector>
#include <string>
#include <bitset>
class UWRegModel : public QAbstractTableModel
{
Q_OBJECT
public:
UWRegModel(QObject *parent = 0, std::vector<std::vector<int>> *uregs = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const ;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
};
#endif // UWREGMODEL_H
uwregmodel.cpp
#include "uwregmodel.h"
std::vector<std::vector<int>>* _uregs = NULL;
UWRegModel::UWRegModel(QObject *parent, std::vector<std::vector<int> > *uregs) :QAbstractTableModel(parent)
{
_uregs = uregs;
}
int UWRegModel::rowCount(const QModelIndex & ) const
{
return (_uregs->at(0)).size();
}
int UWRegModel::columnCount(const QModelIndex &) const
{
return _uregs->size();
}
std::string intToBits(int x){
std::bitset<32> _x(x);
return _x.to_string();
}
QVariant UWRegModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
std::string bin = intToBits((_uregs->at(index.column())).at(index.row()));
QString res = QString::fromStdString(bin);
return res;
}
return QVariant();
}
the main class
#include <QApplication>
#include <QPushButton>
#include <QtGui>
#include <QTableView>
#include <QtWidgets>
#include "uwregmodel.h"
#include "uwsimgui.h"
#include "ram.h"
#include "uwcpu.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
UWCPU cpu(32,8,8);
QMainWindow w;
QTableView tableView;
UWRegModel myModel(0,cpu.getUREGs());
tableView.setModel( &myModel );
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(&tableView);
QWidget *centralw = new QWidget;
centralw->setLayout(hbox);
w.setCentralWidget(centralw);
w.show();
return app.exec();
}
The UWCPU (uwcpu.cpp) class is very large, but here is the constructor and getUREGs method, only the relevant parts, I think:
int WORD_SIZE; //Ultraword will be WORD_SIZE^2
int NUM_BLOCKS;
int NUM_UREGS;
int NUM_REGS;
std::vector<std::vector<int>> UREG;
UWCPU::UWCPU(int _wordsize, int _uregs, int _regs){
WORD_SIZE = _wordsize;
NUM_BLOCKS = _wordsize;//Ultraword will be WORD_SIZE^2
NUM_UREGS = _uregs;
NUM_REGS = _regs;
for(int i = 0; i<NUM_UREGS; i++){
UREG.push_back(std::vector<int>(WORD_SIZE,0));
}
}
std::vector<std::vector<int>>* UWCPU::getUREGs(){
return &UREG;
}
Any help would be greatly appreciated, I've been stuck for a while and can't seem to get anywhere. I googled this pretty hard, but unfortunately it seems most people who get this error had problems with Qt libraries and versions. I don't think this is my case, I am using Qt creator, with version 5.8, and have tried running with debug and release versions and it makes no difference.
I am relatively new to C++ and the Qt Framework. I tried to implement a simple QTableView with a custom model derived from QAbstractListModel.
If I call
model->appendRow("Test");
in my main application, everything works perfectly fine. However, if I call
table->model()->appendRow("Test");
I get the following build-error:
'class QAbstractItemModel' has no member named 'appendRow'
table->model()->appendRow("Test");
^
What am I missing?
Sourcecode:
main.c
#include <QApplication>
#include <QTableView>
#include "exercises.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableView *table = new QTableView();
exercisesModel *model = new exercisesModel();
table->setModel(model);
model->appendRow("Test"); //why does this work...
table->model()->appendRow("Test"); //...and this doesn't?
table->show();
return a.exec();
}
exercises.h
#ifndef EXERCISES_H
#define EXERCISES_H
#include <QWidget>
#include <QAbstractListModel>
class exercisesModel : public QAbstractListModel
{
public:
exercisesModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
bool appendRow(QString data);
private:
QList <QString> lst;
};
#endif // EXERCISES_H
exercises.c
#include "exercises.h"
#include <QDebug>
exercisesModel::exercisesModel(QObject *parent) : QAbstractListModel(parent){
}
int exercisesModel::rowCount(const QModelIndex &parent) const{
Q_UNUSED(parent);
return this->lst.size();
}
QVariant exercisesModel::data(const QModelIndex &index, int role) const{
if(this->lst.size() != 0){
if (role == Qt::DisplayRole){
return this->lst[index.row()];
}
return QVariant();
}
return QVariant();
}
bool exercisesModel::appendRow(QString data){
int lastElemPos = this->lst.size();
beginInsertRows(QModelIndex(), lastElemPos, lastElemPos);
this->lst << data;
endInsertRows();
return true;
}
The QTableView::model() member function returns a pointer to a QAbstractItemModel. That is all the compiler knows about the type at the time you call table->model()->appendRow("Test"). It doesn't know that your derived model is what is actually being pointed to. There are a few ways this could be handled.
One way is to use a cast:
static_cast<exercisesModel*>(table->model())->appendRow("Test")
Now you are explicitly telling the compiler "I know that the actual type of the object is exercisesModel", so it can treat it appopriately.
As noted by Kuba Ober, Qt has its own cast that will verify that the type is correct
qobject_cast<exercisesModel*>(table->model())->appendRow("Test")
This is better than static_cast, but this check will only be done at run-time, and the more problems you can catch during compiling, the better.
The best option may be to keep the pointer to your exercisesModel and use that, so there is no question about what the underlying type is.
Because table->model() returns a QAbstractItemModel, and QAbstractItemModel does not have appendRow function you declared in exerciseModel, which is a child class of QAbstractItemModel. The compiler cannot know the actual type the table stored.
To access appendRow from table->model(), you need a type-casting:
exercisesModel *model = qobject_cast<exercisesModel *>(table->model());
// dynamic_cast could also be used.
Q_ASSERT(model);
model->appendRow("Test");
In my program I need to download a file, and I came across this article:
http://www.java2s.com/Code/Cpp/Qt/DownloadfromURL.htm
This code does work but it doesn't fit into my program so I re-coded it. I haven't completed it all but I've got the basics coded. However, when I test it, it pops up with a send error report window.
So far this is my code:
QtDownload.h
#include <QObject>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkReply>
class QtDownload : public QObject
{
Q_OBJECT
public:
explicit QtDownload();
~QtDownload();
void setTarget(const QString& t);
private:
QNetworkAccessManager manager;
QNetworkReply* reply;
QString target;
void connectSignalsAndSlots();
signals:
public slots:
void download();
void downloadFinished(QNetworkReply* data);
void downloadProgress(qint64 recieved, qint64 total);
};
QtDownload.cpp
#include "qtdownload.h"
#include <QUrl>
#include <QNetworkRequest>
#include <QFile>
QtDownload::QtDownload()
: QObject(0)
{
this->connectSignalsAndSlots();
}
QtDownload::~QtDownload()
{
if (reply != 0)
delete reply;
}
void QtDownload::connectSignalsAndSlots()
{
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*)));
QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
}
void QtDownload::setTarget(const QString &t)
{
this->target = t;
}
void QtDownload::downloadFinished(QNetworkReply *data)
{
QFile localFile("downloadedfile");
if (!localFile.open(QIODevice::WriteOnly))
return;
localFile.write(data->readAll());
localFile.close();
delete data;
data = 0;
}
void QtDownload::download()
{
QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
QNetworkRequest request(url);
this->reply = manager.get(request);
}
void QtDownload::downloadProgress(qint64 recieved, qint64 total)
{
}
main.cpp
#include "qtdownload.h"
#include <QTimer>
int main()
{
QtDownload dl;
dl.setTarget("http://www.java2s.com/Code/Cpp/Qt/DownloadfromURL.htm");
QTimer::singleShot(0, &dl, SLOT(download()));
}
As I said it's not completely finished but I want this part to be working before I move on.
I'm also new to Qt so any tips would be appreciated.
You're using uninitialized pointer, so it points out to nowhere. Initialize reply with NULL in your constructor.
You should connect reply after it was created (reply = manager.get(...)), not inside of your constructor.
QNetworkReply is never deleted by QNetworkManager as docs say:
Do not delete the reply object in the slot connected to this signal. Use deleteLater().
So you shouldn't call delete on QNetworkReply in finished slot.
In finished slot setting data to 0 will only set parameter value to 0, not your class member reply. It's an unneeded line of code. You should set your reply member to NULL instead.
Also you should consider writing to a file every time you get data chunk, as whole file will be buffered in memory in your current case. It may lead to huge memory usage of your software when file at pointed URL is big.
You need QCoreApplication to start the event loop for Qt4.
Something like this should work (not tested) :
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QtDownload dl;
dl.setTarget("http://www.java2s.com/Code/Cpp/Qt/DownloadfromURL.htm");
dl.download();
QObject::connect(app, SIGNAL(aboutToQuit()), app, SLOT(quit()));
return app.exec();
}
edit :: new version
I found some problems :
You don't need the custom reply, also you never set it to 0 in your constructor, so if it was never used it will delete a random piece of memory in your ~QtDownload();
you were deleting data inside QtDownload::downloadFinished, which shouldn't be done, it is handled by Qt, so it was getting deleted twice.
because of #2, you were deleting reply 3 times.
Here's the modified version :
qtdownload.h :
#include <QObject>
#include <QString>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
class QtDownload : public QObject {
Q_OBJECT
public:
explicit QtDownload();
~QtDownload();
void setTarget(const QString& t);
private:
QNetworkAccessManager manager;
QString target;
signals:
void done();
public slots:
void download();
void downloadFinished(QNetworkReply* data);
void downloadProgress(qint64 recieved, qint64 total);
};
qtdownload.cpp :
#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("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 recieved, qint64 total) {
qDebug() << recieved << total;
}
main.cpp :
#include <QtCore>
#include "qtdownload.h"
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QtDownload dl;
dl.setTarget("http://localhost");
dl.download();
//quit when the download is done.
QObject::connect(&dl, SIGNAL(done()), &app, SLOT(quit()));
return app.exec();
}
As you asked for it, some general comments:
void QtDownload::downloadFinished(QNetworkReply *data)
{
QFile localFile("downloadedfile");
if (!localFile.open(QIODevice::WriteOnly))
return;
localFile.write(data->readAll());
localFile.close();
delete data;
data = 0;
}
You read all data in one chunk. Bad for big files. Better read it incrementally.
Deleting the argument data from a slot is dangerous. You don't know whether the network manager continues to use (or delete) the object "data" points to right after it emits the finished signal. Probably you don't even have to delete the reply, if its owned by the manager, something to check the documentation for.
If opening the files fails, data is not deleted. So whatever is correct, its inconsistent. Either you leak or you have the risk of double-deletion.
localFile.write(data->readAll()) is not guaranteed to write all data at once. that's why it has a return value, which you should check, to make sure everything is written. If it returns -1, you should handle the error.
if (reply != 0)
delete reply;
Omit the if. Deleting a null pointer is safe.