Results 1 to 6 of 6

Thread: QTcpSocket

  1. #1
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket

    Hi, can someone help me with this code?
    I don't know if it connects or anything it just doesnt write anything to the "QTextEdit* read"
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QDialog>
    5. #include <QTcpSocket>
    6.  
    7. class QLineEdit;
    8. class QLabel;
    9. class QTextEdit;
    10.  
    11. class MainWindow : public QDialog
    12. {
    13. Q_OBJECT
    14. public:
    15. MainWindow(QWidget* parent = 0);
    16.  
    17. public slots:
    18. void Connect();
    19. void appendToWindow();
    20. void sendMessage();
    21.  
    22. private:
    23. QLabel *ipLabel, *portLabel;
    24. QPushButton *done, *quit;
    25. QLineEdit *ipLine, *portLine, *write;
    26. QTcpSocket *socket;
    27. QTextEdit *read;
    28. };
    29. #endif
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QtGui>
    3. #include <QtNetwork>
    4.  
    5. MainWindow::MainWindow(QWidget* parent)
    6. : QDialog(parent)
    7. {
    8. socket = new QTcpSocket(this);
    9. ipLabel = new QLabel(tr("<i><font color=red>IP:</font></i>"));
    10. portLabel = new QLabel(tr("<i><font color=red>Port:</font></i>"));
    11. done = new QPushButton(tr("Connect"));
    12. quit = new QPushButton(tr("Quit"));
    13. ipLine = new QLineEdit;
    14. portLine = new QLineEdit;
    15. write = new QLineEdit;
    16. read = new QTextEdit;
    17.  
    18. read->setReadOnly(true);
    19.  
    20. QVBoxLayout* layout = new QVBoxLayout;
    21. layout->addWidget(ipLabel);
    22. layout->addWidget(ipLine);
    23. layout->addWidget(portLabel);
    24. layout->addWidget(portLine);
    25.  
    26. QVBoxLayout* layout2 = new QVBoxLayout;
    27. layout2->addWidget(done);
    28. layout2->addWidget(quit);
    29.  
    30. QVBoxLayout* layout3 = new QVBoxLayout;
    31. layout3->addWidget(write);
    32. layout3->addWidget(read);
    33.  
    34. QHBoxLayout* mainLayout = new QHBoxLayout;
    35. mainLayout->addLayout(layout);
    36. mainLayout->addLayout(layout2);
    37. mainLayout->addLayout(layout3);
    38. setLayout(mainLayout);
    39.  
    40. connect(done, SIGNAL(clicked()), this, SLOT(Connect()));
    41. connect(quit, SIGNAL(clicked()), this, SLOT(close()));
    42. connect(done, SIGNAL(clicked()), this, SLOT(appendToWindow()));
    43. connect(write, SIGNAL(textChanged(const QString&)), this, SLOT(sendMessage()));
    44. }
    45.  
    46. void MainWindow::Connect()
    47. {
    48. qint16 port;
    49. QString host;
    50. if(ipLine->text().isEmpty())
    51. return;
    52.  
    53. if(portLine->text().isEmpty())
    54. port = 6667;
    55.  
    56. host = ipLine->text();
    57. socket->connectToHost(host, port);
    58. }
    59.  
    60. void MainWindow::appendToWindow()
    61. {
    62. char buffer[1024];
    63. while(socket->canReadLine()) {
    64. socket->readLine(buffer, sizeof(buffer));
    65. read->append(buffer);
    66. }
    67. }
    68.  
    69. void MainWindow::sendMessage()
    70. {
    71. if(write->text().isEmpty())
    72. return;
    73.  
    74. socket->write(write->text().toLatin1());
    75. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. w.resize(200, 200);
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Any help is appreciated.

  2. #2
    Join Date
    Aug 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket

    Quote Originally Posted by Fallen_ View Post
    Hi, can someone help me with this code?
    I don't know if it connects or anything it just doesnt write anything to the "QTextEdit* read"
    mainwindow.h
    Qt Code:
    1. connect(write, SIGNAL(textChanged(const QString&)), this, SLOT(sendMessage()));
    2. }
    To copy to clipboard, switch view to plain text mode 
    Hi,

    This part is problemmatic I guess. The signal and corresponding slot should have the same arguments. The connection should be like
    Qt Code:
    1. connect(write, SIGNAL(textChanged(QString)), this, SLOT(sendMessage(QString)));
    To copy to clipboard, switch view to plain text mode 

    Your slot should be declared as

    void sendMessage(const QString& dummy)

    I hope this solves your problem.

  3. #3
    Join Date
    Aug 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket

    The reason you cannot type to "TextEdit* read" is

    Qt Code:
    1. read->setReadOnly(true);
    To copy to clipboard, switch view to plain text mode 
    if you change the argument to "false", you will be able to write in the "read".

  4. #4
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket

    Quote Originally Posted by hkaraoguz84 View Post
    Hi,

    This part is problemmatic I guess. The signal and corresponding slot should have the same arguments. The connection should be like
    Qt Code:
    1. connect(write, SIGNAL(textChanged(QString)), this, SLOT(sendMessage(QString)));
    To copy to clipboard, switch view to plain text mode 

    Your slot should be declared as

    void sendMessage(const QString& dummy)

    I hope this solves your problem.
    my problem is that im not receiving anything from the socket & i need to be able to receive & send, this function:
    Qt Code:
    1. void MainWindow::appendToWindow()
    2. {
    3. char buffer[1024];
    4. while(socket->canReadLine()) {
    5. socket->readLine(buffer, sizeof(buffer));
    6. read->append(buffer);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by hkaraoguz84 View Post
    The reason you cannot type to "TextEdit* read" is


    if you change the argument to "false", you will be able to write in the "read".
    i don't want to write here, this text edit is for receiving data from the socket ;/
    --
    edit:
    i made something like this:
    Qt Code:
    1. if(socket->waitForConnected(-1) == true)
    2. read->append("Connected.");
    3. else
    4. read->append(socket->errorString());
    To copy to clipboard, switch view to plain text mode 
    it gave me this:
    Qt Code:
    1. Socket operation timed out
    To copy to clipboard, switch view to plain text mode 
    Last edited by Fallen_; 24th August 2010 at 23:04.

  5. #5
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket

    bump.........

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTcpSocket

    The textChanged signal is fired every time you press a key. This might not be the behaviour you want.

    Use the readyRead() signal of the socket and connect it to your appendToWindow() slot.

    By the way, if a socket times out, that's in most cases not a problem with your code.

Similar Threads

  1. Need Help with QTcpSocket
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 13:55
  2. QTcpSocket help
    By tsd-charlie in forum Newbie
    Replies: 1
    Last Post: 6th August 2009, 19:40
  3. Using QTcpSocket
    By mraittin in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2007, 09:54
  4. QTcpSocket and Qt 4.2.3
    By slcotter in forum Qt Programming
    Replies: 4
    Last Post: 9th May 2007, 16:14
  5. QT4.2.2 and QTcpSocket
    By Nyphel in forum Qt Programming
    Replies: 13
    Last Post: 11th March 2007, 11:30

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.