PDA

View Full Version : QThread signal and slots problem



mero
24th April 2010, 19:01
Hello,
I don't know why slot is not working...

k.h



class logoThread : public QThread
{
Q_OBJECT
public:
logoThread(QString param1, QString param2) { strUrl = param1; QString strChannel = param2; }

void run()
{
QTimer::singleShot(0, this, SLOT(doTheWork()));

exec();
}

public slots:
void doTheWork()
{
QNetworkAccessManager accessManager;
QNetworkReply* pReply;
QEventLoop eventLoop;
pReply = accessManager.get(QNetworkRequest(QUrl(strUrl)));
QObject::connect(pReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();

QByteArray bData = pReply->readAll();

delete pReply;
qDebug() << "got img";
emit setLogo(strChannel, bData);

quit(); // not needed ?
}

private:
QString strUrl;
QString strChannel;

signals:
void setLogo(QString, QByteArray);

};

class k : public QObject
{
Q_OBJECT
public:
...

public slots:
void setLogoS(QString, QByteArray);
}


k.cpp


void k::img()
{
....
logoThread *logoThr = new logoThread(strU, strC);
QObject::connect(logoThr, SIGNAL(setLogo(QString,QByteArray)), this, SLOT(setLogoS(QString,QByteArray)), Qt::QueuedConnection);
logoThr->start();
}
}
}
}

void k::setLogoS(QString param1, QByteArray param2)
{
qDebug() << "slot";
// show img
}

Lykurg
24th April 2010, 20:51
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"!)

mero
24th April 2010, 21:17
I recieve data, but slot is not called.

Lykurg
24th April 2010, 21:31
I used your code (only removed the network things):
#include <QtGui>



class logoThread : public QThread
{
Q_OBJECT

public:
logoThread(QString param1, QString param2)
{
strUrl = param1;
strChannel = param2;
}

void run()
{
QTimer::singleShot(0, this, SLOT(doTheWork()));
exec();
}

public slots:
void doTheWork()
{
qDebug() << "got img";
QByteArray bData;
emit setLogo(strChannel, bData);
quit(); // not needed ?
}

private:
QString strUrl;
QString strChannel;

signals:
void setLogo(QString, QByteArray);
};

class k : public QObject
{
Q_OBJECT
public:
k(QObject* parent = 0) : QObject(parent) {}

public slots:
void setLogoS(QString, QByteArray)
{
qDebug() << __FUNCTION__ << "reached";
}
};

int main(int argc, char** argv)
{
QApplication app(argc, argv);

k test;
logoThread* logoThr = new logoThread("foo", "bar");
QObject::connect(logoThr, SIGNAL(setLogo(QString, QByteArray)), &test, SLOT(setLogoS(QString, QByteArray)), Qt::QueuedConnection);
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.

wysota
25th April 2010, 10:19
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.

Zmrca
26th April 2010, 14:33
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.

squidge
26th April 2010, 18:57
Have you looked at the documentation for moveToThread?

mero
28th April 2010, 08:17
Have you looked at the documentation for moveToThread?

yes, but I don't know where use it ;/

mero
8th May 2010, 01:56
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.