Results 1 to 6 of 6

Thread: Need help handling received messages with QWebSocket based GUI

  1. #1
    Join Date
    Aug 2015
    Location
    Buckinghamshire, UK
    Posts
    3
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Need help handling received messages with QWebSocket based GUI

    AIM
    I am writing a Qt GUI client that interacts with a database server via firewall friendly network traffic. In testing this I have created a trivial GUI project opening connections to Qt EchoServer. On button-click I can send a string to EchoServer. What I really want is to allow the user to enter an ID into a Line Edit box, then for my code to send this ID to the server which replies with a string. This string to then be placed into a Line Edit box (or Label) adjacent to the ID Line Edit. Then to move on to the next Line Edit box and so on. The proper application will be doing database lookups - but I am OK with that.

    PROBLEM
    I am stuck on how to handle the reply from the server and how to place it into the correct widget on my form.

    MY ATTEMPTS
    I have tried creating WebSocket in my MainWindow code module and then connecting QWebSocket::textMessageReceived to one of a number of handlers. I have also tried implementing my WebSocket as a thread and waiting for completion before fetching the reply from the WebSocket thread and stuffing the result into my form. Neither of these approaches seem elegant.

    What is the way to do this please? Can anyone explain or provide a link to a web tutorial?

    I am fairly new to Qt and returning to C++ after 10 years of other work (Embedded C, Java, etc)

  2. #2
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need help handling received messages with QWebSocket based GUI

    Ouchh,, my compilers getting confused at line 4 near "placed into Line Edit box"
    Well have you tried qt network project example? what kind of feature you want to add more?


    Added after 37 minutes:


    Ok, I'm pretending know your problem. Please correct me if i have wrong asumption

    1. Rather than using many QLineEdit/QLabel and move the received data between them, better use 2 QLineEdit for UserID & message, and 1 QPlainTextEdit (or even better QTextEdit/QTextBrowser in case you need special coloring) for received message,just like console view/log view. Use "QTextEdit::moveCursor(QTextCursor::Start)" before writing new line
    2. Before sending message to server, you can add the message with UserID using QString append/prepend and separator that has known by server
    3. Parse the message+UserID in server side, execute parsed message to DB, then return the values to client like in echo client-server example
    Last edited by Frdz; 25th August 2015 at 16:59.

  3. #3
    Join Date
    Aug 2015
    Location
    Buckinghamshire, UK
    Posts
    3
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help handling received messages with QWebSocket based GUI

    I really thought I had explained my problem clearly. Evidently this is not the case... Sorry!

    Here we are talking about a GUI in which my user must enter key data, which is validated against a database and the confirmation displayed in the GUI next to the key data input. For example; entering EmployeeID results in my code passing the ID back to the server, then getting the Employee Name back as a string which I then display in the Employee Name box. Whatever widget is chosen for the box. I anticipate this message passing and reply getting being done by JSON via WebSockets. I am OK with database, JSON and the like.

    After EmployeeID is done I can then change focus, or enable the next widget to handle the next item of data. Say, ItemID and ItemName, then BuildingID and BuildingName, etc.

    I don't mean to be vague. I wish to avoid your time being spent explaining stuff that I am not stuck on. What is the best practice to handle sends and replies via QWebSockets in a GUI form? Eg: Send key data to the server and then display the result for a number of sequential inputs.

    Thank you.

  4. #4
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need help handling received messages with QWebSocket based GUI

    Eheh, I'm so sorry for my misunderstanding
    Modifying my prev project, hope can give you a little clue
    Qt Code:
    1. class MyClient;
    2.  
    3. private slots:
    4. void sendMsg();
    5. void receiveMsg();
    6.  
    7. private:
    8. Ui::MyClient *ui;
    9. QLineEdit* activeLineEdit;
    10. QLabel* activeLabel;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "myclient.h"
    2. #include "ui_myclient.h"
    3. #include <QDebug>
    4.  
    5. MyClient::MyClient(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MyClient)
    8. {
    9. ui->setupUi(this);
    10. foreach (QWidget *widget, findChildren<QWidget *>()) //hide all lineedit & label
    11. widget->setVisible(false);
    12. foreach (QLabel *label, findChildren<QLabel *>()) //make label read only to display received data
    13. label->setEnabled(false);
    14. foreach (QLineEdit *lineedit, findChildren<QLineEdit *>()) //create connect signal & slot
    15. connect(lineedit, SIGNAL(editingFinished()), this, SLOT(sendMsg()));
    16. ui->lineEdit_1->setVisible(true); //show the first entry field
    17. ui->label_1->setVisible(true);
    18. connect(&m_webSocket, &QWebSocket::textMessageReceived, this, receivedMsg);
    19. }
    20.  
    21. void MyClient::sendMsg()
    22. {
    23. activeLineEdit = qobject_cast<QLineEdit*>(sender());
    24. qDebug()<<activeLineEdit->text(); //just to check the line edit value
    25. //send the value to server, add header as you need e.g requested objectindex
    26. //objectindex related to enumerator of table/column in your db against existing qlabel
    27. }
    28.  
    29. void MyClient::receiveMsg()
    30. {
    31. bool validity;
    32. //suppose you get data from server e.g (validity~objectindex~receivedmessage)
    33. //parse received message
    34. if (!validity)
    35. {
    36. //re-entry or something
    37. }
    38. else
    39. {
    40. QString testName = QString("label_%1").arg(objectindex);
    41. QString testName2 = QString("label_%1").arg(objectindex+1);
    42. foreach (QLabel *whichlabel, findChildren<QLabel *>())
    43. {
    44. if (whichlabel->objectName()==testName) //lookup right label to place the message
    45. whichlabel->setText(receivedMessage);
    46. if (whichlabel->objectName()==testName2) //show next label
    47. whichlabel->setVisible(true);
    48. }
    49. activeLineEdit->nextInFocusChain()->setVisible(true);//show next field
    50. activeLineEdit->nextInFocusChain()->setFocus();
    51. }
    52. }
    To copy to clipboard, switch view to plain text mode 
    ...all the rest is about qwebsocket connection & data processing in server I think you could manage it.
    For the last part, maybe any expert here have better approach. My method above rely on ordered QObject naming.
    I'v ever seen wysota in different thread using static meta object enumeration, may be can be used to move to next item. Have it try
    Last edited by Frdz; 25th August 2015 at 21:44.

  5. The following user says thank you to Frdz for this useful post:

    JaffaMicrobrain (26th August 2015)

  6. #5
    Join Date
    Aug 2015
    Location
    Buckinghamshire, UK
    Posts
    3
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help handling received messages with QWebSocket based GUI

    Thank you Frdz.

    Of course I would be using message ID's for the server to identify the service request. Funny - it did not occur to me to pass back a reply ID that could be used within the GUI to place the data into the correct widget. Thank you for pointing that out.

    The big question now is...

    Should I run the QWebSocket instance in it's own thread?

    Again, thank you.

  7. #6
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need help handling received messages with QWebSocket based GUI

    Ah, it's up to you. Actually implementation of basic qwebsocket is simple in Qt.
    In my opinion it's depend on how complex your app next expansion, maybe you face security issues or something next day so you need to run it separately to make you ease. Good luck
    ^^

  8. The following user says thank you to Frdz for this useful post:

    JaffaMicrobrain (28th August 2015)

Similar Threads

  1. Replies: 1
    Last Post: 15th October 2014, 22:00
  2. Replies: 1
    Last Post: 15th January 2013, 20:08
  3. Replies: 4
    Last Post: 11th August 2011, 12:31
  4. TCP: Don't process until a whole packet is received
    By P@u1 in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2011, 13:11
  5. Replies: 0
    Last Post: 31st July 2010, 18:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.