Search code examples
c++qtsignalssignals-slots

How to deal with the SIGNAL(with arguments) of dynamically added QObject in QT?


I've found out that QSignalMapper can deal with the SIGNALs with NO Arguments, but how can I deal with some SIGNALs with it's arguments.

The actually problem is, I have created some QProgressBar dynamically, and i wanted to use QNetworkReply's downloadProgress(qint64, qint64) to update the bar, then the problem occurred.


Solution

  • The problem is that QNetworkReply's downloadProgress(qint64, qint64) and QProgressBar's slots are incompatable in any way. And signal mapper will not help you in this case, that is too specific.

    You have to make you own adapter class:

    class Adapter: public QObject
    {
        Q_OBJECT
    public:
        explicit Adapter(QProgressBar* bar, const QNetworkReply* reply):QObject(bar)
        {
            connect(reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(changeProgress(qint64,qint64)));
        }
    
    private slots:
        void changeProgress(qint64 progress, qint64 total)
        {
            QProgressBar* bar = static_cast<QProgressBar*>(parent());
            bar->setMaximum(total);
            bar->setValue(progress);
        }
    
    };