Results 1 to 6 of 6

Thread: Slot not visible after static cast

  1. #1
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Qt Code:
    1. Socket* soc = static_cast<Socket*> (srv->nextPendingConnection());
    To copy to clipboard, switch view to plain text mode 

    In class Socket:
    Qt Code:
    1. Header:
    2. public slots:
    3. void dataRead();
    4.  
    5. CPP:
    6. connect(this, SIGNAL(readyRead()), this, SLOT(dataRead()));
    To copy to clipboard, switch view to plain text mode 

    Error message:
    Qt Code:
    1. Object::connect: No such slot QTcpSocket::dataRead() in ..\Sample\socket.cpp:16
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot not visible after static cast

    Tried dynamic_cast, application crashed.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Slot not visible after static cast

    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!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QMetaObject cast Error
    By nirab_25 in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2010, 08:58
  2. Why doesn't this cast?
    By spraff in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2008, 19:28
  3. C# and performance with cast and hashtable
    By mickey in forum General Programming
    Replies: 8
    Last Post: 24th January 2008, 16:51
  4. cast
    By mickey in forum General Programming
    Replies: 1
    Last Post: 12th July 2006, 11:10
  5. Cast problem
    By yellowmat in forum Newbie
    Replies: 3
    Last Post: 7th February 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.