Signal to a custom Slot.



  • Hello folks, I'm trying to connect a signal from a thread, to a custom Slot in the MainWindow.
    That is my idea:

    In the thread:
    //*******************************************
    char msg[] ="Send info\n";
    emit xtring(msg);
    //*******************************************
    in MainWindow , i have

    //*******************************************
    connect(mnC,SIGNAL(xtring(QString)),this,SLOT(filterSignal(QString received)),Qt::QueuedConnection);
    (in constructor)
    //*******************************************

    and the function
    //*******************************************
    void MainWindow::filterSignal(QString received){

    string rcv = received.toStdString().c_str();
    cerr << rcv << endl;
    

    }
    //*******************************************
    The console, don't show rcv. I don't undestand why don't works.
    Please, can somebody help me?

    Thanks in advance


  • Qt Champions 2016

    @yczo said:

    connect(mnC,SIGNAL(xtring(QString)),this,SLOT(filterSignal(QString received)),Qt::QueuedConnection);
    (in constructor)

    I would expect
    SLOT(filterSignal(QString)),
    (without the actual parameter name)

    also you can do
    qDebug() << "con:" << connect(mnC,xxxx)
    to see if it returns true for the connection.



  • The connection shows false :-(

    but why????

    I have
    //*********************************************
    connect(mnC, SIGNAL(xtring(QString)),ui->tBr_2, SLOT(append(QString)),Qt::QueuedConnection);

    qDebug() << "con: " << connect(mnC,SIGNAL(xtring(QString)),this,SLOT(filterSignal(QString)),Qt::QueuedConnection);
    

    //**************************************************

    the first connection is from the same source to a text browser and works good... Why the second to the custom slot not?

    Thank you very much for your help


  • Qt Champions 2016

    @yczo said:

    mnC

    u dont show this class.
    Does it both have Q_OBJECT macro and a
    public signal called
    void xtring(QString xx);

    ?



  • that is the class header

    class mngCOM : public QThread{
    Q_OBJECT
    public:
    bool exit = false;

    void sendMSG(char *msg, short nSend); //ret 0 if err
    char *receiveMSG();
    mngCOM(QObject *parent);
    int searchPort();
    ~mngCOM();
    void run();
    

    signals:
    void xtring(QString xtr);
    void cnxCom(QString cnxC);
    private:
    HANDLE handlerP;
    DWORD bytesRW, bEvent; //0 initialize muss
    char bufferPort[buffS];

    HANDLE openPort(char serialPort[],QString &cnxSt);
    

    };

    Thank you



  • I had a similiar case, when i want to connect signals/slots between threads. I had to register the metatype. Usually this should be done automatically, but maybe it will help:

    Declare the QString outside the class with (http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE)

    Q_DECLARE_METATYPE(QString)
    

    and register it in your constructor (http://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType-1)

    int id = qRegisterMetaType<QString>();
    

    Maybe it will help.



  • Thank you very much, I will take a look;

    Anymore:
    After clean the project un re-compile, I obtained the next warning

    C:\Users\ljm\workspace@Qt\project\mainwindow.h:30: warning: Function declaration MainWindow::filterSignal contains extra qualification. Ignoring as signal or slot.

    Greetings


  • Qt Champions 2016

    @yczo said:

    Function declaration MainWindow::filterSignal

    Sounds like you have

    class Mainwindow :
    MainWindow::filterSignal() {} << extra qualification
    };

    in the .h file



  • Solved! the error, was.... that i am newbie to Qt.. XD

    That in the header from function i had the name from object copied

    void MainWindow::filterSignal(QString received);

    instead of

    void filterSignal(QString received);

    but the compiler did not an error show :-(

    Greetings and thank you very much.


Log in to reply
 

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