Question about Qt slots and multiple calls

I am just learning Qt and asking a very simple question.

If there is a variable in the slot (scope function), and the slot is called several times, each time before the last call returns (is this possible?), Will the variable be overwritten every time? In the sense that if the slot was called before the previous start came back, would there not be an error?

Thank.

+3
source share
3 answers

If you really use function scope variables, that doesn't matter. Example:

class WheelSpinner : public QThread
{
Q_OBJECT;
public:
    WheelSpinner( QObject* receiver, const char* slot )
    {
        connect( this, SIGNAL( valueChanged( int ) ), receiver, slot,
                 Qt::DirectConnect );
    }

    void run()
    {
        for ( int i = 0; i < 100000; ++i )
        {
            emit ( valueChanged( i ) );
        }
    }

public signals:
    void valueChanged( int value );
};

class ProgressTracker : public QObject
{
Q_OBJECT;
public:
    ProgressTracker() { }

public slots:
    void updateProgress( int value )
    {
        // While in this function, "value" will always be the proper 
        // value corresponding to the signal that was emitted.
        if ( value == 100000 )
        {
            // This will cause us to quit when the *first thread* that 
            // emits valueChanged with the value of 100000 gets to this point.
            // Of course, other threads may get to this point also before the 
            // program manages to quit.
            QApplication::quit();
        }
    }
};

int main( int argc, char **argv )
{
    QApplication app( argc, argv );
    ProgressTracker tracker;
    WheelSpinner spinner1( &tracker, SLOT( updateProgress( int  ) ) );
    WheelSpinner spinner2( &tracker, SLOT( updateProgress( int  ) ) );
    WheelSpinner spinner3( &tracker, SLOT( updateProgress( int  ) ) );

    spinner1.run();
    spinner2.run();
    spinner3.run();

    return ( app.exec() );
}
+1
source

Yes, if the calls are made from different threads And you are using a direct connection.

, , , . (, Idan K).

QMutexLocker, .

+2

While the reentrant function , there are no problems.

+1
source

Source: https://habr.com/ru/post/1726966/


All Articles