0

我正在尝试编写一段代码以使用带有 Qt 的 c++ 连接到 FTP 服务器,但我看到了一些奇怪的结果。代码如下

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;
}

现在让奇怪的开始......注意我已经重复了这connectToHost行。出于某种原因,如果我只在它说“即使你给了我一个命令我也没有命令...... WTF !!!”时才打那个电话 只有当我两次调用时才会hasPendingCommands()返回true。此外,无论我拨打一次还是两次,commandStarted(int)都不会发出信号。

有谁知道为什么会发生这种情况或如何解决?在此先感谢大家:-)。

4

1 回答 1

0

QFtp::hasPendingCommands的文档

正在执行的命令不被视为计划命令。

因此,在调用 to 后立即为 false 是正常的connectToHost()

对于连接到commandStarted()从未调用过的槽的问题,从代码中很难说,但一个典型的原因是你的代码没有返回到Qt的主事件循环。QFtp 是异步的,所以如果 Qt 事件循环没有运行,什么也不会发生。

于 2012-05-16T11:47:35.890 回答