Multi threaded TCP server GUI using Qt widgets application
-
wrote on 17 Dec 2020, 18:44 last edited by fari35This post is deleted!
-
Qt has a threaded server example: https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html
-
Qt has a threaded server example: https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html
wrote on 17 Dec 2020, 19:03 last edited by@Christian-Ehrlicher I know how to make a threaded tcp server console application but I am having issues in making a gui of it. I have edited my question you can see the error there.
-
You should also define your function MyServer::startServer() I would guess.
-
You should also define your function MyServer::startServer() I would guess.
wrote on 17 Dec 2020, 19:14 last edited by@Christian-Ehrlicher yes its defined in server.h file :
void MyServer::startServer() { int port = 54000; if(!this->listen(QHostAddress::Any,port)) { qDebug() << "Could not start server"; } else { qDebug() << "Listening to port " << port << "..."; } }
I am using the same code as in this link:"https://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php"
-
And you added the server.cpp to your pro-file?
-
And you added the server.cpp to your pro-file?
wrote on 17 Dec 2020, 19:24 last edited by@Christian-Ehrlicher yes, see I have added all the files:
QT += core QT +=network QT +=gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 TEMPLATE = app SOURCES += \ main.cpp \ mainwindow.cpp myserver.cpp mythread.cpp HEADERS += \ mainwindow.h myserver.h mythread.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
-
@fari35 said in Multi threaded TCP server GUI using Qt widgets application:
> SOURCES += \ > main.cpp \ > mainwindow.cpp > myserver.cpp > mythread.cpp
This does not look correct - you forgot the \
-
@fari35 said in Multi threaded TCP server GUI using Qt widgets application:
> SOURCES += \ > main.cpp \ > mainwindow.cpp > myserver.cpp > mythread.cpp
This does not look correct - you forgot the \
wrote on 17 Dec 2020, 19:34 last edited by fari35@Christian-Ehrlicher Do I need to put this
\
in front of every file? -
@Christian-Ehrlicher Do I need to put this
\
in front of every file? -
@fari35
No, it's a line continuation character. It must go at the end of each line in a list of file names other than the final one. -
Please post your pro file and start over with a clean build dir
-
Please post your pro file and start over with a clean build dir
wrote on 17 Dec 2020, 19:55 last edited by@Christian-Ehrlicher This is my pro file:
QT += core QT +=network QT +=gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 TEMPLATE = app SOURCES += \ main.cpp \ mainwindow.cpp\ myserver.cpp mythread.cpp HEADERS += \ mainwindow.h\ myserver.h mythread.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
-
You did notice what @JonB and I told you? You forgot to add the
\
... -
You did notice what @JonB and I told you? You forgot to add the
\
...wrote on 18 Dec 2020, 11:44 last edited by fari35@Christian-Ehrlicher @JonB yes Now its working but when I'm trying to create an object of MainWindow class in server.h class so I'm not able to create that. Cany you please tell me how can I make the object of MainWindow class?
server.h:#ifndef SERVER_H #define SERVER_H #include <QStringList> #include <QTcpServer> #include "mainwindow.h" class server : public QTcpServer { Q_OBJECT public: server(QObject *parent = nullptr); QTcpServer *tcpServer; protected: void incomingConnection(qintptr socketDescriptor) override; private: QStringList fortunes; MainWindow uicopy; }; #endif // SERVER_H
-
@Christian-Ehrlicher @JonB yes Now its working but when I'm trying to create an object of MainWindow class in server.h class so I'm not able to create that. Cany you please tell me how can I make the object of MainWindow class?
server.h:#ifndef SERVER_H #define SERVER_H #include <QStringList> #include <QTcpServer> #include "mainwindow.h" class server : public QTcpServer { Q_OBJECT public: server(QObject *parent = nullptr); QTcpServer *tcpServer; protected: void incomingConnection(qintptr socketDescriptor) override; private: QStringList fortunes; MainWindow uicopy; }; #endif // SERVER_H
wrote on 18 Dec 2020, 11:59 last edited by@fari35
You make aMainWindow
object/instance byMainWindow instance
orMainWindow *istance = new MainWindow
.when I'm trying to create an object of MainWindow class in server.h class
You absolutely do not want to create, or reference, any
MainWindow
class/object, or for that matter any UI object, in yourserver.h
/.cpp
files, which should solely work onQTcp...
stuff! And nor should it attempt to#include "mainwindow.h"
....You need to learn to keep your classes separate, each one only doing what it needs for its own specialized functionality.
If, say, you do have a
QMainWindow
in an application usingQTcpServer
,mainwindow
can know aboutserver
, but not the other way round.MainWindow
can call methods inQTcpServer
, but not the other way round. Or, they can use signals/slots if required to communicate.
1/16