-1

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    Please edit your question to show the relevant code. Preferably a [mcve]. – G.M. Aug 13 '18 at 07:32
  • 2
    Sounds like you might be blocking the event loop with your `while` loop. Difficult to say without seeing your code. – thuga Aug 13 '18 at 07:34
  • if you want help you must provide a **[mcve]** – eyllanesc Aug 13 '18 at 08:00
  • What @thuga said, + as a first workaround you might want to call your `read()` calls through the event loop via `QTimer::singleShot(0, ...)` so at least you don't block the UI. – Peter Ha Aug 13 '18 at 08:19
  • so exactly where do I need to put QTimer.. inside while loop? – shweta srivastav Aug 13 '18 at 08:46
  • Where does the `size = 10000` come from? I'm guessing `reply` is of type `QNetworkReply *`? If so, you should `connect` to its [`QIODevice::readyRead`](http://doc.qt.io/qt-5/qiodevice.html#readyRead) signal and read data as/when it becomes available *or* after you've received the `finished` signal make a single call to [`QIODevice::readAll`](http://doc.qt.io/qt-5/qiodevice.html#readAll). – G.M. Aug 13 '18 at 09:21
  • Yes, I am calling sendFinished() after I have recieved finished signal. But my program demands read bytes in chunks thats why I can't use readAll. and thats why I need this in a loop. (size has been fixed manually as thats the requirement) – shweta srivastav Aug 13 '18 at 09:25
  • use `file.write(reply->read(size));` – eyllanesc Aug 13 '18 at 10:00
  • while loop is a necessity, I can't change that. Please tell me the reason why close button goes inactive. I guess I will find a solution after that.. – shweta srivastav Aug 13 '18 at 10:17
  • @shwetasrivastav your *necessary loop* may take a long time to run for a GUI, so it will not allow you to hear other events as it is happening. Why is the loop necessary? Do you want to show the progress of the download? – eyllanesc Aug 13 '18 at 10:31
  • okay. yes I want to show each filename as I am downloading a folder of very big size and downloading it filewise that too in small chunks with the help of php script. – shweta srivastav Aug 13 '18 at 11:00
  • and this isnt a http download program. I guess there s no solution then :( I have to close terminal to abort it in between. – shweta srivastav Aug 13 '18 at 11:02
  • If you must write it in small chunks, you can either replace your `while` loop with a `QTimer` or use threads. – thuga Aug 14 '18 at 07:53
  • okay I will try. Thanks! – shweta srivastav Aug 14 '18 at 09:12

1 Answers1

0

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

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

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97