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
#-------------------------------------------------
#
# Project created by QtCreator 2016-01-17T12:45:39
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = server
TEMPLATE = app
SOURCES += main.cpp\
server.cpp
HEADERS += server.h
FORMS += server.ui
#-------------------------------------------------
#
# Project created by QtCreator 2016-01-17T12:45:39
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = server
TEMPLATE = app
SOURCES += main.cpp\
server.cpp
HEADERS += server.h
FORMS += server.ui
To copy to clipboard, switch view to plain text mode
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QList>
namespace Ui {
class server;
}
{
Q_OBJECT
public:
explicit server
(QWidget *parent
= 0);
~server();
public slots:
void connect_new();
void leer_socketclient();
private slots:
void on_pushButton_clicked();
private:
Ui::server *ui;
};
#endif // SERVER_H
#ifndef SERVER_H
#define SERVER_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QList>
namespace Ui {
class server;
}
class server : public QMainWindow
{
Q_OBJECT
public:
explicit server(QWidget *parent = 0);
~server();
public slots:
void connect_new();
void leer_socketclient();
private slots:
void on_pushButton_clicked();
private:
Ui::server *ui;
QTcpServer *tcpserver;
QTcpSocket *tcpclient;
};
#endif // SERVER_H
To copy to clipboard, switch view to plain text mode
server.cpp
#include "server.h"
#include "ui_server.h"
ui(new Ui::server)
{
ui->setupUi(this);
tcpserver->newConnection();
connect(tcpserver, SIGNAL(newConnection()), this, SLOT(connect_new()));
}
server::~server()
{
delete ui;
}
void server::connect_new()
{
tcpclient = tcpserver->nextPendingConnection();
connect(tcpclient, SIGNAL(readyRead()), this, SLOT(leer_socketclient()));
}
void server::leer_socketclient()
{
buffer.resize( tcpclient->bytesAvailable() );
tcpclient->read( buffer.data(), buffer.size() );
ui->plainTextEdit->setReadOnly( true );
ui
->plainTextEdit
->appendPlainText
( QString (buffer
));
}
void server::on_pushButton_clicked()
{
tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
ui->lineEdit->clear();
}
#include "server.h"
#include "ui_server.h"
server::server(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::server)
{
ui->setupUi(this);
tcpserver = new QTcpServer (this);
tcpserver->newConnection();
tcpserver->listen(QHostAddress::LocalHost, 1234);
connect(tcpserver, SIGNAL(newConnection()), this, SLOT(connect_new()));
}
server::~server()
{
delete ui;
}
void server::connect_new()
{
tcpclient = tcpserver->nextPendingConnection();
connect(tcpclient, SIGNAL(readyRead()), this, SLOT(leer_socketclient()));
}
void server::leer_socketclient()
{
QByteArray buffer;
buffer.resize( tcpclient->bytesAvailable() );
tcpclient->read( buffer.data(), buffer.size() );
ui->plainTextEdit->setReadOnly( true );
ui->plainTextEdit->appendPlainText( QString (buffer));
}
void server::on_pushButton_clicked()
{
tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
ui->lineEdit->clear();
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "server.h"
#include <QApplication>
int main(int argc, char *argv[])
{
server w;
w.show();
return a.exec();
}
#include "server.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
server w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
=========================================
and client if you check
client.pro
#-------------------------------------------------
#
# Project created by QtCreator 2016-01-17T12:22:05
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = client
TEMPLATE = app
SOURCES += main.cpp\
client.cpp
HEADERS += client.h
FORMS += client.ui
#-------------------------------------------------
#
# Project created by QtCreator 2016-01-17T12:22:05
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = client
TEMPLATE = app
SOURCES += main.cpp\
client.cpp
HEADERS += client.h
FORMS += client.ui
To copy to clipboard, switch view to plain text mode
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QHostAddress>
#include <QList>
namespace Ui {
class client;
}
{
Q_OBJECT
public:
explicit client
(QWidget *parent
= 0);
~client();
private slots:
void on_pushButton_clicked();
public slots:
void leer_socketserver();
private:
Ui::client *ui;
};
#endif // CLIENT_H
#ifndef CLIENT_H
#define CLIENT_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QHostAddress>
#include <QList>
namespace Ui {
class client;
}
class client : public QMainWindow
{
Q_OBJECT
public:
explicit client(QWidget *parent = 0);
~client();
private slots:
void on_pushButton_clicked();
public slots:
void leer_socketserver();
private:
Ui::client *ui;
QTcpSocket *tcpclient;
};
#endif // CLIENT_H
To copy to clipboard, switch view to plain text mode
client.cpp
#include "client.h"
#include "ui_client.h"
ui(new Ui::client)
{
ui->setupUi(this);
connect (tcpclient, SIGNAL (readyRead()), this, SLOT (leer_socketserver()));
}
client::~client()
{
delete ui;
}
void client::on_pushButton_clicked()
{
tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
ui->lineEdit->clear();
}
void client::leer_socketserver()
{
buffer.resize( tcpclient->bytesAvailable() );
tcpclient->read( buffer.data(), buffer.size() );
ui->plainTextEdit->setReadOnly( true );
ui
->plainTextEdit
->appendPlainText
( QString (buffer
));
}
#include "client.h"
#include "ui_client.h"
client::client(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::client)
{
ui->setupUi(this);
tcpclient = new QTcpSocket (this);
tcpclient->connectToHost( QHostAddress::LocalHost , 1234);
connect (tcpclient, SIGNAL (readyRead()), this, SLOT (leer_socketserver()));
}
client::~client()
{
delete ui;
}
void client::on_pushButton_clicked()
{
tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
ui->lineEdit->clear();
}
void client::leer_socketserver()
{
QByteArray buffer;
buffer.resize( tcpclient->bytesAvailable() );
tcpclient->read( buffer.data(), buffer.size() );
ui->plainTextEdit->setReadOnly( true );
ui->plainTextEdit->appendPlainText( QString (buffer));
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "client.h"
#include <QApplication>
int main(int argc, char *argv[])
{
client w;
w.show();
return a.exec();
}
#include "client.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
client w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
=========================
server.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>server</class>
<widget class="QMainWindow" name="server">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>376</width>
<height>335</height>
</rect>
</property>
<property name="windowTitle">
<string>server</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>100</x>
<y>200</y>
<width>261</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>200</y>
<width>91</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p><span style=" font-size:11pt;">Wiadomosc:</span></p></body></html></string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>91</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Chat</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>250</x>
<y>230</y>
<width>111</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Wyslij na client</string>
</property>
</widget>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>341</width>
<height>151</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>376</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>server</class>
<widget class="QMainWindow" name="server">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>376</width>
<height>335</height>
</rect>
</property>
<property name="windowTitle">
<string>server</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>100</x>
<y>200</y>
<width>261</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>200</y>
<width>91</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p><span style=" font-size:11pt;">Wiadomosc:</span></p></body></html></string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>91</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Chat</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>250</x>
<y>230</y>
<width>111</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Wyslij na client</string>
</property>
</widget>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>341</width>
<height>151</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>376</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
To copy to clipboard, switch view to plain text mode
Bookmarks