Hi, im new in QT. I have QT 5.5 and i create a very simple chat client-server based on TCP. But when i clicked button on server window to send message, application crashed. Client aplication doesn't get any errors, so i think it's working. I dont know how to fix it :/

Thanks for any reply!!

This is my code that i made:
server.pro
Qt Code:
  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2016-01-17T12:45:39
  4. #
  5. #-------------------------------------------------
  6.  
  7. QT += core gui
  8. QT += network
  9. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  10.  
  11. TARGET = server
  12. TEMPLATE = app
  13.  
  14.  
  15. SOURCES += main.cpp\
  16. server.cpp
  17.  
  18. HEADERS += server.h
  19.  
  20. FORMS += server.ui
To copy to clipboard, switch view to plain text mode 

server.h
Qt Code:
  1. #ifndef SERVER_H
  2. #define SERVER_H
  3.  
  4. #include <QMainWindow>
  5. #include <QTcpServer>
  6. #include <QTcpSocket>
  7. #include <QHostAddress>
  8. #include <QList>
  9.  
  10. namespace Ui {
  11. class server;
  12. }
  13.  
  14. class server : public QMainWindow
  15. {
  16. Q_OBJECT
  17.  
  18. public:
  19. explicit server(QWidget *parent = 0);
  20. ~server();
  21.  
  22. public slots:
  23. void connect_new();
  24. void leer_socketclient();
  25.  
  26. private slots:
  27. void on_pushButton_clicked();
  28.  
  29. private:
  30. Ui::server *ui;
  31. QTcpServer *tcpserver;
  32. QTcpSocket *tcpclient;
  33. };
  34.  
  35. #endif // SERVER_H
To copy to clipboard, switch view to plain text mode 

server.cpp
Qt Code:
  1. #include "server.h"
  2. #include "ui_server.h"
  3.  
  4. server::server(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::server)
  7. {
  8. ui->setupUi(this);
  9.  
  10. tcpserver = new QTcpServer (this);
  11. tcpserver->newConnection();
  12.  
  13. tcpserver->listen(QHostAddress::LocalHost, 1234);
  14. connect(tcpserver, SIGNAL(newConnection()), this, SLOT(connect_new()));
  15. }
  16.  
  17. server::~server()
  18. {
  19. delete ui;
  20. }
  21.  
  22. void server::connect_new()
  23. {
  24. tcpclient = tcpserver->nextPendingConnection();
  25. connect(tcpclient, SIGNAL(readyRead()), this, SLOT(leer_socketclient()));
  26. }
  27.  
  28. void server::leer_socketclient()
  29. {
  30. QByteArray buffer;
  31. buffer.resize( tcpclient->bytesAvailable() );
  32. tcpclient->read( buffer.data(), buffer.size() );
  33. ui->plainTextEdit->setReadOnly( true );
  34. ui->plainTextEdit->appendPlainText( QString (buffer));
  35. }
  36.  
  37. void server::on_pushButton_clicked()
  38. {
  39. tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
  40. ui->lineEdit->clear();
  41. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include "server.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. server w;
  8. w.show();
  9.  
  10. return a.exec();
  11. }
To copy to clipboard, switch view to plain text mode 

=========================================
and client if you check

client.pro
Qt Code:
  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2016-01-17T12:22:05
  4. #
  5. #-------------------------------------------------
  6.  
  7. QT += core gui
  8. QT += network
  9. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  10.  
  11. TARGET = client
  12. TEMPLATE = app
  13.  
  14.  
  15. SOURCES += main.cpp\
  16. client.cpp
  17.  
  18. HEADERS += client.h
  19.  
  20. FORMS += client.ui
To copy to clipboard, switch view to plain text mode 

client.h
Qt Code:
  1. #ifndef CLIENT_H
  2. #define CLIENT_H
  3.  
  4. #include <QMainWindow>
  5. #include <QTcpSocket>
  6. #include <QHostAddress>
  7. #include <QList>
  8.  
  9. namespace Ui {
  10. class client;
  11. }
  12.  
  13. class client : public QMainWindow
  14. {
  15. Q_OBJECT
  16.  
  17. public:
  18. explicit client(QWidget *parent = 0);
  19. ~client();
  20.  
  21.  
  22. private slots:
  23. void on_pushButton_clicked();
  24.  
  25. public slots:
  26. void leer_socketserver();
  27.  
  28. private:
  29. Ui::client *ui;
  30.  
  31. QTcpSocket *tcpclient;
  32. };
  33.  
  34. #endif // CLIENT_H
