Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QtWebSockets and custom Signals



  • 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 :)


  • Lifetime Qt Champion

    Hi,

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



  • @SGaist MainWindow.cpp or MainWindow.h ?

    and like this

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



  • Anyone know?


  • Lifetime Qt Champion

    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.



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



  • Bump

    Anyone?


  • Lifetime Qt Champion

    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.



  • @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'


  • Lifetime Qt Champion

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



  • @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?



  • @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)



  • @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?


Log in to reply