Results 1 to 10 of 10

Thread: QFtp in a separate instances

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFtp in a separate instances

    Can we see some code related to ftp downloading?

  2. #2
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFtp in a separate instances

    my signal and slots connection
    Qt Code:
    1. ftp = new QFtp(this);
    2. connect(ftp, SIGNAL(commandFinished(int, bool)),
    3. this, SLOT(ftpCommandFinished(int, bool)));
    4. connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
    5. this, SLOT(addToList(const QUrlInfo &)));
    6. connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
    7. this, SLOT(updateDataTransferProgress(qint64, qint64)));
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void FtpDialog::downloadFile()
    2. {
    3. m_dir = QFileDialog::getExistingDirectory(this, tr("Save To Directory"),
    4. m_dir,
    5. QFileDialog::ShowDirsOnly
    6. | QFileDialog::DontResolveSymlinks);
    7. if (m_dir.isEmpty())
    8. {
    9. return;
    10. }
    11. selectedFiles = fileList->selectedItems(); // fileList is a QTreeWidget
    12. disconnect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
    13. connect(downloadButton, SIGNAL(canceled()), this, SLOT(cancelDownload()));
    14. for (int i = 0; i < selectedFiles.count(); i++)
    15. {
    16. QTreeWidgetItem* item = selectedFiles.at(i);
    17. QIcon icon = item->icon(NameColumn);
    18. icon.addFile(":/ftpSelected.png");
    19. item->setIcon(NameColumn,icon);
    20. }
    21.  
    22. for (int i = 0 ; i < selectedFiles.count(); i++)
    23. {
    24. currentFileItem = selectedFiles.at(i);
    25. fileList->setCurrentItem(currentFileItem);
    26. QString fileName = currentFileItem->text(NameColumn);
    27. done = false;
    28. if (QFile::exists(m_dir+"/"+fileName))
    29. {
    30. QMessageBox::critical(this, tr("FTP"),
    31. tr("There already exists a file called \"%1\" in "
    32. "the target directory. For safety reason,"
    33. "Delete the existing file manually in \"%2\" !")
    34. .arg(fileName)
    35. .arg(m_dir));
    36. continue;
    37. }
    38.  
    39. file = new QFile(m_dir+"/"+fileName);
    40. if (!file->open(QIODevice::WriteOnly))
    41. {
    42. QMessageBox::information(this, tr("FTP"),
    43. tr("Unable to save the file %1: %2.")
    44. .arg(fileName).arg(file->errorString()));
    45. delete file;
    46. continue;
    47. }
    48. startTime = QTime::currentTime();
    49. ftp->get(currentFileItem->text(NameColumn), file); // getting file starts here
    50. statusLabel->setText(tr("Downloading %1...to Directory %2").arg(fileName)
    51. .arg(m_dir));
    52. downloadButton->setText("Cancel Download");
    53. while(ftp->hasPendingCommands() || ftp->currentCommand()!=QFtp::None)
    54. {
    55. qApp->processEvents();
    56. }
    57. }
    58. selectedFiles.clear();
    59. downloadButton->setText("Download");
    60. disconnect(downloadButton, SIGNAL(canceled()), this, SLOT(cancelDownload()));
    61. connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
    62. }
    63.  
    64. void FtpDialog::ftpCommandFinished(int, bool error)
    65. {
    66. setCursor(Qt::ArrowCursor);
    67.  
    68. if (ftp->currentCommand() == QFtp::ConnectToHost)
    69. {
    70. if (error)
    71. {
    72. QMessageBox::information(this, tr("FTP"),
    73. tr("Unable to connect to the FTP server "
    74. "at %1. Please check that the host "
    75. "name is correct.")
    76. .arg(ftpServerLineEdit->text()));
    77. connectOrDisconnect();
    78. return;
    79. }
    80.  
    81. statusLabel->setText(tr("Logged onto %1.")
    82. .arg(ftpServerLineEdit->text()));
    83. fileList->setFocus();
    84. downloadButton->setDefault(true);
    85. connectButton->setEnabled(true);
    86. return;
    87. }
    88. if (ftp->currentCommand() == QFtp::Close)
    89. {
    90. statusLabel->setText(tr("Disconnected from %1.")
    91. .arg(ftpServerLineEdit->text()));
    92. return;
    93. }
    94.  
    95.  
    96. if (ftp->currentCommand() == QFtp::Login)
    97. ftp->list();
    98.  
    99. if (ftp->currentCommand() == QFtp::Get)
    100. {
    101. if (error)
    102. {
    103. statusLabel->setText(tr("Canceled download of %1.")
    104. .arg(file->fileName()));
    105. file->close();
    106. file->remove();
    107.  
    108. selectedFiles.clear();
    109. done = true;
    110. }
    111. else
    112. {
    113. statusLabel->setText(tr("Downloaded %1 ")
    114. .arg(file->fileName()));
    115. file->close();
    116. done = true;
    117. previousTime = QTime(0,0,0,0);
    118. previousReadBytes = 0;
    119. currentFileItem->setText(DownloadStatusColumn,QString("%1")
    120. .arg(100));
    121. currentFileItem->setIcon(NameColumn,QPixmap(":/ftpDownloaded.png"));
    122. }
    123. delete file;
    124. enableDownloadButton();
    125. }
    126. else if (ftp->currentCommand() == QFtp::List)
    127. {
    128. if (isDirectory.isEmpty())
    129. {
    130. fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>")));
    131. fileList->setEnabled(false);
    132. }
    133. }
    134. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by baray98; 10th April 2008 at 17:53.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFtp in a separate instances

    Why are you using processEvents()? Why not rely on QFtp's signals?

  4. #4
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFtp in a separate instances

    like what for example... it thought there was nothing wrong in that but can you elaborate on the logic of not using processEvents().

    baray98

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFtp in a separate instances

    You are practically spinning a local event loop effectively stopping the flow and probably preventing your other window from executing its code. You should really use an event controlled approach.

    You should connect to its signals, queue requests and return from the function. When QFtp is done doing its job, it will call your slot so can do everything you want to do.

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.