To copy to clipboard, switch view to plain text mode 

client.cpp
Qt Code:
  1. #include "client.h"
  2. #include "ui_client.h"
  3.  
  4. client::client(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::client)
  7. {
  8. ui->setupUi(this);
  9.  
  10. tcpclient = new QTcpSocket (this);
  11.  
  12. tcpclient->connectToHost( QHostAddress::LocalHost , 1234);
  13.  
  14. connect (tcpclient, SIGNAL (readyRead()), this, SLOT (leer_socketserver()));
  15. }
  16.  
  17. client::~client()
  18. {
  19. delete ui;
  20. }
  21.  
  22. void client::on_pushButton_clicked()
  23. {
  24. tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
  25. ui->lineEdit->clear();
  26. }
  27.  
  28. void client::leer_socketserver()
  29. {
  30. QByteArray buffer;
  31. buffer.resize( tcpclient->bytesAvailable() );
  32. tcpclient->read( buffer.data(), buffer.size() );
  33. ui->plainTextEdit->setReadOnly( true );
  34. ui->plainTextEdit->appendPlainText( QString (buffer));
  35. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include "client.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. client w;
  8. w.show();
  9.  
  10. return a.exec();
  11. }
To copy to clipboard, switch view to plain text mode 

=========================
server.ui
Qt Code:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>server</class>
  4. <widget class="QMainWindow" name="server">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>376</width>
  10. <height>335</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>server</string>
  15. </property>
  16. <widget class="QWidget" name="centralWidget">
  17. <widget class="QLineEdit" name="lineEdit">
  18. <property name="geometry">
  19. <rect>
  20. <x>100</x>
  21. <y>200</y>
  22. <width>261</width>
  23. <height>20</height>
  24. </rect>
  25. </property>
  26. </widget>
  27. <widget class="QLabel" name="label">
  28. <property name="geometry">
  29. <rect>
  30. <x>10</x>
  31. <y>200</y>
  32. <width>91</width>
  33. <height>16</height>
  34. </rect>
  35. </property>
  36. <property name="text">
  37. <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:11pt;&quot;&gt;Wiadomosc:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
  38. </property>
  39. </widget>
  40. <widget class="QLabel" name="label_2">
  41. <property name="geometry">
  42. <rect>
  43. <x>20</x>
  44. <y>10</y>
  45. <width>91</width>
  46. <height>16</height>
  47. </rect>
  48. </property>
  49. <property name="text">
  50. <string>Chat</string>
  51. </property>
  52. </widget>
  53. <widget class="QPushButton" name="pushButton">
  54. <property name="geometry">
  55. <rect>
  56. <x>250</x>
  57. <y>230</y>
  58. <width>111</width>
  59. <height>23</height>
  60. </rect>
  61. </property>
  62. <property name="text">
  63. <string>Wyslij na client</string>
  64. </property>
  65. </widget>
  66. <widget class="QPlainTextEdit" name="plainTextEdit">
  67. <property name="geometry">
  68. <rect>
  69. <x>20</x>
  70. <y>30</y>
  71. <width>341</width>
  72. <height>151</height>
  73. </rect>
  74. </property>
  75. </widget>
  76. </widget>
  77. <widget class="QMenuBar" name="menuBar">
  78. <property name="geometry">
  79. <rect>
  80. <x>0</x>
  81. <y>0</y>
  82. <width>376</width>
  83. <height>21</height>
  84. </rect>
  85. </property>
  86. </widget>
  87. <widget class="QToolBar" name="mainToolBar">
  88. <attribute name="toolBarArea">
  89. <enum>TopToolBarArea</enum>
  90. </attribute>
  91. <attribute name="toolBarBreak">
  92. <bool>false</bool>
  93. </attribute>
  94. </widget>
  95. <widget class="QStatusBar" name="statusBar"/>
  96. </widget>
  97. <layoutdefault spacing="6" margin="11"/>
  98. <resources/>
  99. <connections/>
  100. </ui>
To copy to clipboard, switch view to plain text mode