QFtp hasPendingCommand() 返回 true,但从不发出 commandStarted(int) 信

QFtp hasPendingCommands() returns true, but never emits commandStarted(int) signal

本文关键字:commandStarted int 返回 hasPendingCommand true QFtp      更新时间:2023-10-16

我正在尝试编写一段代码以使用带有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)信号。

有没有人知道为什么会发生这种情况或如何解决它?提前感谢大家:-)。

QFtp::hasPendingCommands 的文档说

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

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

对于连接到commandStarted()从未被调用的插槽的问题,很难从显示的代码中说出来,但一个典型的原因是你的代码没有返回到Qt的主事件循环。QFtp 是异步的,因此如果 Qt 事件循环不运行,则不会发生任何事情。