QProgressbar 和 QNetworkReply 信号

发布于 2024-08-18 06:05:56 字数 874 浏览 4 评论 0 原文

我正在使用 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)

但是 QNetworkReply 的 Qt 文档 说:

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

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

感谢您的帮助,并对我的英语表示抱歉!

i'm writing an application in C++ with the Qt Framework.
It should download a File over http and display the download progress with a QProgressbar - but I don't get that part to work!

Sample code:

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

If I run my code, the following error occurs:

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

But the Qt docs for QNetworkReply say:

This signal is suitable to connecting to QProgressBar::setValue() to update the QProgressBar that provides user feedback.

What is wrong with my code and how do I get it working?
I'm running Qt 4.5.3 under Linux.

Thanks for help and sorry for my english!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭⌒浅淡时光〆 2024-08-25 06:05:56

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

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

Yeah, it's right, you have to set matching arguments in your SIGNAL/SLOT methods... Anyway, in the Qt Examples And Demos, you can find the following code in the exemple "FTP Client" :

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

...

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

You could copy that part and update your progress bar this way...

I would therefore propose :

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

I hope it helps you !

More info : http://qt.nokia.com/doc/4.6/network-qftp.html

风吹过旳痕迹 2024-08-25 06:05:56

我引用文档:

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

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

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

I quote the documentation:

Exceptionally, if a signal has more
parameters than the slot it is
connected to, the additional
parameters are simply ignored:

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

In your case the problem actually was that parameter types didn't match since qint64 != int and it is impossible to accomplish the task without wrapper or type cast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文