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
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDialog>
#include <QTcpSocket>
{
Q_OBJECT
public:
public slots:
void Connect();
void appendToWindow();
void sendMessage();
private:
};
#endif
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDialog>
#include <QTcpSocket>
class QLineEdit;
class QLabel;
class QPushButton;
class QTextEdit;
class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
public slots:
void Connect();
void appendToWindow();
void sendMessage();
private:
QLabel *ipLabel, *portLabel;
QPushButton *done, *quit;
QLineEdit *ipLine, *portLine, *write;
QTcpSocket *socket;
QTextEdit *read;
};
#endif
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include <QtGui>
#include <QtNetwork>
MainWindow
::MainWindow(QWidget* parent
){
ipLabel
= new QLabel(tr
("<i><font color=red>IP:</font></i>"));
portLabel
= new QLabel(tr
("<i><font color=red>Port:</font></i>"));
read->setReadOnly(true);
layout->addWidget(ipLabel);
layout->addWidget(ipLine);
layout->addWidget(portLabel);
layout->addWidget(portLine);
layout2->addWidget(done);
layout2->addWidget(quit);
layout3->addWidget(write);
layout3->addWidget(read);
mainLayout->addLayout(layout);
mainLayout->addLayout(layout2);
mainLayout->addLayout(layout3);
setLayout(mainLayout);
connect(done, SIGNAL(clicked()), this, SLOT(Connect()));
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
connect(done, SIGNAL(clicked()), this, SLOT(appendToWindow()));
connect(write, SIGNAL(textChanged(const QString&)), this, SLOT(sendMessage()));
}
void MainWindow::Connect()
{
qint16 port;
if(ipLine->text().isEmpty())
return;
if(portLine->text().isEmpty())
port = 6667;
host = ipLine->text();
socket->connectToHost(host, port);
}
void MainWindow::appendToWindow()
{
char buffer[1024];
while(socket->canReadLine()) {
socket->readLine(buffer, sizeof(buffer));
read->append(buffer);
}
}
void MainWindow::sendMessage()
{
if(write->text().isEmpty())
return;
socket->write(write->text().toLatin1());
}
#include "mainwindow.h"
#include <QtGui>
#include <QtNetwork>
MainWindow::MainWindow(QWidget* parent)
: QDialog(parent)
{
socket = new QTcpSocket(this);
ipLabel = new QLabel(tr("<i><font color=red>IP:</font></i>"));
portLabel = new QLabel(tr("<i><font color=red>Port:</font></i>"));
done = new QPushButton(tr("Connect"));
quit = new QPushButton(tr("Quit"));
ipLine = new QLineEdit;
portLine = new QLineEdit;
write = new QLineEdit;
read = new QTextEdit;
read->setReadOnly(true);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(ipLabel);
layout->addWidget(ipLine);
layout->addWidget(portLabel);
layout->addWidget(portLine);
QVBoxLayout* layout2 = new QVBoxLayout;
layout2->addWidget(done);
layout2->addWidget(quit);
QVBoxLayout* layout3 = new QVBoxLayout;
layout3->addWidget(write);
layout3->addWidget(read);
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addLayout(layout);
mainLayout->addLayout(layout2);
mainLayout->addLayout(layout3);
setLayout(mainLayout);
connect(done, SIGNAL(clicked()), this, SLOT(Connect()));
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
connect(done, SIGNAL(clicked()), this, SLOT(appendToWindow()));
connect(write, SIGNAL(textChanged(const QString&)), this, SLOT(sendMessage()));
}
void MainWindow::Connect()
{
qint16 port;
QString host;
if(ipLine->text().isEmpty())
return;
if(portLine->text().isEmpty())
port = 6667;
host = ipLine->text();
socket->connectToHost(host, port);
}
void MainWindow::appendToWindow()
{
char buffer[1024];
while(socket->canReadLine()) {
socket->readLine(buffer, sizeof(buffer));
read->append(buffer);
}
}
void MainWindow::sendMessage()
{
if(write->text().isEmpty())
return;
socket->write(write->text().toLatin1());
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
w.resize(200, 200);
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.resize(200, 200);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Any help is appreciated.
Bookmarks