GUI based Threaded Socket Programming
-
Hello dears,
I use this code in a console application:
http://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.phpI want to use this code in an GUI Based app.
The prblem is MainWindow class inheres from QMainWindow. It`s impossible:class MainWindow : public QMainWindow , public QTcpServer
How can I solve this problem?
-
@MohammadReza said in GUI based Threaded Socket Programming:
How can I solve this problem?
By not deriving twice from
QObject
. Derive from theQTcpServer
class in a dedicated class of your own and aggregate an instance of it in your main window. -
Thanks for your reply.
Can you explain your solution in code in detail?
Thanks in advance.
-
Do not create a main window that inherits from QTcpServer, it is bad design :/.
EDIT:
For example, it's better to do
class MainWindow : public QMainWindow { public: MainWindow(); private: QTcpServer* m_pServer; }; ... MainWindow::MainWindow() : QMainWindow() , m_pServer(new QTcpServer(this)) { //Connect relevant signals... }
Altough I would prefer even more separation between TCP stuff and GUI stuff.
-
@MohammadReza said in GUI based Threaded Socket Programming:
Can you explain your solution in code in detail?
More or less I'd had in mind what Bjorn gave as an example.
-
Hi,
Then create a class based on QTcpServer that handles the network stuff the way you want it. There's really no sense in having a widget inherit QTcpServer.
-
It seems you are trying to make a GUI on top of the server rather than a client for your server, isn't it ?
-
Then keep things cleanly separated. You are trying to make a super class that does everything and that's a bad idea.
By the way, why the GUI for the server rather than the clients ?