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. QtWebSockets and custom Signals
Forum Updated to NodeBB v4.3 + New Features

QtWebSockets and custom Signals

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 875 Views 1 Watching
  • 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1

    So what im trying todo here is to make 2 custom signals from MySocket.cpp to be used in MainWindow.cpp

    what i have so far is i've added 2 Signals under Q_SIGNALS in the socket class onError(); and whenConnected();

    MySocket.h

    #ifndef MYSOCKET_H
    #define MYSOCKET_H
    
    // =========================================================================================================== //
    // INCLUDE
    // =========================================================================================================== //
    #include <QObject>
    #include <QtWebSockets/QWebSocket>
    
    // =========================================================================================================== //
    // Socket CLASS
    // =========================================================================================================== //
    class MySocket : public QObject
    {
        Q_OBJECT
    public:
        explicit MySocket(QObject *parent = nullptr);
        void doConnect(QString URL);
    
    Q_SIGNALS:
        void closed();
        void onError();
        void whenConnected();
    
    private Q_SLOTS:
        void onConnected();
        void sendCommandStrip(const QString &bName, int bValue);
        void sendCommandMatrix(QString bValue);
        void close();
    
    private:
        QWebSocket m_webSocket;
        QUrl m_url;
        bool m_debug;
    };
    
    #endif // MYSOCKET_H
    
    

    MySocket.cpp

    // =========================================================================================================== //
    // INCLUDE
    // =========================================================================================================== //
    #include "MySocket.h"
    #include <QDebug>
    
    QT_USE_NAMESPACE
    
    // =========================================================================================================== //
    // CONSTRUCTOR
    // =========================================================================================================== //
    MySocket::MySocket(QObject *parent) : QObject(parent)
    {
        connect(&m_webSocket, &QWebSocket::connected, this, &MySocket::onConnected);
        connect(&m_webSocket, &QWebSocket::disconnected, this, &MySocket::closed);
        connect(&m_webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [=](QAbstractSocket::SocketError error)
        {
           qDebug() << "[ERROR][SOCKET] " << error;
        });
    
    }
    
    void MySocket::onConnected()
    {
        qDebug() << "We Are Connected";
    }
    // =========================================================================================================== //
    // Socket Connected
    // =========================================================================================================== //
    void MySocket::doConnect(QString URL)
    {
        qDebug() << "WebSocket server:" << URL;
        m_webSocket.open(QUrl(URL));
    }
    // =========================================================================================================== //
    // Send Message (For Strips)
    // =========================================================================================================== //
    void MySocket::sendCommandStrip(const QString &bName, int bValue)
    {
        m_webSocket.sendTextMessage(QStringLiteral("{%0:%1}").arg(bName).arg(bValue));
    }
    // =========================================================================================================== //
    // Send Message (For Matrix)
    // =========================================================================================================== //
    void MySocket::sendCommandMatrix(QString bValue)
    {
        m_webSocket.sendTextMessage(bValue);
    }
    // =========================================================================================================== //
    // Socket Close
    // =========================================================================================================== //
    void MySocket::close()
    {
        m_webSocket.close();
    }
    

    but where togo from here? any help would be awsome :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Connect them in your MainWindow to the appropriate slots and emit them form your subclass when appropriate.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Connect them in your MainWindow to the appropriate slots and emit them form your subclass when appropriate.

        K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #3

        @SGaist MainWindow.cpp or MainWindow.h ?

        and like this

        connect(&m_webSocket, &QWebSocket::connected, this, &MySocket::onConnected); ?

        K 1 Reply Last reply
        0
        • K Kris Revi

          @SGaist MainWindow.cpp or MainWindow.h ?

          and like this

          connect(&m_webSocket, &QWebSocket::connected, this, &MySocket::onConnected); ?

          K Offline
          K Offline
          Kris Revi
          wrote on last edited by
          #4

          Anyone know?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Please show some patience. Allow 24 hours to pass before bumping your own thread. This is a voluntary driven forum and people might not live in the same time zone as you.

            As for your issue, connect is a QObject static method, it follows the same rule as any other C++ static method.

            Your connect statement is wrong, you have a custom class with a custom signal that does not belong to QWebSocket so your connect call will not compile.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply
            1
            • SGaistS SGaist

              Please show some patience. Allow 24 hours to pass before bumping your own thread. This is a voluntary driven forum and people might not live in the same time zone as you.

              As for your issue, connect is a QObject static method, it follows the same rule as any other C++ static method.

              Your connect statement is wrong, you have a custom class with a custom signal that does not belong to QWebSocket so your connect call will not compile.

              K Offline
              K Offline
              Kris Revi
              wrote on last edited by
              #6

              @SGaist said in QtWebSockets and custom Signals:

              Your connect statement is wrong, you have a custom class with a custom signal that does not belong to QWebSocket so your connect call will not compile.

              Im so confused!
              when i try connect(Socket, MySocket::connected, this, MySocket::whenConnected)

              i get 2 errors

              1.no member named 'connected' in 'MySocket
              2. call to non-static member function without an object argument

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kris Revi
                wrote on last edited by
                #7

                Bump

                Anyone?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  As already said:

                  @SGaist said in QtWebSockets and custom Signals:

                  Please show some patience. Allow 24 hours to pass before bumping your own thread. This is a voluntary driven forum and people might not live in the same time zone as you.
                  As for your issue, connect is a QObject static method, it follows the same rule as any other C++ static method.
                  Your connect statement is wrong, you have a custom class with a custom signal that does not belong to QWebSocket so your connect call will not compile.

                  Please check the Signals And Slots chapter of Qt's documentation, it shows how to use the new syntax.

                  Your MySocket class contains a QWebSocket but is not one and does not provide a signal by named connected. Also you are missing the & for your signal and slot arguments.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    As already said:

                    @SGaist said in QtWebSockets and custom Signals:

                    Please show some patience. Allow 24 hours to pass before bumping your own thread. This is a voluntary driven forum and people might not live in the same time zone as you.
                    As for your issue, connect is a QObject static method, it follows the same rule as any other C++ static method.
                    Your connect statement is wrong, you have a custom class with a custom signal that does not belong to QWebSocket so your connect call will not compile.

                    Please check the Signals And Slots chapter of Qt's documentation, it shows how to use the new syntax.

                    Your MySocket class contains a QWebSocket but is not one and does not provide a signal by named connected. Also you are missing the & for your signal and slot arguments.

                    K Offline
                    K Offline
                    Kris Revi
                    wrote on last edited by
                    #9

                    @SGaist

                    i've put Socket(this) under the init of MainWindow

                    void MainWindow::connectTo(QString selected_board)
                    {
                        connect(Socket, &QWebSocket::connected, this, MainWindow::whenConnected);
                        Socket.doConnect(QStringLiteral("ws://%1:80").arg(selected_board));
                    }
                    

                    Now if i try to write

                    connect(Socket, &QWebSocket::connected, this, MainWindow::whenConnected);
                    

                    i get reference to non-static member function must called; dud you mean to call it with no arguments?

                    if i write

                    connect(Socket, &QWebSocket::connected, this, &MainWindow::whenConnected);
                    

                    i get no matching member function for call to 'connect'

                    if i write

                    connect(Socket, &QWebSocket::connected, this, &MainWindow::whenConnected());
                    

                    i get cannot take the address of an rvalue of type 'void'

                    JonBJ 1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Kris-Revi said in QtWebSockets and custom Signals:

                      connect(Socket, &QWebSocket::connected, this, &MainWindow::whenConnected);

                      What exactly is Socket ? An variable ? A class ?

                      If a variable, then it's the only one that has the correct syntax. However, if Socket is an instance of your MySocket class: you cannot connect a signal that is from a different class.

                      In C++, name starting with an uppercase letter usually signals a class. Variable names usually starts with a lowercase letter, or an underscore or some other "prefix" but not uppercased letters. Even if just a convention it's one that is used almost everywhere so it would be good to follow it as well to make your code easier to work with.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • K Kris Revi

                        @SGaist

                        i've put Socket(this) under the init of MainWindow

                        void MainWindow::connectTo(QString selected_board)
                        {
                            connect(Socket, &QWebSocket::connected, this, MainWindow::whenConnected);
                            Socket.doConnect(QStringLiteral("ws://%1:80").arg(selected_board));
                        }
                        

                        Now if i try to write

                        connect(Socket, &QWebSocket::connected, this, MainWindow::whenConnected);
                        

                        i get reference to non-static member function must called; dud you mean to call it with no arguments?

                        if i write

                        connect(Socket, &QWebSocket::connected, this, &MainWindow::whenConnected);
                        

                        i get no matching member function for call to 'connect'

                        if i write

                        connect(Socket, &QWebSocket::connected, this, &MainWindow::whenConnected());
                        

                        i get cannot take the address of an rvalue of type 'void'

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #11

                        @Kris-Revi
                        Further to @SGaist. I've been reading through your code/changes and I too am confused!

                        First show exactly what your Socket is, as he asks. However, additionally I have not seen you define a MainWindow::whenConnected() method, does that exist as a slot in MainWindow?

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          Kris Revi
                          wrote on last edited by
                          #12

                          @SGaist said in QtWebSockets and custom Signals:

                          @Kris-Revi said in QtWebSockets and custom Signals:

                          connect(Socket, &QWebSocket::connected, this, &MainWindow::whenConnected);

                          What exactly is Socket ? An variable ? A class ?

                          If a variable, then it's the only one that has the correct syntax. However, if Socket is an instance of your MySocket class: you cannot connect a signal that is from a different class.

                          In C++, name starting with an uppercase letter usually signals a class. Variable names usually starts with a lowercase letter, or an underscore or some other "prefix" but not uppercased letters. Even if just a convention it's one that is used almost everywhere so it would be good to follow it as well to make your code easier to work with.

                          Socket is MySocket constructor (MySocket is a class)

                          JonBJ 1 Reply Last reply
                          0
                          • K Kris Revi

                            @SGaist said in QtWebSockets and custom Signals:

                            @Kris-Revi said in QtWebSockets and custom Signals:

                            connect(Socket, &QWebSocket::connected, this, &MainWindow::whenConnected);

                            What exactly is Socket ? An variable ? A class ?

                            If a variable, then it's the only one that has the correct syntax. However, if Socket is an instance of your MySocket class: you cannot connect a signal that is from a different class.

                            In C++, name starting with an uppercase letter usually signals a class. Variable names usually starts with a lowercase letter, or an underscore or some other "prefix" but not uppercased letters. Even if just a convention it's one that is used almost everywhere so it would be good to follow it as well to make your code easier to work with.

                            Socket is MySocket constructor (MySocket is a class)

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #13

                            @Kris-Revi said in QtWebSockets and custom Signals:

                            Socket is MySocket constructor (MySocket is a class)

                            Sorry, but "Socket is MySocket constructor" just does not mean anything. Nor do I know what your earlier "i've put Socket(this) under the init of MainWindow" means.

                            If Socket is indeed a class, then you cannot write connect(Socket, ...) in any situation. connect() connects the signal of one object/instance to a slot of another object/instance. Not classes.

                            I think you are wanting to achieve the following:

                            1. You have a class MySocket. It is a QObject, so it can use signals/slots.
                            2. It has a member QWebSocket m_webSocket;. So it contains/encapsulates a QWebSocket, but it is not a QWebSocket itself.
                            3. You define a custom signal whenConnected(), which you want emitted when the socket is connected.
                            4. You define a custom slot in MainWindow, to connect to that signal.

                            So...

                            • Inside MySocket you want to connect m_webSocket's connected signal to a slot, also inside MySocket, which goes emit whenConnected(), to raise/emit that signal to the outside world.
                            • And inside MainWindow you want to define a slot which you connect there to the MySocket::whenConnected() signal.

                            Is that right?

                            1 Reply Last reply
                            1

                            • Login

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