Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal to a custom Slot.
QtWS25 Last Chance

Signal to a custom Slot.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • yczoY Offline
    yczoY Offline
    yczo
    wrote on last edited by yczo
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • yczoY Offline
        yczoY Offline
        yczo
        wrote on last edited by yczo
        #3

        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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @yczo said:

          mnC

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

          ?

          1 Reply Last reply
          1
          • yczoY Offline
            yczoY Offline
            yczo
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • beeckscheB Offline
              beeckscheB Offline
              beecksche
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              1
              • yczoY Offline
                yczoY Offline
                yczo
                wrote on last edited by
                #7

                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

                mrjjM 1 Reply Last reply
                0
                • yczoY yczo

                  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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @yczo said:

                  Function declaration MainWindow::filterSignal

                  Sounds like you have

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

                  in the .h file

                  1 Reply Last reply
                  0
                  • yczoY Offline
                    yczoY Offline
                    yczo
                    wrote on last edited by yczo
                    #9

                    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.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved