Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpSocket accessable between different cpp files.
QtWS25 Last Chance

QTcpSocket accessable between different cpp files.

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 345 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NotAChemist
    wrote on last edited by
    #1

    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?

    jsulmJ S 2 Replies Last reply
    0
    • N NotAChemist

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N NotAChemist

        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?

        S Offline
        S Offline
        Simplicius Simplicissimus
        wrote on last edited by
        #3

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

        N 1 Reply Last reply
        0
        • S Simplicius Simplicissimus

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

          N Offline
          N Offline
          NotAChemist
          wrote on last edited by NotAChemist
          #4

          @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);
          
          
          }
          
          jsulmJ 1 Reply Last reply
          0
          • N NotAChemist

            @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);
            
            
            }
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            0
            • jsulmJ jsulm

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

              N Offline
              N Offline
              NotAChemist
              wrote on last edited by
              #6

              @jsulm

              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();
              }
              
              JonBJ 1 Reply Last reply
              0
              • N NotAChemist

                @jsulm

                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();
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @NotAChemist
                At present you must have a DeviceController _controller instance in each dialog. If you want to share one instance across multiple dialogs take it outside of any dialog, e.g. in main or maybe MainWindow. Then you could, for example, pass a pointer to it into each dialog, e.g. explicitly in constructor or via a setDeviceController(). Your connect(&_controller, ...)s become connect(_controller, ...) since it's a pointer now. All dialogs access the one device controller, with its single internal socket, etc.

                S N 2 Replies Last reply
                3
                • JonBJ JonB

                  @NotAChemist
                  At present you must have a DeviceController _controller instance in each dialog. If you want to share one instance across multiple dialogs take it outside of any dialog, e.g. in main or maybe MainWindow. Then you could, for example, pass a pointer to it into each dialog, e.g. explicitly in constructor or via a setDeviceController(). Your connect(&_controller, ...)s become connect(_controller, ...) since it's a pointer now. All dialogs access the one device controller, with its single internal socket, etc.

                  S Offline
                  S Offline
                  Simplicius Simplicissimus
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  1
                  • JonBJ JonB

                    @NotAChemist
                    At present you must have a DeviceController _controller instance in each dialog. If you want to share one instance across multiple dialogs take it outside of any dialog, e.g. in main or maybe MainWindow. Then you could, for example, pass a pointer to it into each dialog, e.g. explicitly in constructor or via a setDeviceController(). Your connect(&_controller, ...)s become connect(_controller, ...) since it's a pointer now. All dialogs access the one device controller, with its single internal socket, etc.

                    N Offline
                    N Offline
                    NotAChemist
                    wrote on last edited by
                    #9

                    @JonB

                    Thank you, could you provide a full example using the connect command?

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved