QTcpSocket accessable between different cpp files.
-
New to Qt and c++ programming.
I have a cpp/h (devicemanager) file that creates and connects a TCP socket through signals and slots from another cpp/h (connect1) file. Whenever i want to use that same socket from another cpp/h (connect2) file i get an error that is socket was not created even when i know the other file combo created the socket.
How do i properly allow any cpp files in my project to use the tcp socket created?
-
New to Qt and c++ programming.
I have a cpp/h (devicemanager) file that creates and connects a TCP socket through signals and slots from another cpp/h (connect1) file. Whenever i want to use that same socket from another cpp/h (connect2) file i get an error that is socket was not created even when i know the other file combo created the socket.
How do i properly allow any cpp files in my project to use the tcp socket created?
@NotAChemist said in QTcpSocket accessable between different cpp files.:
How do i properly allow any cpp files in my project to use the tcp socket created?
Dirty solution: put the QTcpSocket variable declaration into a header file and include it in the other cpp file where you want to access it.
This is bad design though. You should rather provide an interface to communicate via this QTcpSocket without accessing it directly. -
New to Qt and c++ programming.
I have a cpp/h (devicemanager) file that creates and connects a TCP socket through signals and slots from another cpp/h (connect1) file. Whenever i want to use that same socket from another cpp/h (connect2) file i get an error that is socket was not created even when i know the other file combo created the socket.
How do i properly allow any cpp files in my project to use the tcp socket created?
@NotAChemist said in QTcpSocket accessable between different cpp files.:
New to Qt and c++ programming.
Whenever i want to use that same socket from another cpp/h (connect2) file i get an error that is socket was not created even when i know the other file combo created the socket.In this precise case – and for more than one reason – it would be best for us to take a look at your code. jsulm is probably right, but I can imagine many things, misunderstanding and general advice that could be necessary.
PSE consider posting the relevant code (formatted as code) here in this thread.
-
@NotAChemist said in QTcpSocket accessable between different cpp files.:
New to Qt and c++ programming.
Whenever i want to use that same socket from another cpp/h (connect2) file i get an error that is socket was not created even when i know the other file combo created the socket.In this precise case – and for more than one reason – it would be best for us to take a look at your code. jsulm is probably right, but I can imagine many things, misunderstanding and general advice that could be necessary.
PSE consider posting the relevant code (formatted as code) here in this thread.
@Simplicius-Simplicissimus @jsulm
Thanks for the help so far. Here is code snippets from my devicecontroller which contains the code to actually connect and connectdialog which i have the slots and signals to initiate a connection through it. This works fine but when i want to add another lets say dialog window to access the socket and send commands it wont work. Tell me if you need more, or something specific.
#include "DeviceController.h" #include <sstream> #include <iomanip> #include <cctype> #include <QThread> DeviceController::DeviceController(QObject *parent) : QObject{parent} { connect(&_socket, &QTcpSocket::connected, this, &DeviceController::connected); connect(&_socket, &QTcpSocket::disconnected, this, &DeviceController::disconnected); connect(&_socket, &QTcpSocket::errorOccurred, this, &DeviceController::errorOccurred); connect(&_socket, &QTcpSocket::stateChanged, this, &DeviceController::socket_stateChanged); //connect(&_socket, &QTcpSocket::readyRead, this, &DeviceController::socket_readyRead); } void DeviceController::connectToDevice(QString ip, int port) { if (_socket.isOpen()) { if (ip == _ip && port == _port) { return; } _socket.close(); } _ip = ip; _port = port; _socket.connectToHost(_ip, _port); _socket.setSocketOption(QAbstractSocket::LowDelayOption, 1); }
void ConnectDialog::setDeviceContoller() { connect(&_controller, &DeviceController::connected, this, &ConnectDialog::device_connected); connect(&_controller, &DeviceController::disconnected, this, &ConnectDialog::device_disconnected); connect(&_controller, &DeviceController::stateChanged, this, &ConnectDialog::device_stateChanged); connect(&_controller, &DeviceController::errorOccurred, this, &ConnectDialog::device_errorOccurred); //connect(&_controller, &DeviceController::socket_readyRead, this, &ConnectDialog::WF_available); connect(&_controller, &DeviceController::WFReady, this, &ConnectDialog::WF_available); connect(&_controller, &DeviceController::emitGates, this, &ConnectDialog::SS_emitedGates); }
-
@Simplicius-Simplicissimus @jsulm
Thanks for the help so far. Here is code snippets from my devicecontroller which contains the code to actually connect and connectdialog which i have the slots and signals to initiate a connection through it. This works fine but when i want to add another lets say dialog window to access the socket and send commands it wont work. Tell me if you need more, or something specific.
#include "DeviceController.h" #include <sstream> #include <iomanip> #include <cctype> #include <QThread> DeviceController::DeviceController(QObject *parent) : QObject{parent} { connect(&_socket, &QTcpSocket::connected, this, &DeviceController::connected); connect(&_socket, &QTcpSocket::disconnected, this, &DeviceController::disconnected); connect(&_socket, &QTcpSocket::errorOccurred, this, &DeviceController::errorOccurred); connect(&_socket, &QTcpSocket::stateChanged, this, &DeviceController::socket_stateChanged); //connect(&_socket, &QTcpSocket::readyRead, this, &DeviceController::socket_readyRead); } void DeviceController::connectToDevice(QString ip, int port) { if (_socket.isOpen()) { if (ip == _ip && port == _port) { return; } _socket.close(); } _ip = ip; _port = port; _socket.connectToHost(_ip, _port); _socket.setSocketOption(QAbstractSocket::LowDelayOption, 1); }
void ConnectDialog::setDeviceContoller() { connect(&_controller, &DeviceController::connected, this, &ConnectDialog::device_connected); connect(&_controller, &DeviceController::disconnected, this, &ConnectDialog::device_disconnected); connect(&_controller, &DeviceController::stateChanged, this, &ConnectDialog::device_stateChanged); connect(&_controller, &DeviceController::errorOccurred, this, &ConnectDialog::device_errorOccurred); //connect(&_controller, &DeviceController::socket_readyRead, this, &ConnectDialog::WF_available); connect(&_controller, &DeviceController::WFReady, this, &ConnectDialog::WF_available); connect(&_controller, &DeviceController::emitGates, this, &ConnectDialog::SS_emitedGates); }
@NotAChemist said in QTcpSocket accessable between different cpp files.:
but when i want to add another lets say dialog window to access the socket
Why should this dialog directly access the socket?!
Or do you mean you want to connect to the signals from DeviceController like you do in ConnectDialog? If so then it should work, if it does not then please tell us what happens and also show how you do that... -
@NotAChemist said in QTcpSocket accessable between different cpp files.:
but when i want to add another lets say dialog window to access the socket
Why should this dialog directly access the socket?!
Or do you mean you want to connect to the signals from DeviceController like you do in ConnectDialog? If so then it should work, if it does not then please tell us what happens and also show how you do that...I want to be able to send commands to the socket to write and read on the socket, on a different dialog window. I have no idea if i need to or not, its more of i followed a tutorial and thats how they did it with only one other dialog box connected to the DeviceController. I tried connecting another dialog box to the DeviceController in the same way i do with the ConnectDialog and it provides that error. Same code and everything but it says that the socket is not connected.
Calling the other dialog window
void MainWindow::on_actionApp2_triggered() { scopeScan.show(); }
other dialog window defining the DeviceManager
private: Ui::ScopeScan *ui; ConnectDialog connectDialog; DeviceController _controller; };
other dialog window calling a function in the DeviceManager cpp
void ScopeScan::on_get_gates_clicked() { _controller.socket_getGate(); }
-
I want to be able to send commands to the socket to write and read on the socket, on a different dialog window. I have no idea if i need to or not, its more of i followed a tutorial and thats how they did it with only one other dialog box connected to the DeviceController. I tried connecting another dialog box to the DeviceController in the same way i do with the ConnectDialog and it provides that error. Same code and everything but it says that the socket is not connected.
Calling the other dialog window
void MainWindow::on_actionApp2_triggered() { scopeScan.show(); }
other dialog window defining the DeviceManager
private: Ui::ScopeScan *ui; ConnectDialog connectDialog; DeviceController _controller; };
other dialog window calling a function in the DeviceManager cpp
void ScopeScan::on_get_gates_clicked() { _controller.socket_getGate(); }
@NotAChemist
At present you must have aDeviceController _controller
instance in each dialog. If you want to share one instance across multiple dialogs take it outside of any dialog, e.g. inmain
or maybeMainWindow
. Then you could, for example, pass a pointer to it into each dialog, e.g. explicitly in constructor or via asetDeviceController()
. Yourconnect(&_controller, ...)
s becomeconnect(_controller, ...)
since it's a pointer now. All dialogs access the one device controller, with its single internal socket, etc. -
@NotAChemist
At present you must have aDeviceController _controller
instance in each dialog. If you want to share one instance across multiple dialogs take it outside of any dialog, e.g. inmain
or maybeMainWindow
. Then you could, for example, pass a pointer to it into each dialog, e.g. explicitly in constructor or via asetDeviceController()
. Yourconnect(&_controller, ...)
s becomeconnect(_controller, ...)
since it's a pointer now. All dialogs access the one device controller, with its single internal socket, etc.@NotAChemist: I am late, again. But JonB has given the best advice. If you are new to C++, too, then the concept of the pointer might quite simply be the origin of - and also the solution to your problem.
I write this because it is not visible to anybody here, what kind of exercise you have already accomplished, before you started this GUI project. But if you have any difficulty with JonB's contribution, then it may not be Qt, that will help you along right now.
-
@NotAChemist
At present you must have aDeviceController _controller
instance in each dialog. If you want to share one instance across multiple dialogs take it outside of any dialog, e.g. inmain
or maybeMainWindow
. Then you could, for example, pass a pointer to it into each dialog, e.g. explicitly in constructor or via asetDeviceController()
. Yourconnect(&_controller, ...)
s becomeconnect(_controller, ...)
since it's a pointer now. All dialogs access the one device controller, with its single internal socket, etc.Thank you, could you provide a full example using the connect command?