【问题标题】:QProgressbar and QNetworkReply signalsQProgressbar 和 QNetworkReply 信号
【发布时间】:2010-01-16 12:23:35
【问题描述】:

我正在使用 Qt 框架用 C++ 编写应用程序。 它应该通过 http 下载文件并使用 QProgressbar 显示下载进度 - 但我没有让这部分工作!

示例代码:

QProgressBar* pbar = new QProgressBar();
//calls the website and returns the QNetworkReply*
QNetworkReply* downloader = Downloader->getFile(); 

connect(downloader, SIGNAL(downloadProgress(qint64,qint64)), pbar, SLOT(setValue(int)));

如果我运行我的代码,会出现以下错误:

QObject::connect: Incompatible sender/receiver arguments
QNetworkReplyImpl::downloadProgress(qint64,qint64) --> QProgressBar::setValue(int)

但是the Qt docs for QNetworkReply 说:

该信号适用于连接QProgressBar::setValue(),更新提供用户反馈的QProgressBar。

我的代码有什么问题,如何让它工作? 我在 Linux 下运行 Qt 4.5.3。

感谢您的帮助,对不起我的英语!

【问题讨论】:

    标签: c++ qt networking qt4


    【解决方案1】:

    是的,没错,你必须在你的 SIGNAL/SLOT 方法中设置匹配的参数......无论如何,在 Qt 示例和演示中,你可以在示例“FTP 客户端”中找到以下代码:

    connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)), this, SLOT(updateDataTransferProgress(qint64, qint64)));
    

    ...

    void FtpWindow::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes)
    {
        progressDialog->setMaximum(totalBytes);
        progressDialog->setValue(readBytes);
    }
    

    您可以复制该部分并以这种方式更新进度条...

    因此我建议:

    connect(downloader, SIGNAL(downloadProgress(qint64,qint64)), pbar, SLOT(updateDataTransferProgress(qint64,qint64)));
    

    希望对你有帮助!

    更多信息:http://qt.nokia.com/doc/4.6/network-qftp.html

    【讨论】:

    • 您好,谢谢您的回答!它应该以这种方式工作,但我认为它不像没有“连接器”的解决方案那样“性感”。也许它只是不可能的。 ://
    • 好吧,既然他们也在使用这种方式,我认为这是不可能的,至少目前是这样!很高兴它对您有所帮助...
    【解决方案2】:

    我引用文档:

    在特殊情况下,如果一个信号有更多 参数比它的插槽 连接到,额外的 参数被简单地忽略:

    connect(ftp,
    SIGNAL(rawCommandReply(int, const
    QString &)),
            this, SLOT(checkErrorCode(int)));
    

    在您的情况下,问题实际上是参数类型不匹配,因为 qint64 != int 并且没有包装器或类型转换就不可能完成任务。

    【讨论】:

      猜你喜欢
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2013-06-20
      相关资源
      最近更新 更多