
Originally Posted by
ttimt
So maybe here's a "simplier" form of my question.
After I do reply->readAll() in a function/method named readdata, I want to store it in a variable called "data" and use it in another function.
You can call the other function and pass data, or, if both functions are methods of the same class, store data as a member variable and access it from both methods, or if not, store data in an object that both functions have acess to.
Not really a question of Qt though, that applies to C++ in general.
Lets do an example
{
Q_OBJECT
public:
void login(const LoginData &loginData);
signals:
void receivedLoginResponse
(const QByteArray &data
);
private:
void doSomethingElse();
private:
private slots:
void loginiFinished(QNetworkReply *reply);
};
class Foo : public QObject
{
Q_OBJECT
public:
void login(const LoginData &loginData);
signals:
void receivedLoginResponse(const QByteArray &data);
private:
void doSomething(const QByteArray &data);
void doSomethingElse();
private:
QByteArray m_data;
private slots:
void loginiFinished(QNetworkReply *reply);
};
To copy to clipboard, switch view to plain text mode
void Foo::login(const LoginData &loginData)
{
// prepare QNetworkRequest
QNetworkReply *reply = networkAccessManager->get(...);
connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(loginFinished(QNetworkReply*)));
}
{
qDebug() << Q_FUNC_INFO << "data .length=" << data.length();
}
void Foo::doSomethingElse()
{
qDebug() << Q_FUNC_INFO << "m_data .length=" << m_data.length();
}
void Foo::loginFinished(QNetworkReply *reply)
{
// call a function with data
doSomething(data);
// call a method of same object, share data as member
m_data = data;
doSomethingElse();
// let some other class handle the data
emit receivedLoginResponse(data);
}
void Foo::login(const LoginData &loginData)
{
// prepare QNetworkRequest
QNetworkReply *reply = networkAccessManager->get(...);
connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(loginFinished(QNetworkReply*)));
}
void Foo::doSomething(const QByteArray &data)
{
qDebug() << Q_FUNC_INFO << "data .length=" << data.length();
}
void Foo::doSomethingElse()
{
qDebug() << Q_FUNC_INFO << "m_data .length=" << m_data.length();
}
void Foo::loginFinished(QNetworkReply *reply)
{
QByteArray data = reply->readAll();
// call a function with data
doSomething(data);
// call a method of same object, share data as member
m_data = data;
doSomethingElse();
// let some other class handle the data
emit receivedLoginResponse(data);
}
To copy to clipboard, switch view to plain text mode
Cheers,
_
Bookmarks