PDA

View Full Version : Same socket in multiple classes



kupikupi
18th January 2014, 18:10
This might be a general programming question, but since I'm using Qt i thought this would be the right place to ask. I'm programming a GUI client-server application. I'm encountering some difficulties on the client side. I have a login class in which I'm using a socket for the connection ( the user is prompted to introduce IP address and PORT in order to move on). Next, I have some sort of a menu. I want to use the same socket, but since it's declared in the login class, I can't (at this moment). I already tried to use inheritance; it looks something like:

class Footbal : public QDialog, public Login

But I get an error:

‘QObject’ is an ambiguos base of ‘Footbal’ - 4 times. And a warning : Class Footbal inherits from two QObject subclasses QDialog and Login. This is not supported!

From what I noticed, it's because of QDialog - I tried to use QApplication instead but I got errors since I work with setModal() function.
I really need an alternative. Singleton pattern would be the worst case scenario since I'm terrible at design patterns.

Thanks for reading all this much

anda_skoa
18th January 2014, 18:54
If you need access to the socket then just pass the socket to whatever code needs it.

Either using a getter in Login to return the socket pointer or let Login emit the socket through a signal when login succeeded.

Cheers,
_

kupikupi
18th January 2014, 21:35
If i try to create an int type getter i get:
error: invalid conversion from 'QTcpSocket*' to 'int' [-fpermissive]
If i try to create an QTcpSocket getter i get an error related to QTcpSocket class file. I'm still unable to make it work

anda_skoa
19th January 2014, 10:44
Well, int is obviously not the same as a pointer to QTcpSocket, no?



QTcpSocket Login::socket() const
{
return m_socket;
}


Cheers,
_

kupikupi
19th January 2014, 11:30
I get an error message:
C:\Qt\Qt5.1.1\5.1.1\mingw48_32\include\QtNetwork\q tcpsocket.h:64: error: 'QTcpSocket::QTcpSocket(const QTcpSocket&)' is private
Q_DISABLE_COPY(QTcpSocket)
^

anda_skoa
19th January 2014, 16:26
Yes, typo, sorry. As I said return a pointer. Missed the *



QTcpSocket *Login::socket() const
{
return m_socket;
}


Cheers,
_