QThread signal and slots problem
Hello,
I don't know why slot is not working...
k.h
Code:
{
Q_OBJECT
public:
void run()
{
QTimer::singleShot(0,
this,
SLOT(doTheWork
()));
exec();
}
public slots:
void doTheWork()
{
QNetworkAccessManager accessManager;
QNetworkReply* pReply;
pReply
= accessManager.
get(QNetworkRequest
(QUrl(strUrl
)));
QObject::connect(pReply,
SIGNAL(finished
()),
&eventLoop,
SLOT(quit
()));
eventLoop.exec();
delete pReply;
qDebug() << "got img";
emit setLogo(strChannel, bData);
quit(); // not needed ?
}
private:
signals:
};
{
Q_OBJECT
public:
...
public slots:
}
k.cpp
Code:
void k::img()
{
....
logoThread *logoThr = new logoThread(strU, strC);
logoThr->start();
}
}
}
}
{
qDebug() << "slot";
// show img
}
Re: QThread signal and slots problem
I guess the slot is called, but you don't receive any data. That's because get() works asynchronously! Read on how to use QNetworkAccessManager correctly. (Your posted thread class looks also very very crowded and "ugly"!)
Re: QThread signal and slots problem
I recieve data, but slot is not called.
Re: QThread signal and slots problem
I used your code (only removed the network things):
Code:
#include <QtGui>
{
Q_OBJECT
public:
{
strUrl = param1;
strChannel = param2;
}
void run()
{
QTimer::singleShot(0,
this,
SLOT(doTheWork
()));
exec();
}
public slots:
void doTheWork()
{
qDebug() << "got img";
emit setLogo(strChannel, bData);
quit(); // not needed ?
}
private:
signals:
};
{
Q_OBJECT
public:
public slots:
{
qDebug() << __FUNCTION__ << "reached";
}
};
int main(int argc, char** argv)
{
k test;
logoThread* logoThr = new logoThread("foo", "bar");
logoThr->start();
return app.exec();
}
#include "main.moc"
and the slot is called! So the error must be somewhere else in your original code. Try to strip your code down to locate the error.
Re: QThread signal and slots problem
The slot will be called in context of the main thread, not the thread represented by the QThread object which is probably not what you want as your thread is otherwise idle. If you main thread is blocked, the slot will not be called.
Re: QThread signal and slots problem
Quote:
Originally Posted by
wysota
The slot will be called in context of the main thread, not the thread represented by the QThread object which is probably not what you want as your thread is otherwise idle. If you main thread is blocked, the slot will not be called.
I agree with what wysota said. I met the same proble with using a thread to connect with Http server. And slots are called ,but reply->readAll() return empty. That issues makes me almost crazy.
Finally, I rethink about if thread really send out request.
I think NetworkAccessManager just put data and request into a queue which only main thread can send it out. So I recommand you use a QTimer in your class logoThread instead of thread ,and if so, the request can been sent and datas can be read correctly.I did so , and got datas from Http server. I think that may help you.
Re: QThread signal and slots problem
Have you looked at the documentation for moveToThread?
Re: QThread signal and slots problem
Quote:
Originally Posted by
fatjuicymole
Have you looked at the documentation for moveToThread?
yes, but I don't know where use it ;/
Re: QThread signal and slots problem
Quote:
Originally Posted by
Lykurg
I used your code (only removed the network things): and the slot is called! So the error must be somewhere else in your original code. Try to strip your code down to locate the error.
Thank you - Yes, error was somewhere else in code.