PDA

View Full Version : QT Thread Call - Issues



Girija
1st October 2010, 05:09
Hi,

Please help me. I am struck-up with thread concept. Actually my problem : I want to display the cities List in the combobox. I am getting cities list from the webservice. I am using thread for update the combo box value after webserice call finished.

Here I can call the webservice. But I couldn't get the Reply.

:(:( … Thanks in advance.

I am using the following code.


MainWindow.cpp


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

CGNetwork *cgNetwork = new CGNetwork();
ui->setupUi(this);

renderThread = new RenderThread(cgNetwork);
renderThread->start();

connect(renderThread,SIGNAL(finished()),this,SLOT( initControls()));
}

void MainWindow::initControls()
{
CGMainWindowUtility *pointer = CGMainWindowUtility::instance();
QStringList cityitems;
cityitems <<tr("All");
cityitems.append(pointer->getCityList());
QStringListModel *cityModel = new QStringListModel(cityitems, this);
ui->cityComboBox->setModel(cityModel);
}


RenderThread.cpp


RenderThread::RenderThread(CGNetwork *cgnetwork)
{
cityUrl = "http://112.138.3.181/City/Cities";
categoryUrl = "http://112.138.3.181/City/Categories";

}
void RenderThread::run()
{
qDebug()<< "THREAD Started";
CGNetwork *cgnetworks = new CGNetwork();
cgnetworks->getCityList(cityUrl);
}

CGNetwork.cpp


void CGNetwork ::getCityList(const QUrl url)
{
cityGuideNetworkAccessManager = new QNetworkAccessManager(this);

qDebug()<<"connection";

connect(cityGuideNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseCityList()));


const QNetworkRequest cityRequest(url);

cityReply= cityGuideNetworkAccessManager->get(cityRequest);

connect(cityReply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError()));

}

void CGNetwork::parseCityList()
{

qDebug()<<"Parsing";
cgParser = new CGJsonParser();
cgParser->CityRead(cityReply);
}

tbscope
1st October 2010, 05:33
Do you mean that CGNetwork::parseCityList() is not called?

Girija
1st October 2010, 06:59
Yes. I could not call that function. AM I going in correct way?

Girija
1st October 2010, 08:43
Hi , I changed the code little bit in the RenderThread.cpp and also the mainwindow.cpp
Mainwindow.cpp


cgNetwork->moveToThread(renderThread);


RenderThread.cpp


void RenderThread::run()
{
qDebug()<< "THREAD Started";
cgnetworks->getCityList(cityUrl);
cgnetworks->getCategoryList(categoryUrl);
emit(finished());
exec();
}



Now, I can parse the result. But the problem , The thread finished before parsing completed...
Still the combo box is empty

How to avoid this?

Please help me...