SIGNAL/SLOT using 'uint16_t' parameter



  • When I declare a SIGNAL/SLOT pair with a single uint16_t parameter I get the following error message :-

    QObject::connect: Cannot queue arguments of type 'uint16_t'.

    Using an int parameter works fine.

    It looks like I need to register the uint16_t type somewhere but I'm unsure where to do this. I tried putting a Q_DECLARE_METATYPE() in mainwindow.h as shown below but this didn't work.

    namespace Ui {
    class MainWindow;
    }

    Q_DECLARE_METATYPE(uint16_t)

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    .
    .
    .

    Where should I place the Q_DECLARE_METATYPE() or am I barking up the wrong tree altogether ?

    Thanks

    Alan



  • http://doc.qt.io/qt-5/qmetatype.html#details

    try calling

    qRegisterMetaType<uint16_t>();
    


  • _t is normally used for typedefs, if it was a typedef for unsigned short as the name suggests then it would work out of the box. this behaviour makes me think there's more behind it. what's the underlying type of uint16_t?


  • Qt Champions 2016


  • Qt Champions 2016

    @Alan-B said in SIGNAL/SLOT using 'uint16_t' parameter:

    Where should I place the Q_DECLARE_METATYPE() or am I barking up the wrong tree altogether ?

    Nope, it's the correct tree.
    You need to put the metatype declaration in one of the header files, then you need to call qRegisterMetaType at runtime (for example in main()), just as @BjornW wrote.



  • Tested this:

    #include <QObject>
    #include <QDebug>
    #include <cstdint>
    
    class TestObj : public QObject{
        Q_OBJECT
        Q_DISABLE_COPY(TestObj)
    public:
        explicit TestObj(QObject* parent=Q_NULLPTR)
            :QObject(parent)
        {
            connect(this, &TestObj::testSig,this, &TestObj::testSlo,Qt::QueuedConnection);
            connect(this, SIGNAL(testSig(std::uint16_t)),this,SLOT(testSlo2(std::uint16_t)),Qt::QueuedConnection);
            testSig(0);
        }
        Q_SIGNAL void testSig(std::uint16_t val);
        Q_SLOT void testSlo(std::uint16_t val){qDebug() << "testSlo " << val;}
        Q_SLOT void testSlo2(std::uint16_t val){qDebug() << "testSlo2 " << val;}
    
    };
    

    basically the solution is either use the New Signal Slot Syntax or register the metatype (I did not realise the metatype was required before as I was always testing using the pointer to method connect


  • Qt Champions 2016

    @VRonin said in SIGNAL/SLOT using 'uint16_t' parameter:

    I did not realise the metatype was required before as I was always testing using the pointer to method connect

    It's required for queued connections regardless of the connection syntax.



  • @kshegunov said in SIGNAL/SLOT using 'uint16_t' parameter:

    It's required for queued connections regardless of the connection syntax.

    Sorry, had to specify.

    • connect(this, &TestObj::testSig,this, &TestObj::testSlo,Qt::QueuedConnection); works correctly
    • connect(this, SIGNAL(testSig(std::uint16_t)),this,SLOT(testSlo2(std::uint16_t)),Qt::QueuedConnection); does not

    so you don't need to register the typedef of a known metatype if you use the new connection. in general, of course, if you have a custom class you have to register it to work in queued connection


Log in to reply
 

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