Slot not visible after static cast
I reimplemented class QTcpSocket to "Socket". QTcpServer returns only QTcpSocket, so I static casted it to Socket. Program compiles, but slot not visible.
Code:
Socket* soc = static_cast<Socket*> (srv->nextPendingConnection());
In class Socket:
Code:
Header:
public slots:
void dataRead();
CPP:
connect(this, SIGNAL(readyRead()), this, SLOT(dataRead()));
Error message:
Code:
Object
::connect: No such
slot QTcpSocket::dataRead() in ..\Sample\socket.
cpp:16
Re: Slot not visible after static cast
First of all: you want to use danammic_cast not static_cast for polymorphism. (google about the differences and dangers of the various casts).
Since QTcpServer does not inherit from your Socket class, it has no knowledge of the slots you defined in your class - in this case down casting makes only sense if your Socket class has reimplemented QTcpServer methods.
Thats is, if your Socket class is at all related to QTcpServer, since you code does not show that.
Re: Slot not visible after static cast
Tried dynamic_cast, application crashed.
Re: Slot not visible after static cast
Quote:
Tried dynamic_cast, application crashed.
Just using dynamic_cast will not help you either.
Read the rest of what I wrote, and act based on that!
Re: Slot not visible after static cast
The application most likely crashed because dynamic_cast ensures the assigned value is NULL if the type you are casting to is not compatible with the object, and you are probably not checking for NULL.
Therefore, you should re-read high_flyer's response.
Re: Slot not visible after static cast
Forseeing the continuation of this thread I suggest that you subclass QTcpServer (you probably already do that anyway) and reimplement nextPendingConnection() to return an instance of your socket class instead of an instance of QTcpSocket. Of course first you have to understand why you need to do this (which is the mentioned forseen continuation of the thread).