Results 1 to 4 of 4

Thread: QT Thread Call - Issues

  1. #1
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    37
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanks
    8

    Default QT Thread Call - Issues

    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
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5.  
    6. CGNetwork *cgNetwork = new CGNetwork();
    7. ui->setupUi(this);
    8.  
    9. renderThread = new RenderThread(cgNetwork);
    10. renderThread->start();
    11.  
    12. connect(renderThread,SIGNAL(finished()),this,SLOT(initControls()));
    13. }
    14.  
    15. void MainWindow::initControls()
    16. {
    17. CGMainWindowUtility *pointer = CGMainWindowUtility::instance();
    18. QStringList cityitems;
    19. cityitems <<tr("All");
    20. cityitems.append(pointer->getCityList());
    21. QStringListModel *cityModel = new QStringListModel(cityitems, this);
    22. ui->cityComboBox->setModel(cityModel);
    23. }
    To copy to clipboard, switch view to plain text mode 
    RenderThread.cpp
    Qt Code:
    1. RenderThread::RenderThread(CGNetwork *cgnetwork)
    2. {
    3. cityUrl = "http://112.138.3.181/City/Cities";
    4. categoryUrl = "http://112.138.3.181/City/Categories";
    5.  
    6. }
    7. void RenderThread::run()
    8. {
    9. qDebug()<< "THREAD Started";
    10. CGNetwork *cgnetworks = new CGNetwork();
    11. cgnetworks->getCityList(cityUrl);
    12. }
    To copy to clipboard, switch view to plain text mode 
    CGNetwork.cpp
    Qt Code:
    1. void CGNetwork ::getCityList(const QUrl url)
    2. {
    3. cityGuideNetworkAccessManager = new QNetworkAccessManager(this);
    4.  
    5. qDebug()<<"connection";
    6.  
    7. connect(cityGuideNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),
    8. this, SLOT(parseCityList()));
    9.  
    10.  
    11. const QNetworkRequest cityRequest(url);
    12.  
    13. cityReply= cityGuideNetworkAccessManager->get(cityRequest);
    14.  
    15. connect(cityReply, SIGNAL(error(QNetworkReply::NetworkError)),
    16. this, SLOT(slotError()));
    17.  
    18. }
    19.  
    20. void CGNetwork::parseCityList()
    21. {
    22.  
    23. qDebug()<<"Parsing";
    24. cgParser = new CGJsonParser();
    25. cgParser->CityRead(cityReply);
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Girija; 1st October 2010 at 06:15.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: QT Thread Call - Issues

    Do you mean that CGNetwork::parseCityList() is not called?

  3. #3
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    37
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanks
    8

    Default Re: QT Thread Call - Issues

    Yes. I could not call that function. AM I going in correct way?

  4. #4
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    37
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanks
    8

    Default Re: QT Thread Call - Issues

    Hi , I changed the code little bit in the RenderThread.cpp and also the mainwindow.cpp
    Mainwindow.cpp
    Qt Code:
    1. cgNetwork->moveToThread(renderThread);
    To copy to clipboard, switch view to plain text mode 

    RenderThread.cpp
    Qt Code:
    1. void RenderThread::run()
    2. {
    3. qDebug()<< "THREAD Started";
    4. cgnetworks->getCityList(cityUrl);
    5. cgnetworks->getCategoryList(categoryUrl);
    6. emit(finished());
    7. exec();
    8. }
    To copy to clipboard, switch view to plain text mode 


    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...

Similar Threads

  1. None-GUI thread call widget method cause error
    By luochen601 in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2010, 06:39
  2. Replies: 9
    Last Post: 28th November 2009, 21:31
  3. Replies: 16
    Last Post: 7th October 2009, 09:17
  4. Replies: 6
    Last Post: 29th April 2009, 19:17
  5. Replies: 3
    Last Post: 16th May 2007, 12:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.