QtWebSockets and custom Signals
-
So what im trying todo here is to make 2 custom signals from
MySocket.cpp
to be used inMainWindow.cpp
what i have so far is i've added 2 Signals under
Q_SIGNALS
in the socket classonError();
andwhenConnected();
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 :)
-
Hi,
Connect them in your MainWindow to the appropriate slots and emit them form your subclass when appropriate.
-
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 tryconnect(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
-
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. -
i've put
Socket(this)
under the init of MainWindowvoid 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'
-
@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 aMainWindow::whenConnected()
method, does that exist as a slot inMainWindow
? -
@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
isMySocket
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 writeconnect(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:
- You have a class
MySocket
. It is aQObject
, so it can use signals/slots. - It has a member
QWebSocket m_webSocket;
. So it contains/encapsulates aQWebSocket
, but it is not aQWebSocket
itself. - You define a custom signal
whenConnected()
, which you want emitted when the socket is connected. - You define a custom slot in
MainWindow
, to connect to that signal.
So...
- Inside
MySocket
you want to connectm_webSocket
'sconnected
signal to a slot, also insideMySocket
, which goesemit whenConnected()
, to raise/emit that signal to the outside world. - And inside
MainWindow
you want to define a slot which you connect there to theMySocket::whenConnected()
signal.
Is that right?
- You have a class