Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Subclassing QSerialPort - can't connect to slot



  • I'm trying something that looks straight forward enough:

    This is my header:
    @
    class SerialPort : public QSerialPort
    {
    public:
    SerialPort(QObject *parent = 0);

    public slots:
    void handleReadyRead();
    };
    @

    And the implementation:
    @
    SerialPort::SerialPort(QObject *parent)
    : QSerialPort(parent)
    {
    // This connection fails.
    bool ok = connect(this, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
    }

    void SerialPort::handleReadyRead()
    {
    }
    @

    When debugging, I see this warning:
    QObject::connect: No such slot QSerialPort::handleReadyRead() in ..\serialport.cpp:6

    But I don't understand why this fails. And why the error message refers to a slot in the parent QSerialPort and not to my subclass, SerialPort.

    Could anyone spot what I'm doing wrong?



  • Ah I found the answer through this SO post:
    http://stackoverflow.com/questions/4322224/qt-slots-and-inheritance-why-is-my-program-trying-to-connect-to-the-parent-inst

    I needed to put the Q_OBJECT macro in my subclass to enable the signals and slots mechanism, even when subclassing a QObject subclass... Works now.


Log in to reply