qtqmainwindowqnetworkreply

QMainWindow close button gets inactive while QNetworkReply is reading data


Here is some idea of my code.It executes successfully. I am just not able to use close button the moment it starts executing read statement.!

inside main():


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWindow;

    mainWindow.show();
    return app.exec();

}

this is my constructor: one main window and a button that calls execute when clicked

inside MainWindow.cpp:


MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) //this creates a window that has a download button
{
    setWindowTitle("File Downloader");
    QWidget* central = new QWidget(this); 
    QVBoxLayout *lay = new QVBoxLayout(central);

    button1 = new QPushButton("Download", this);
    connect(button1, SIGNAL (clicked()),this,SLOT( execute()));         
    lay->addWidget(button1);
    manager = new QNetworkAccessManager(this);
}

this function sets up connection with php file and then calls sendFinished

void MainWindow::execute()   //PHP file link here
{
 QUrl url"/patofmyphp");
 reply = manager->post(request, data); //data is some string
 connect(reply, SIGNAL (finished()),this,SLOT( sendFinished()));
}

this function reads data from php file in chunks

void MainWindow::sendFinished()  //this gets called successfully
{
      while(copied < size)    //copied = 0, size = 10000; 
      {
          const QByteArray data = reply->read(tocopy);
          file.write(data);
          copied += data.size();
      }
}

Whole program is running successfully. But when I want to abort my reply using closebutton of QMainWindow before program gets executed successfully. But the moment it reply->read gets executed, it seems close button stops working. What should I do?


Solution

  • Looks like I solved my issue just by using this line inside my while loop.

    QApplication::processEvents(); if(close == true) { closeProgram(); }