Results 1 to 6 of 6

Thread: clean solution?

  1. #1
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default clean solution?

    For a chat program, when I receive some text from a peer, how do I display this in my QTextBrowser?

    I know about QTextBrowser::append(), but what I mean is: how do I get my socket instance to know about my QTextBrowser instance?

    I could pass the pointer to QTextBrowser to QServer's constructor then to QSocket's constructor so that in my receiveText() slot I can use QTextBrowser::append() to output the text I receive... but is there a cleaner solution?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: clean solution?

    You can subclass a socket and make it emit a signal with the message you want to append to the browser. Then you can connect the socket and the browser from within a component which knows them both.

  3. #3
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: clean solution?

    I see what you are saying, and I have already subclassed tcpSocket, but I don't see an easy way to connect the signal to the appropriate slot.

    The code is really simple, its just split into many files.

    MSocket.cpp
    Qt Code:
    1. #include "MSocket.h"
    2.  
    3. void MSocket::recv() {
    4. QTextStream in(this);
    5. QString inChat;
    6. inChat = in.readLine();
    7. //chatOutput->append(inChat); <- this can't work.
    8. }
    To copy to clipboard, switch view to plain text mode 

    MServer.cpp
    Qt Code:
    1. #include "MServer.h"
    2.  
    3. MServer::MServer(QObject* parent) : QTcpServer(parent) {
    4. }
    5.  
    6. void MServer::incomingConnection(int socketDescriptor) {
    7. MSocket* socket = new MSocket(this);
    8. socket->setSocketDescriptor(socketDescriptor);
    9. }
    To copy to clipboard, switch view to plain text mode 

    MChat.cpp
    Qt Code:
    1. #include "MChat.h"
    2. #include <QMessageBox>
    3.  
    4.  
    5. MChat::MChat(QMainWindow *parent) : QMainWindow(parent), tcpServer(NULL) {
    6. setupUi(this);
    7.  
    8. //connections
    9. connect(inputChat, SIGNAL(pressedReturn()),
    10. sendButton, SLOT(animateClick()));
    11.  
    12. }
    13.  
    14. void MChat::on_actionConnect_activated() {
    15. if (NULL == tcpServer) {
    16. tcpServer = new MServer(this);
    17. connect(tcpServer, SIGNAL(inText(QString&)), this,
    18. SLOT(outputText(QString&)));
    19. if (!tcpServer->listen(QHostAddress::Any, 43545)) {
    20. QMessageBox::critical(this, tr("MChat"),
    21. tr("Unable to start the server: %1.")
    22. .arg(tcpServer->errorString()));
    23. close();
    24. return;
    25. }
    26.  
    27. }
    28. }
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: clean solution?

    So my solution is to first emit a signal with MSocket:
    Qt Code:
    1. emit incomingChat(text);
    To copy to clipboard, switch view to plain text mode 
    Catch it with a slot from MServer, and emit it again to MChat:
    Qt Code:
    1. slot relayText(text) {
    2. emit text(text);
    3. }
    To copy to clipboard, switch view to plain text mode 
    Catch it in MChat with a slot:
    Qt Code:
    1. MChat:: outputText(QString& text) {
    2. chatOutput->append(text);
    3. }
    To copy to clipboard, switch view to plain text mode 
    There must be a cleaner way though.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: clean solution?

    You can connect a signal to a signal.

  6. #6
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: clean solution?

    alright, that makes it a little nicer.

Similar Threads

  1. Can't drop a widget in designer - solution
    By heathbar82 in forum Qt Tools
    Replies: 2
    Last Post: 24th July 2012, 15:47
  2. Downloading from a clean url
    By travlr in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2007, 21:55
  3. Clean project deletes makefile
    By Djony in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2006, 10:15
  4. nmake clean and qmke
    By mickey in forum Newbie
    Replies: 1
    Last Post: 13th April 2006, 06:59
  5. Obtaining clean (x)html from QTextEdit
    By ccf_h in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2006, 14:47

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.