[Solved] Signal, Slot and QtPlugin



  • Hello,

    I've run into an odd issue, and I don't find where the problem is.
    First of all : I'm on Qt 5.2.1 with MVS2010.

    My application do some heavy stuff, so one side I have my GUI and other side I have the heavy stuff in a thread called "processeur".
    This thread use many plugins from the same interface. And I want that plugins send log messages to the GUI.
    So my thread loads and uses the plugin, the plugin emits a signal, catched by the thread, who transmit by sending another signal to the GUI.

    Here my code :

    The Interface, "FournitureBancInterface" :
    @
    #include "fourniturebancinterface_global.h"

    class FOURNITUREBANCINTERFACESHARED_EXPORT FournitureBancInterface : public QObject
    {
    Q_OBJECT

    protected:
    FournitureBancInterface();

     virtual int verification(int niveau) = 0;
    

    signals:
    void logMessage(const QString &msg);

    };

    Q_DECLARE_INTERFACE(FournitureBancInterface, "blabla.FournitureBancInterface")
    @

    The plugin "Ute" :
    @
    #include "../fourniturebancinterface/fourniturebancinterface.h"

    class Ute : public FournitureBancInterface
    {
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "blabla.FournitureBancInterface" FILE "ute.json")
    Q_INTERFACES(FournitureBancInterface)

    public:
    Ute() {}

    int verification(int niveau);
    

    };

    int Ute::verification(int niveau)
    {
    int state = codeErreurs::NO_ERROR;

    emit logMessage("NUMINIT");
    
    return state;
    

    }
    @

    And then, my thread "Processeur" :
    @
    #include "../format_banc/fourniturebancinterface/fourniturebancinterface.h"

    class Processeur : public QThread
    {
    Q_OBJECT

    public:

    Processeur(int niveau, QString adresseDir, int bancID, bool codage);
    ~Processeur() {}
    
    void run();
    

    private:
    FournitureBancInterface *loadPlugin(QString driverName);

    signals:
    void logMessage(const QString &msg);

    public slots:
    void handleLogMessage(const QString &msg);

    };

    void Processeur::handleLogMessage(const QString &msg)
    {
    emit logMessage(msg);
    }

    void Processeur::run()
    {
    FournitureBancInterface *piloteUte=NULL;

    piloteUte = loadPlugin("ute.dll");
    connect(piloteUte, SIGNAL(logMessage(QString)), this, SLOT(handleLogMessage(QString)));
    piloteUte->verification(niveauVerif);
    

    }
    @

    The code compiles, the connect() in the thread returns "true", and the signal is well emited.
    But if I place a breakpoint in Processeur::handleLogMessage(), it's never reached, as if the "processeur" never see any signal from my plugin.

    I'm working on this since 2 days, having a signal emited from the plugins catched by the "processeur". It's my second solution which compile but didn't works (and many other which didn't compile at all...) I'm totally out of idea.
    Any help?

    Regards,
    Julia.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How do you know that logMessage is correctly emitted ?

    On a side note, you can avoid the intermediate handleLogMessage slot by connecting Ute's logMessage directly to Processor logMessage



  • Thank for the reply.

    [quote author="SGaist" date="1398716338"]How do you know that logMessage is correctly emitted ?[/quote]
    I caught it with a slot, directly in the Ute plugin.

    [quote author="SGaist" date="1398716338"]On a side note, you can avoid the intermediate handleLogMessage slot by connecting Ute's logMessage directly to Processor logMessage[/quote]
    Oh, I didn't know we can do this, it's a nice thing. :)


  • Lifetime Qt Champion

    Just realized…

    Using QThread like that, at the end of run your thread is stopped. Have a look at the the latest Qt 5 QThread documentation. You'll see how to implement the worker object technique with QThread



  • Hey! Now everything works!
    Thanks!

    Yep, initialy I've implemented the "another way" described in QThread documentation.
    Now, I have implemented the thread with the worker technique. And the signal is well caught by the processor.

    I don't understand why it's works with the worker and didn't with the "another way". Is there a reason about this?

    Anyway, thanks again. :)


  • Lifetime Qt Champion

    I don't know about the rest of your program, but implementing run like that you did would just do what's written in run and then the end, the thread is already stopped since you don't have any while loop or event loop running.

    You're welcome !

    Happy coding :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.