[SOLVED] Help using SLOTs



  • I cannot for the life of me, get my slot to be connected/recognized. Any help would be appreciated.

    The following class just connects and polls a simple telnet server running on port 8090. The connection and reading work fine. (I was originally using UDP,hence the weird file/class names).

    xpudp.h
    @#ifndef XPUDP_H
    #define XPUDP_H

    #include <QtNetwork/QTcpSocket>

    class XpUDP : public QObject
    {
    public:
    XpUDP(char * host = "localhost", int port = 8090);
    bool getIsConnected();
    QTcpSocket * getTelnet();
    bool connectTelnet();
    private:
    QTcpSocket * telnet;
    void initSocket();
    bool isConnected;
    char * myhost;
    int myport;
    private slots:
    void readPort();
    };

    #endif // XPUDP_H
    @

    xpudp.cpp
    @#include "xpudp.h"
    #include <iostream>
    #include <QtNetwork>

    XpUDP::XpUDP(char * host, int port)
    {
    myhost = host;
    myport = port;
    initSocket();
    }

    void XpUDP::initSocket()
    {
    telnet = new QTcpSocket;
    isConnected = connectTelnet();
    }

    QTcpSocket * XpUDP::getTelnet() /* Just have this for testing purposes */
    {
    return telnet;
    }

    bool XpUDP::getIsConnected()
    {
    return isConnected;
    }

    void XpUDP::readPort() // This SLOT not being recognized
    {
    int read_ret = 0;
    char buf[512];
    memset(buf, 0, 512);

    read_ret =  telnet->read(buf, 512);
    //printf("recived[%i] '%s'\n", read_ret, buf);
    std::cout << buf;
    memset(buf, 0, 512);
    

    }

    bool XpUDP::connectTelnet()
    {
    telnet->connectToHost(myhost, myport);
    if (telnet->waitForConnected(5000) == false)
    {
    return false;
    }
    else
    {
    connect(telnet, SIGNAL(readyRead()),
    this, SLOT(readPort())); /* I get QObject::connect: No such slot QObject::readPort() */
    return true;
    }
    }
    @
    During runtime, if a connection is made I get this message in the console: QObject::connect: No such slot QObject::readPort(). Any help getting my slot reacting to the signal would be appreciated, or if events would be the way to go.

    I have Qt 4.8.2, using Qt Creator 2.4.1, qmake 2.01a, g++ 4.7.0, on a Linux Fedora 17 x86_64 system.

    FYI if it helps, I am making a moving map (MarbleMap) and instrument cluster for a flight sim.

    Thanks, Dave



  • You forgot to include the Q_OBJECT macro in your class declaration.


  • Moderators

    Be sure and add the Q_OBJECT macro when you define your class.

    [Edit: Andre beat me to it.]



  • Awesome, works now.

    After I added Q_OBJECT, did a make clean and rebuilt; I got an undefined reference to vtable for xxx. After running qmake -r all is good.

    Thanks, Dave


  • Moderators

    Be sure and edit your initial post to add [Solved] to the title. Thanks!


Log in to reply
 

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