Does the slot function in Qt perform a different thread?

In the next function, the dispatcher issues a signal finished(QNetworkReply*), then the slot function will be called getCategories(QNetworkReply*).

    void getCategories()
    {
        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(getCategories(QNetworkReply*)));

        for(int i = 0; i < stores.size(); ++i)
        {
            request.setUrl(QUrl(QString("http://www.example.com/%1").arg(stores[i].store_id)));

            manager.get(request);
        }
    }

If the second signal emitted during the first call to the slot function, Qt executes another thread to trigger the slot function in response to the second signal. And if so, is there some kind of method that allows the second call to the slot function to wait for the completion of the first call?

UPDATE

I mean, is it possible that the slot function works at the same time?

+3
source share
2 answers

. AFAIK, , . , .

, , ( ) .

, , , .

1 ( , )

main.cpp

#include <QObject>
#include <QThread>
#include <QDebug>
#include <QCoreApplication>

class MyClass : public QObject
{
    Q_OBJECT

    public:
        explicit MyClass(QObject *parent = 0) : QObject(parent), counter(0)
        {
            connect(this, SIGNAL(mySignal()),
                    this, SLOT(mySlot()), Qt::QueuedConnection);
        }

    signals:
        void mySignal();
    public slots:
        void mySlot()
        {
            if (counter >= 2) return;
            ++counter;
            qDebug() << "mySlot started";
            emit mySignal();
            QThread::msleep(1000);
            qDebug() << "mySlot quit";
        }

    private:
        int counter;

};

#include "main.moc"

int main(int argc, char **argv)
{
    QCoreApplication application(argc, argv);
    MyClass myObject;
    myObject.mySlot();
    return application.exec();
}

main.pro

TEMPLATE = app
TARGET = test
QT = core
SOURCES += main.cpp

moc -o main.moc main.cpp && qmake && make && ./test

mySlot started 
mySlot quit 
mySlot started 
mySlot quit

, , :

mySlot started
mySlot started
mySlot quit  
mySlot quit

2 ( )

main.cpp

#include <QObject>
#include <QThread>
#include <QDebug>
#include <QCoreApplication>
#include <QTimer>

class MyClass : public QObject
{
    Q_OBJECT

    public:
        explicit MyClass(QObject *parent = 0) : QObject(parent), counter(0), timer(new QTimer(this))
        {
            // Note: there is no need for queued connection in this case
            connect(this, SIGNAL(mySignal()), this, SLOT(mySlot()));
            connect(timer, SIGNAL(timeout()), this, SLOT(mySlot()));
            timer->setSingleShot(true);
            timer->start(200);
        }

    signals:
        void mySignal();
    public slots:
        void mySlot()
        {
            ++counter;
            qDebug() << "mySlot started" << counter;
            QThread::msleep(1000);
            qDebug() << "mySlot quit" << counter;
        }

    private:
        int counter;
        QTimer *timer;

};

#include "main.moc"

int main(int argc, char **argv)
{
    QCoreApplication application(argc, argv);
    MyClass myObject;
    myObject.mySlot();
    return application.exec();
}

main.pro

TEMPLATE = app
TARGET = test
QT = core
SOURCES += main.cpp

moc -o main.moc main.cpp && qmake && make && ./test

mySlot started 1 
mySlot quit 1 
mySlot started 2 
mySlot quit 2
+2

, Qt "" . , Qt , . -, , . , , .

/ : :

- , , . , , "emit signal()"; ( ).

, , parallelism/. , : , , , , - (). - , .

QNetworkAccessManager (QNAM) (*), : get(), ; QNAM ; , /, QNAM. - , , , : , , QNAM, (), , mySlot(), .

(*) , QNAM . - " ", .

+3

All Articles