0

I make simple HTTP get operation.I get JSON result , then parse it. I use this operations steps in functuon.I start function with QtConcurrent::run , HttpGet Function :

    void MobileOperations::GetHTTPData(MyGlobal::Metods MetodName, QString Parameters, QMap<QString, QVariant> paramlist)
{

parameter=new HttpRequest();
parameter->url=m_url;
parameter->metodname=MetodName;
parameter->resource=m_path;
parameter->appid=m_appid;
parameter->apppass=m_apppass;
parameter->parametersname=Parameters;
parameter->params=paramlist;
rest= new RestWebservice(parameter->GenerateHTTPQuery(),MetodName);
json=new JSonParser();
loop=new QEventLoop();

      QObject::connect(rest,SIGNAL(sendhttpdata(QByteArray,MyGlobal::Metods)),&json,SLOT(onGetData(QByteArray,MyGlobal::Metods)));
 QObject::connect(&json,SIGNAL(serilazitionCompleted()),loop,SLOT(quit()));


 rest->get();



  loop->exec();


}

in here, Http get request start, then wait in loop until json result is parsed.I call this function like this:

void MobileOperations::getUserAccountT( QString kullaniciAdi,  QString   sifre)
  {

 MyGlobal::Metods metod=MyGlobal::KullaniciGiris;
 QString parameters="{UserId}/{UserPass}/{GSM}";
 QMap<QString,QVariant> paramlist;
 paramlist["UserId"]=kullaniciAdi;
 paramlist["UserPass"]=sifre;
 paramlist["GSM"]="";

 GetHTTPData(metod,parameters,paramlist);

if(json.user.IsSuccess==true)
{
emit successlogin("Login Başarılı");
emit processstop("Login Başarılı");
}
else
{
    emit processstop("Bağlantı Başarısız Oldu");
}
 qDebug()<<json.user.IsSuccess<<json.user.UserName;

 }

  void MobileOperations::getUserAccount(QString kullaniciAdi, QString  sifre)
 {
 QFutureWatcher<void> watcher;
 connect(&watcher,SIGNAL(canceled()),&watcher,SLOT(deleteLater()));

QFuture<void> t1=       QtConcurrent::run(this,&getUserAccountT,kullaniciAdi,sifre);
watcher.setFuture(t1);
emit processstart("Bağlanıyor");



    }

I use thread because of avoiding freeze screan.But thread never ends.I commet loop.exe() , thread finish but in this case I can't know json value is filled , I miss value.

Aykar Kr.
  • 21
  • 5
  • Is serilazitionCompleted signal raised? Is onGetData get called? – Fred Mar 25 '16 at 09:34
  • sendhttpdata send signal to OnGetData . After ongetdata serilazition stars, Then completed loop is quit.But When I use QtConcurrent , deadlock occurs. – Aykar Kr. Mar 25 '16 at 10:07
  • More code sample would help a lot. Otherwise you may find solution here: https://forum.qt.io/topic/11619/solved-qt-concurrent-emit-signal-from-run-method-do-not-work – Fred Mar 25 '16 at 11:39
  • I have given more detail above now.I solved problem.But there is an acception.İf I close Application , without Http get operation finished.Programs crash ?? – Aykar Kr. Mar 25 '16 at 12:57
  • You're getting the crash because the concurrent function hasn't finished. Functions run inside of `QtConcurrent::run()` can't be canceled or queried for their progress; you can only check `isFinished()`. You can solve this by connecting the main `QApplication`'s `aboutToQuit()` signal to a function that waits for the `QFuture` to finish in a loop. Alternatively, run the other function in your own `QThread`, which you have more direct control over. – bnaecker Mar 25 '16 at 14:33

0 Answers0