Results 1 to 9 of 9

Thread: c++ TcpSocket readAll from other form help

  1. #1
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question c++ TcpSocket readAll from other form help

    I am trying to send and receive data to/from a client with a qtcpserver. This is working, but now I want to send and receive it with an other form. sending the data works. But receiving not. The readReady signal is emitted, but when I do readAll/readLine the string is empty.

    This is the source:

    Qt Code:
    1. //MainWindow
    2. //This is how I open the dialog from the main form, clientSocket is the tcpsocket where I want to send and receive data from.
    3. MessageBox msgform;
    4. msgform.socket = clientSocket;
    5. msgform.setModal(true);
    6. msgform.exec();
    7.  
    8.  
    9.  
    10.  
    11.  
    12.  
    13.  
    14.  
    15.  
    16.  
    17. //messagebox
    18. //This is the full source of messagebox.cpp
    19. #include "messagebox.h"
    20. #include "mainwindow.h"
    21. #include "ui_messagebox.h"
    22.  
    23. MessageBox::MessageBox(QWidget *parent) :
    24. QDialog(parent),
    25. ui(new Ui::MessageBox)
    26. {
    27. ui->setupUi(this);
    28. //connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &MessageBox::OKClicked);
    29. connect(ui->pushButton_3, &QPushButton::clicked, this, &MessageBox::OKClicked);
    30. connect(ui->pushButton, &QPushButton::clicked, this, &MessageBox::RemoveButton);
    31. connect(ui->pushButton_2, &QPushButton::clicked, this, &MessageBox::AddButton);
    32. }
    33.  
    34. MessageBox::~MessageBox()
    35. {
    36. delete ui;
    37. }
    38.  
    39. void MessageBox::OKClicked(){
    40. QTcpSocket *socket2 = socket;//if I try to use socket it gives a lot of errors so I have to use socket2
    41. connect(socket2, SIGNAL(readyRead()), this, SLOT(OnRecieve())); //connecting readready when data has been send
    42. QString buttonArray;
    43. int buttoncount = ui->listWidget->count();
    44. for(int index = 0; index < buttoncount; index++){
    45. QListWidgetItem * item = ui->listWidget->item(index);
    46. if(index == 0){
    47. buttonArray = buttonArray + item->text();
    48. }else{
    49. buttonArray = buttonArray + "-+-" + item->text();
    50. }
    51. }
    52. socket2->write("MESSAGE|" + ui->lineEdit->text().toLocal8Bit() + "|" + ui->plainTextEdit->toPlainText().toLocal8Bit() + "|" + QString::number(ui->comboBox->currentIndex()).toLocal8Bit() + "|" + buttonArray.toLocal8Bit()); //This is send succesfully
    53. }
    54.  
    55. void MessageBox::RemoveButton(){
    56. qDeleteAll(ui->listWidget->selectedItems());
    57. }
    58.  
    59. void MessageBox::AddButton(){
    60. QListWidgetItem *buttonName = new QListWidgetItem;
    61. buttonName->setText("Button name");
    62. buttonName->setFlags (buttonName->flags () | Qt::ItemIsEditable);
    63. ui->listWidget->addItem(buttonName);
    64. }
    65.  
    66. void MessageBox::OnRecieve(){//is executed when I receive data
    67. QTcpSocket *socket2 = static_cast<QTcpSocket*>(sender());
    68. QString receivedText = socket2->readAll();
    69. QStringList receivedArray = receivedText.split("|");
    70. qDebug() << receivedText;//this is always empty
    71. }
    72.  
    73.  
    74.  
    75.  
    76.  
    77.  
    78.  
    79.  
    80.  
    81.  
    82.  
    83. //full source of messagebox.h
    84. #ifndef MESSAGEBOX_H
    85. #define MESSAGEBOX_H
    86.  
    87. #include <QDialog>
    88. #include <QDebug>
    89. #include <QTcpServer>
    90. #include <QTcpSocket>
    91.  
    92. namespace Ui {
    93. class MessageBox;
    94. }
    95.  
    96. class MessageBox : public QDialog
    97. {
    98. Q_OBJECT
    99.  
    100. public:
    101. explicit MessageBox(QWidget *parent = 0);
    102. ~MessageBox();
    103. QTcpSocket *socket;
    104. private:
    105. Ui::MessageBox *ui;
    106. public slots:
    107. void OKClicked();
    108. void RemoveButton();
    109. void AddButton();
    110. void OnRecieve();
    111. };
    112.  
    113. #endif // MESSAGEBOX_H
    To copy to clipboard, switch view to plain text mode 

    Could someone help me with this?
    And if you see any other mistake please say it, this is my third day of qt c++
    Last edited by jwz104; 6th April 2016 at 16:46.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c++ TcpSocket readAll from other form help

    So something else connected to the readyRead() signal and maybe also reads from the socket?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: c++ TcpSocket readAll from other form help

    In the messagebox.cpp there is only one connection to the readyRead()
    But I have a readyRead connection in mainwindow.cpp that is listening for all clients but that doesn't matter right?(Please correct me if I am wrong)

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c++ TcpSocket readAll from other form help

    Quote Originally Posted by jwz104 View Post
    In the messagebox.cpp there is only one connection to the readyRead()
    But I have a readyRead connection in mainwindow.cpp that is listening for all clients but that doesn't matter right?(Please correct me if I am wrong)
    Well, if the slot in the main window doesn't do anything, then it doesn't matter.
    But if it also reads, then that data is no longer available to any other slot.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: c++ TcpSocket readAll from other form help

    Ah than that is my problem, that slot also reads data, but do you know how I can still read the data on messagebox.cpp?
    I think a good solution is to connect the readready SLOT from mainwindow to the readready SLOT from messagebox. But is this possible?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c++ TcpSocket readAll from other form help

    Maybe you need to rethink what you are doing.

    Why did you end up with two places that compete for socket data?

    Cheers,
    _

  7. #7
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: c++ TcpSocket readAll from other form help

    I now know how I can get this working but it isnt fully working. When the readready on the mainform reads something it emits an signal with the peerport and the text it received. Now I want to connect this signal to my new form. This is how I try to do it:
    Qt Code:
    1. MessageBox msgform;
    2. msgform.socket = clientSocket;
    3. connect(this, SIGNAL(RecieveSignal(int,QString)), msgform, SLOT(OnRecieve(int,QString))); //line 150
    4. msgform.setModal(true);
    5. msgform.exec();
    To copy to clipboard, switch view to plain text mode 

    Only when I try to run it I get this error on line 150:
    Qt Code:
    1. error: no matching function for call to 'MainWindow::connect(MainWindow* const, const char*, MessageBox&, const char*)'
    2. connect(this, SIGNAL(RecieveSignal(int,QString)), msgform, SLOT(OnRecieve(int,QString)));
    3. ^
    To copy to clipboard, switch view to plain text mode 
    The signal and the slot both contain an int and a QString. How can I fix this?

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: c++ TcpSocket readAll from other form help

    Line 3 (150) :
    Qt Code:
    1. connect(this, SIGNAL(RecieveSignal(int,QString)), &msgform, SLOT(OnRecieve(int,QString))); //line 150
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Lesiok for this useful post:

    jwz104 (7th April 2016)

  10. #9
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: c++ TcpSocket readAll from other form help

    Thanks! It no fully works!

Similar Threads

  1. serial.readAll() strange problem reading form FTD232
    By linoprit in forum Qt Programming
    Replies: 6
    Last Post: 23rd October 2015, 16:00
  2. QFile readAll() problem..
    By zgulser in forum Newbie
    Replies: 4
    Last Post: 23rd June 2012, 00:11
  3. QTcpSocket, problems with readall()
    By Leo_san in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2011, 23:31
  4. How to obtain a QI,age from QTcpSocket->readAll
    By gorka_sm in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2011, 08:50
  5. QHTTP Readall() function
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 17th April 2007, 00:55

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.