2012-05-16 78 views
0

我想寫一段代碼連接到一個FTP服務器使用Qt與c + +,我看到一些奇怪的結果。代碼如下所示QFtp hasPendingCommands()返回true,但從不發出commandStarted(int)信號

void eos::WeatherRadar::connectToServer() 
{ 
    m_bomServer = new QFtp; 
    connect(m_bomServer, SIGNAL(commandStarted(int)), this, SLOT(serverConnectStarted())); 
    connect(m_bomServer, SIGNAL(commandFinished(int, bool)), this, SLOT(serverConnectFinished())); 

    m_bomServer->connectToHost("ftp.bom.gov.au"); 
    m_bomServer->connectToHost("ftp.bom.gov.au"); 

    if (m_bomServer->hasPendingCommands()) 
     std::cout << "I have commands to execute..." << std::endl; 
    else 
     std::cout << "I have no commands even though you just gave me one... WTF!!!" << std::endl; 
} 

現在讓wierdness開始...請注意,我已經重複了connectToHost線。出於某種原因,如果我只是打電話說「我沒有命令,即使你給了我一個...... WTF !!!」只有當我撥打兩次電話時,hasPendingCommands()纔會返回true。此外,無論我是否撥打此電話一次或兩次,commandStarted(int)信號都不會發射。

有沒有人有任何想法爲什麼會發生這種情況或如何解決它?在此先感謝球員:-)。

+0

因此,一些進一步的調試顯示該命令正在開始,但執行從未完成。我嘗試連接的ftp服務器肯定存在。 – DrBards

回答

0

使您的QFtp對象成爲您類的成員變量。把你的信號插槽連接到你的信頭後,它會起作用。

+0

對不起,我應該明確指出QFtp對象是該類的成員。 – DrBards

0

doc爲QFtp :: hasPendingCommands說

正在執行該命令不被視爲一個計劃 命令。

因此在撥打connectToHost()之後立即爲假是正常的。

對於連接到commandStarted()的插槽問題從未被調用,很難從所示的代碼中說出,但一個典型的原因是您的代碼不會返回到Qt的主事件循環。 QFtp是異步的,所以如果Qt事件循環沒有運行,沒有任何反應。

相關問題