Results 1 to 10 of 10

Thread: QFtp in a separate instances

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    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.

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.